Results 1 to 9 of 9

Thread: How to transform a screen coordinate to OpenGL coordinate

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2013
    Posts
    12

    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
    	glReadPixels((int)winX,(int)winY,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&winZ);
                  //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. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    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. #3
    Junior Member Newbie
    Join Date
    Mar 2013
    Posts
    12
    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. #4
    Junior Member Newbie
    Join Date
    Dec 2013
    Location
    UK
    Posts
    5
    Trying Googling "Ray Picking" I think that's the term for what you're trying to achieve.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2013
    Posts
    12
    Thanks for your adivse,may i ask for the core idea of ray-picking?

  6. #6
    Junior Member Newbie
    Join Date
    Dec 2013
    Location
    UK
    Posts
    5
    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. #7
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,651
    Quote Originally Posted by Caesirekin View Post
    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. #8
    Junior Member Newbie
    Join Date
    Mar 2013
    Posts
    12
    Quote Originally Posted by Dark Photon View Post
    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. #9
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,651
    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.

Similar Threads

  1. How to convert screen coordinate to 3D world coordinate
    By happy_sweet_juice in forum OpenGL: General
    Replies: 6
    Last Post: 06-10-2017, 09:34 AM
  2. From Screen Coordinate to OpenGL coordinate
    By apapaxionga in forum OpenGL: Advanced Coding
    Replies: 8
    Last Post: 10-12-2011, 12:39 PM
  3. Transform from window coordinate to OpenGL coordinate
    By Debugger in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-27-2007, 07:24 PM
  4. Coordinate transform question
    By ZhenwenHe in forum OpenGL: User Software
    Replies: 1
    Last Post: 06-26-2004, 12:22 AM
  5. how to get screen coordinate from world coordinate?
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-14-2003, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean