View Full Version : How to transform a screen coordinate to OpenGL coordinate

Caesirekin

12-09-2013, 08:50 AM

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:

//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

glReadPixels((int)winX,(int)winY,1,1,GL_DEPTH_COMP ONENT,GL_FLOAT,&winZ);

//so do the objX,objY and objZ... and i can't understand why

gluUnProject(winX,winY,winZ,modelMatrix,projectMat rix,viewport,&objX,&objY,&objZ);

pos.x = static_cast<float>(objX);

pos.y = static_cast<float>(objY);

pos.z = static_cast<float>(objZ);

return pos;

tonyo_au

12-09-2013, 03:05 PM

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.

Caesirekin

12-10-2013, 03:52 AM

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

Reesrevo

12-10-2013, 04:12 AM

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

Caesirekin

12-10-2013, 04:48 AM

Thanks for your adivse,may i ask for the core idea of ray-picking?

Reesrevo

12-10-2013, 05:00 AM

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.

Dark Photon

12-10-2013, 06:17 AM

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 (http://www.opengl.org/discussion_boards/showthread.php/173019-View-Space-Light-Position-Moving?p=1212628&viewfull=1#post1212628). 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).

Caesirekin

12-10-2013, 11:16 PM

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 (http://www.opengl.org/discussion_boards/showthread.php/173019-View-Space-Light-Position-Moving?p=1212628&viewfull=1#post1212628). 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?

Dark Photon

12-11-2013, 09:29 AM

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.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.