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

Thread: How can I get the camera space position on the near plane from the object space posit

  1. #1
    Intern Contributor
    Join Date
    Dec 2011
    Posts
    85

    How can I get the camera space position on the near plane from the object space posit

    As titled, I have an item with a specific position in object space defined by a single vector.


    I would like to retrieve the coordinates in camera space of the projection of this vector on the near clipping plane.


    In other words, I need the intersection in camera space between this vector and the plane defined by the z coordinate equals to -1 (my near plane).


    I needed it for moving object linearly with the mouse in perspective projection

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    894
    You apply the same transformations as OpenGL does for rendering:

    Code :
    vec4 v_world      = model_matrix * v_object
    vec4 v_eye_space  = view_matrix  * v_world
    vec4 v_clip_space = projection_matrix * v_eye_space
    vec3 v_ndc        = v_clip_space.xyz / v_clip_space.w    // perspective division, ndc = normalized device coordinates
    vec3 v_pixel      = viewport_transform * v_ndc           // treats xy quite different from z

    Model and view matrix are often combined into the modelview matrix.

    Since you are saying your near plane is at -1 I'm guessing you want normalized device coordinates?

  3. #3
    Intern Contributor
    Join Date
    Dec 2011
    Posts
    85
    Quote Originally Posted by carsten neumann View Post
    You apply the same transformations as OpenGL does for rendering:

    Code :
    vec4 v_world      = model_matrix * v_object
    vec4 v_eye_space  = view_matrix  * v_world
    vec4 v_clip_space = projection_matrix * v_eye_space
    vec3 v_ndc        = v_clip_space.xyz / v_clip_space.w    // perspective division, ndc = normalized device coordinates
    vec3 v_pixel      = viewport_transform * v_ndc           // treats xy quite different from z

    Model and view matrix are often combined into the modelview matrix.

    Since you are saying your near plane is at -1 I'm guessing you want normalized device coordinates?
    Right now I am doing partially what you say. I go from the object space down to the window space (or as you call it, pixel), then from there up to the camera space by setting the window depth window.z equal to 0, that is the near plane.

    Note that to get the camera space from the unProject I just pass in as modelview matrix an identity matrix "new Mat4(1f)":

    Code :
    public Vec3 getCameraSpacePositionOnNearPlane(Vec2i mousePoint) {
     
            int[] viewport = new int[]{0, 0, glViewer.getGlWindow().getWidth(), glViewer.getGlWindow().getHeight()};
     
            Vec3 window = new Vec3();
     
            window.x = mousePoint.x;
            window.y = viewport[3] - mousePoint.y - 1;
            window.z = 0;
     
            return Jglm.unProject(window, new Mat4(1f), glViewer.getVehicleCameraToClipMatrix(), new Vec4(viewport));
        }

    Is there a better way (more efficient) to get it without going down to the window space and come back to the camera one?

    Ps: no, I dont need NDC

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    894
    The 'direct' approach if you will would be to apply the modelview matrix to get a position in eye space. There the camera is at the origin looking along the negative z axis, the near plane is located at z coordinate -near, and parallel to the camera xy plane. What you are looking for is the intersection of the ray from the origin to the eye space position with that plane. You can find it by scaling the vector such that it's z component is -near.
    I doubt it would be a big difference in performance (unless you do this for a very large number of points each frame) and which way is easier to understand is perhaps also a matter of taste

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
  •