Jassel41

08-04-2010, 09: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

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