Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Is it really that I can't get a correct 3D coordinate from 2D screen?

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2003
    Posts
    21

    Is it really that I can't get a correct 3D coordinate from 2D screen?

    I wish to convert 2D screen coordinate to 3D(code below). But the Z value converted always can't control well. It's often worry.
    How can I do?

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);
    winX=(float)mouse_x;
    winY=(float)viewport[3]-(float)mouse_y;
    glReadPixels(mouse_x,
    int(winY),
    1,1,
    GL_DEPTH_COMPONENT,
    GL_FLOAT,
    &winZ);
    gluUnProject((GLdouble)winX,(GLdouble)winY,(GLdoub le)winZ,modelview,projection,viewport,&object_x,&o bject_y,&object_z);

  2. #2
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,142

    Re: Is it really that I can't get a correct 3D coordinate from 2D screen?

    I see nothing wrong with your code. Of course, you might get some numerical errors, floating point math can never be exact.

    Perhaps the modelview matrix at the time your code gets executed is not the same as the one used when drawing...

    Btw. I don't think this is an advanced question

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2003
    Posts
    21

    Re: Is it really that I can't get a correct 3D coordinate from 2D screen?

    But I can't get the correct 3D coordinate. The x,y and z, especially z, are worry with these codes. It is difficultly to control the Z value.

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2004
    Location
    Greece
    Posts
    1

    Re: Is it really that I can't get a correct 3D coordinate from 2D screen?

    I have the same problem and I can not get the z coordinate from the z buffer. Please, somebody help us

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2006
    Location
    Moscow, Russia
    Posts
    23

    Re: Is it really that I can't get a correct 3D coordinate from 2D screen?

    Well, these difficulties may be connected to internal conversion of depth values. Remember that if you have set near and far depth with glDepthRange(n, f) or if your are using default values n = 0 and f = 1, the depth is converted according to the following formula:
    zw = ((f-n)/2) * z + (f+n)/2;
    Therfore, in the most simple case you just get your depth value divided by 2 before actually being written to the depth buffer (remember that after projection the depth value is between -1 and 1). I've had a problem with this once; so, you could just transform the value back (that is, multiply by 2 and subtract 1 in the simplest case) to get the value you think is correct.

  6. #6
    Intern Contributor
    Join Date
    Apr 2005
    Posts
    98

    Re: Is it really that I can't get a correct 3D coordinate from 2D screen?

    The y coordinate should actually be
    newY = height - y - 1

    Secondly, the depth coordinates are not part of a linear scale. The closer the depth is to the near clipping plane, the better the accuracy. The farther it is, the more likely you are to run into issues. Make sure your near and far clipping planes are only as far apart as you need them to be, otherwise you are introducing greater error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •