PDA

View Full Version : gluUnProject Error



Jassel41
08-04-2010, 10:31 PM
Hi,

I am using gluUnProject to find the necessary translation for an object to be at a given pixel of (x,y). My goal is to find the center of the screen and see what the translation would need to be to place an object in the at the center (x,y) pixel location of the 640x480 window. Below is an example of the code I am using.




int screen_height = 480;
int screen_width = 640;

glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);

gluProject(pos_x, pos_y, pos_z, model_view, projection, viewport, &pos3D_x, &pos3D_y, &pos3D_z);

current_pixel_x = (int)pos3D_x;
current_pixel_y = screen_height - (int)pos3D_y;

// Up to here works perfectly with the correct pixel output based on the current translation of the object

// My attempt to find translation to get to center x and center y pixel

GLdouble z;

glReadPixels (20, 20, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z);

gluUnProject((GLdouble)(screen_width/2), (GLdouble)(screen_height/2), z, model_view, projection, viewport, &pos3D_x, &pos3D_y, &pos3D_z);

center_screen_pixel_x = (int)pos3D_x;
center_screen__pixel_y = (int)pos3D_y;

// I would then translate the object in the x by center_screen_pixel_x and in the y by center_..._y



I also tried subtracting ysize/2 from viewport[3] because I saw some people do that in examples but it did not change anything.

So this is a two part question, for one, is my idea of gluUnProject in the first place correct? Where you input a pixel location and the output will be the necessary translation coordinates for the object to get translated there?

Secondly, if that is the right assumption, can someone please aid me in using the function correctly.

Thank you in advance,
Jassel41

Rosario Leonardi
08-05-2010, 05:58 AM
Why did you get the depth of the 20, 20 pixel if you need the position of the center of the screen?
What behaviour do you get?

There is a simpler (and faster) way to compute the center of the screen using the camera matrix. The point on the center of the screen lie in the Z negative axis of the camera.

Jassel41
08-05-2010, 07:06 AM
Sorry I made the mistake of having 20,20. In my code I have it has the correct location of the center.

This is just one example of where I need to use gluUnProject so although it would be faster to get the center of the screen I also need to use it to find the translation for the four corners of the screen.

Can this be done using the matrix alone as well? If so, can you provide a quick example to doing so?

Thank you,
Jassel41