[gameprogrammer] Re: OpenGL and mouse click location

I'll add in my answer as well :) In simple cases such as yours, it's

>sufficient to use gluUnProject()
>
>Stephane
>
Point View::mouseClickedOn(int mouseX, int mouseY) {
   
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;

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

    winX = (float)mouseX;
    winY = (float)viewport[3] - (float)mouseY;
    winZ = (float)-10.0f;
    //glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT,
&winZ );

    gluUnProject( winX, winY, winZ, modelview, projection, viewport,
&posX, &posY, &posZ);
   
    Point p;
    p.y=posY;
    p.x=posX;
   
    return p;
}

I tried it with this code (modified from nehe) but I can't get it to
work. The width of a quadratic tile is 1.0f, the z-coordinate is -10.0f.
I only get weird coordinates with this code. Does anybody know how to do
it correctly?


---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: