Search code examples
openglscreencoordinate-systemsdepth

relation from normalized device coordinate to screen coordinate?


I am confused in the relation between ndc to screen coordinate system. What i did was

  1. select screen coordinates (for example touch points on the screen).

  2. then pass the screen coordinates to gluUnproject. i set the depth variable(third variable of gluUnproject) to 0.0f.

  3. then i multiply the object coordinates to modelviewmatrix and projectionmatrix.

  4. after that I clipped the space. then i divide the clip space coordinate with W

  5. at last i scaled the ndc coordinates to screen coordinates.
  6. so the result was equal to coordinates which i first chose except Z coordinates. actually the result of Z was equal to -1.0f

after this I changed the gluUnproject`s third variable to 1.0f which i suppose the result was equal to 0.0f. But the result was 1.0f.

So here is the question what is relation between normalized coordinate system and screen coordinate system. how do i set the depth variable of gluUnproject function?


Solution

  • What is relation between normalized coordinate system and screen coordinate system?

    Normalized device coordinates are expressed in the range [-1,+1] and are obtained from the clip-space coordinates dividing them by their W component.

    They are transformed in Window-space coordinates by the viewport parameters and the depth range parameters.


    How do i set the depth variable of gluUnproject function?

    If you want to convert the Window-space coordinates back to Normalized device coordinates you need the Window-space z value at the point you want to convert. So, directly from the OpenGL FAQs:

    GLdouble z;
    glReadPixels (x, y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z);
    

    Please note that OpenGL store non-linear depth in the depth buffer.