Incorrect ray creation in OpenGL from a mouse click when clicking on the extreams of the x-axis of the window?

StackOverflow https://stackoverflow.com/questions/17126617

Domanda

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)

Example

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?

È stato utile?

Soluzione

At first glance it looks like you're passing the wrong aspect ratio to gluPerspective. The second parameter should be width/height.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top