Solved

unexpected depth value returned by gluUnProject

Posted on 2004-09-09
4
413 Views
Last Modified: 2013-12-06
I've found the resources on the net to use gluUnProject() to get the world (x,y,z) values given a screen coordinate. The code seems to work, but I'm a little confused by the z value returned. The application renders a triangle strip in which all z values are -3.0f, but the z value returned from gluUnProject is -1.0f.

The application sets the viewport with

    gluPerspective(37.0, (GLfloat)w/(GLfloat)h, 1, 20.0);

The vertices get setup with
    glBegin(GL_TRIANGLE_STRIP);
    glVertex3f( xstep, ystep, -3.0f);
    ...

Any idea why the z value isn't -3.0f?
0
Comment
Question by:ctuffli
  • 3
4 Comments
 
LVL 17

Expert Comment

by:davebytes
ID: 12018593
From :
>>>>
gluUnProject() does the opposite. It takes an XYZ window coordinate and returns the back-transformed XYZ object coordinate equivalent.

The concept of window space Z is often confusing. It's the depth buffer value expressed as a GLdouble in the range 0.0 to 1.0. Assuming a default glDepthRange(), a window coordinate with a Z value of 0.0 corresponds to an eye coordinate located on the zNear clipping plane. Similarly, a window space Z value of 1.0 corresponds to an eye space coordinate located on the zFar plane. You can obtain any window space Z value by reading the depth buffer with glReadPixels().
<<<<

So, basically depending upon the screen [z] value, you'll get back different world [z] values.  If you have a particular x,y on the screen that you want to get the world coordinates for the pixel, you need to do a readpixels and get that pixel's actual z-buffer value, and pass that into UnProject.

That help?

-d
0
 
LVL 17

Expert Comment

by:davebytes
ID: 12018602
Sorry, clicked submit and didn't mean to...

That was from http://www.opengl.org/resources/faq/technical/glu.htm 

Also look at http://www.gamedev.net/community/forums/topic.asp?topic_id=268798 for another discussion of the topic.

-d
0
 
LVL 1

Author Comment

by:ctuffli
ID: 12021491
I had found the description about how window Z is confusing and added a glReadPixels call to the application to get the depth value. The depth is coming back zero which causes the UnProject to generate a -1 for Z (if I understand the above). So does this mean that the Z value will always come back either zNear or zFar? Does the application need to use the depth value to scale between zNear and zFar?
0
 
LVL 17

Accepted Solution

by:
davebytes earned 250 total points
ID: 12030378
If the pixel has nothing in it, it'll be whatever Z you ClearDepth to, and that should translate approximately to zFar.   Thought I'm not sure that things on the near or far plane have well defined values, may be clamped, which could easily be the -1 you are getting.

If the pixel has something in it, it should translate to the approximate z value.

I don't think you need to be doing any further scaling...

-d
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question