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 3 of 3

Thread: Normalized device coordinates for world coordinates using orthographic projection

  1. #1
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    61

    Question Normalized device coordinates for world coordinates using orthographic projection

    Hi there,

    let's say I have world coordinates stored in a variable Position and I want to calculate
    the Normalized Device Coordinates (NDC) for this Position on the CPU in C++ code.

    Using a ModelViewProjectionMatrix which contains a perspective projection matrix I would do it like this:

    Code :
    // Calculate clip space position
    s_Position= ModelViewProjectionMatrix * glm::vec4(Position[0], Position[1], Position[2], 1.0f);
     
    // Transform from clip space to normalized device coordiantes by doing the "perspective divide" [-1,1]
    // See here: [URL]http://www.songho.ca/opengl/gl_transform.html[/URL]
    s_Position.x= s_Position.x / s_Position.w;
    s_Position.y= s_Position.y / s_Position.w;
    s_Position.z= s_Position.z / s_Position.w;
     
    // Calculate window depth coordinate which would be the value written to the depth buffer [0,1]
    s_Position.z= s_Position.z * 0.5f + 0.5f;

    This should give me s_Position's x and y in a range [-1,1] and z in a range [0,1].
    I hope that is correct so far.

    Now I want to achieve the same thing but with a ModelViewProjectionMatrix which contains
    an orthographic projection matrix.


    But in this case w is always 1 so there is no "perpsective-divide".

    So which step is performed between vertex and fragment shader to
    get the NDC and how can I achieve the same result on CPU side?

    Help is really appreciated!
    ++i--;

  2. #2
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    6,031
    It is the exact same math. Dividing by 1 is legal math; it simply does nothing.

  3. #3
    Intern Contributor
    Join Date
    Nov 2011
    Posts
    61
    I just noticed that I did a mistake in my high level code when switching to orthographic projection.
    The result was that my debug output showed me not normalized values in s_Position and I started
    to debug this "perspective-divide" stuff with w = 1 and so on. Sorry as you said it works fine.
    ++i--;

Posting Permissions

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