3d mouse coords.

I need to transform the mouse coords recieved from windows messages into 3d world coords. Has anyone done this? thanks

Check this page http://www.opengl.org/developers/faqs/technical/selection.htm , it has some information.

Nate Miller

To get your 3D coords, you need to get the depth-value of your x-y position. Use glReadPixels with GL_DEPTH_COMPONENT for this.
Then use gluUnproject to convert from this x-y-z-screen coordinates to your world coordinates.
Have search on gluUnproject on this forum, and you will find some posts.


Thanks for the page nate. It seems to be working, except the retrieved mouse coords seem to be rather high. Such as -89, 41, -94. This doesnt seem to correct when im clicking on an object thats no more then 1x1 unit. I use this.

float y;
float winz;
GLdouble wx, wy, wz;
GLdouble wx2, wy2, wz2;

y = vp[3] - MPos.y;

gluUnProject((GLdouble)MPos.x, (GLdouble)y, (GLdouble)0.0, mv, pro, vp, &wx, &wy, &wz);

gluUnProject((GLdouble)MPos.x, (GLdouble)y, (GLdouble)1.0, mv, pro, vp, &wx2, &wy2, &wz2);

CVector nearp = CVector(wx, wy, wz);
CVector farp = CVector(wx2, wy2, wz2);
CVector ray = nearp - farp;

It just doesnt seem that these values are correct. thanks.

~ Chris