Thread: How to transform a screen coordinate to OpenGL coordinate

1. How to transform a screen coordinate to OpenGL coordinate

Hi there , i got an question here...
I was building an test with Win32 API,and i want get the vertex location in OpenGL coordinate when i receive WM_LBUTTONDOWN
I use those code as follow:
Code :
//isn't an typical transform code?
GLdouble modelMatrix[16];
GLdouble projectMatrix[16];
GLint viewport[4];
GLdouble winX,winY,winZ;
GLdouble objX,objY,objZ;
Vector3f pos;  //That's a class,I decalre in other file

glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projectMatrix);
//x,y are argument
winX = (double)x;
winY = viewport[3] - y;

//if nothing goes wrong, why winZ almost equal 0
//so do the objX,objY and objZ... and i can't understand why
gluUnProject(winX,winY,winZ,modelMatrix,projectMatrix,viewport,&objX,&objY,&objZ);

pos.x = static_cast<float>(objX);
pos.y = static_cast<float>(objY);
pos.z = static_cast<float>(objZ);

return pos;

2. The z value is totally dependent on the values near and far used is the projection matrix. It is a non-linear value representing how far the point is from the near plane. 0 - on the near plane, 1 on the far plane.

3. I set far as 32000.0,is it why i get winZ value like 0.61e-64?if so, how can i transform winZ to an correct value?
Thanks

4. Trying Googling "Ray Picking" I think that's the term for what you're trying to achieve.

6. from the 3rd result in Google -> http://schabby.de/picking-opengl-ray-tracing/

Picking is the process of finding objects in your scene based on user input. Most commonly, you want to determine what object a user has clicked with his mouse. The 2D mouse coordinates serve as a reference on the view port to identify the projected objected that has been clicked. A similar scenario is a first person shooter, where the gun is basically the picking pointer. If you shoot the gun, the trajectory of the bullet is traced through the scene and collisions are detected, similar to a laser pointer shooting a ray through a scene until it hits an object and marks it with a small red dot.

7. Originally Posted by Caesirekin
I set far as 32000.0,is it why i get winZ value like 0.61e-64?if so, how can i transform winZ to an correct value?
Thanks
Lots of ways to skin that cat. gluUnProject will work. Or here is some shader code you can easily convert to C++ if desired that will do this for a perspective projection link. See the PositionFromDepth_DarkPhoton() function toward the end. Just afterwards, tells you how to simplify it alot if you're using symmetric perspective. Also, to your questions about the non-linear 0..1 window-space Z values stored in the framebuffer, see the eye.z line for how to get from non-linear window-space z (depth) to eye-space Z (eye.z).

8. Originally Posted by Dark Photon
Lots of ways to skin that cat. gluUnProject will work. Or here is some shader code you can easily convert to C++ if desired that will do this for a perspective projection link. See the PositionFromDepth_DarkPhoton() function toward the end. Just afterwards, tells you how to simplify it alot if you're using symmetric perspective. Also, to your questions about the non-linear 0..1 window-space Z values stored in the framebuffer, see the eye.z line for how to get from non-linear window-space z (depth) to eye-space Z (eye.z).
That is very detailed,And i'm afraid that might have a long long time to understand it,by the way....what is ndc-space?

9. It's the space before window space, where X,Y,Z are all within -1..1.

Conceptually, think about taking your view frustum, and stretching/squeezing it into a cube (not quite correct but close). That's NDC.