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
    Member 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
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    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
    Member 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
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    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

Similar Threads

  1. Camera space shadow mapping clip space issues
    By fredvam in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 09-27-2015, 04:58 PM
  2. Computing radiance in world space, object space or view space?
    By shapeare in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-25-2013, 06:22 PM
  3. How to get the camera's position in 3d space?
    By mobilevisuals in forum OpenGL ES
    Replies: 1
    Last Post: 03-11-2010, 11:34 AM
  4. Eye position in object space?
    By mattjakob in forum OpenGL: General
    Replies: 2
    Last Post: 11-06-2009, 08:06 AM
  5. NVIDIA ARB light.position still in object space?
    By Zeno in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 02-27-2003, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean