c++openglgraphicsmatrixray-picking

How to reorientate the near and far planes from gluUnProject to account for camera position and angle?


I am trying to have a ray be traced from a mouse click on the screen into 3D space. From what I understand, the default position and orientation looks like this:

gluUnprojection

from this code:

    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];

    GLfloat winX, winY;
    GLdouble near_x, near_y, near_z;
    GLdouble far_x, far_y, far_z;

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

    winX = (float)mouse_x;
    winY = (float)viewport[3] - (float)mouse_y;

    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);

My question is how do you change the position and the angle of the near and far planes so that all mouse clicks will appear to be from the camera's position and angle in 3D space?

Specifically, I am looking for a way to position both the projection planes' center at 65° below the Z axis along the line (0,0,1) -> (0,0,-tan(65°)) [-tan(65°) ~= -2.145] with both the near and far planes perpendicular to the view line through those points.


Solution

  • My question is how do you change the position and the angle of the near and far planes so that all mouse clicks will appear to be from the camera's position and angle in 3D space?

    Feeding the projection and modelview matrices into gluUnProject takes care of that. Specifically you pass the projection and modelview matrix used for rendering into gluUnProject so that the unprojected coordinates match those of the local coordinate system described by those matrices.