PDA

View Full Version : Inncorrect 3D ray created when clicking on the extreams of window?



z_Zelman
06-15-2013, 02:56 PM
I have run into a bug that I am not sure how to fix. When I click on the window the point in 3D space does not map directly onto the click in the x-axis of the actual window. The discrepancy between the clicked point and the actual creation of the ray in 3D space increases proportionally as you get farther and farther from the middle of the x-axis in the window. The y-axis works fine, it is just the x-axis.

Here is a picture of squares being created in 3D space where I click the mouse. I clicked only on the edges and moved around the window. (some squares look like hexagons because the camera angle is pointed down, but that is not important, the gap on the left and right is)

http://i.imgur.com/TWLaJ6v.jpg

Here is the code I used to create the ray in 3D space from a click in the window:

GLfloat window_height = 800;
GLfloat window_width = 800;

void proccess_Mouse(int button, int state, int x, int y) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(camera->getAngle(), window_height / window_width, viewPlane_close, viewPlane_far);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera->getIndex_position(0), camera->getIndex_position(1), camera->getIndex_position(2),
camera->getIndex_looking(0), camera->getIndex_looking(1), camera->getIndex_looking(2),
0, -1, 0);

// matrix information
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];

GLfloat winX, winY; // clicked coords on the screen
GLdouble near_x, near_y, near_z; // position of the mouse on the near view plane
GLdouble far_x, far_y, far_z; // position of the mouse on the far view plane

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

winX = (float)x;
winY = (float)viewport[3] - (float)y; // window height - click position

gluUnProject(winX, winY, 0.0, modelview, projection, viewport, &near_x, &near_y, &near_z);
gluUnProject(winX, winY, 1.0, modelview, projection, viewport, &far_x, &far_y, &far_z);

Vector3f* near = new Vector3f(near_x, near_y, near_z);
Vector3f* far = new Vector3f(far_x, far_y, far_z);

...
}

I believe that I have missed something, or forgot something, but I don't see it. Any ideas?

GClements
06-15-2013, 05:55 PM
gluPerspective(camera->getAngle(), window_height / window_width, viewPlane_close, viewPlane_far);

The aspect parameter is inverted; it should be width/height. But that will just result in a distorted view if the window isn't square; it shouldn't affect the calculation.

z_Zelman
06-18-2013, 10:21 AM
I found the problem, it was the aspect parameter, only in a different place. It was in the 3D initialization (as apposed to the 2D initialization for the HUD), so it did not mater that this code was correct (aside from the width/height, height/width thing), the aspect would always get overridden.

To anyone who has this problem, check ALL gluPerspective's throughout your code, they can over write each other.