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: Need help with glm::project & glm::unproject

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2011
    Location
    Halle/Saale, Saxony Anhalt, Germany
    Posts
    6

    Question Need help with glm::project & glm::unproject

    Hello,

    I'm using the glm library for my project and now i want to calculate the screen and world coordinates. But somehow something went totally wrong.
    For example if I want to get the screen coords from the camera target point (x 3575675.000000 y 0.000000 z 5674475.000000) with glm:roject I get the right x and y coordinates of the screen, but the z value seems not correct in this example i get x 149.0 y 121.5 z 0.999. Now as I thought z=0.99 would mean that the camera target point would nearly on the farplane of the perspective, but the farplane is defined as 100000.f. Now using this screen coords and unproject them with glm::unproject gives the world coordinates x 3575667.750000 y 29989.830000 z 5674465.000000. The new y value is nearly the distance between camera position and target position. What am I doing wrong?

    projection matrix:
    Code :
           P = glm::perspective(fov, ratio, 0.1f, farPlane); //fov 45 ratio winW/winH farplane 100000

    model matrix:
    Code :
        glm::mat4 T     = glm::translate(glm::mat4(1.0f),glm::vec3(X, Y, Z));
        glm::mat4 Rx    = glm::rotate(T,  aX, glm::vec3(1.0f, 0.0f, 0.0f));
        glm::mat4 Ry    = glm::rotate(Rx, aX, glm::vec3(0.0f, 1.0f, 0.0f));
        glm::mat4 Rz    = glm::rotate(Ry, aZ, glm::vec3(0.0f, 0.0f, 1.0f));
        M     = glm::scale(Rz, glm::vec3(scaleXYZ));

    view matrix
    Code :
        V = glm::lookAt( glm::vec3( posX, posY, posZ ),glm::vec3( tarX, tarY, tarZ ),glm::vec3( 0.0f, 1.0f, 0.0f ) );

    viewport:
    Code :
       glViewport(0, 0, wWidth, wHeight);
       viewport=glm::vec4(0, 0, wWidth, wHeight);

    screen to world:
    Code :
       glm::project(inPos, V*M, P, viewport);

    world to screen:
    Code :
       glm::vec3 screen=glm::vec3(inVert.X(),wHeight-inVert.Y(),inVert.Z());
       glm::vec3 world=glm::unProject(screen, V*M, P, viewport);

    output:
    point: 3575675.00 0.00 5674475.000
    point to screen: 149.00 121.50 0.99
    screen to world: 3575667.75 29989.83 5674465.00

    Hope anyone can help me...

    greetz caaso

  2. #2
    Junior Member Newbie
    Join Date
    Feb 2011
    Location
    Halle/Saale, Saxony Anhalt, Germany
    Posts
    6
    No one who can help?

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Now as I thought z=0.99 would mean that the camera target point would nearly on the farplane
    This will be a long way short of the far plane with near 0.1f and farPlane 100000.
    If you want better results from project/unproject you will need to go to double precision or keep near/far much closer together.
    You might want to do some hand (ie with calculator) calulation for z with different near/far values. You will be surprised how much of you view is in the z-depth range of 0.999 - 1.0

Tags for this Thread

Posting Permissions

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