0
votes

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

Edit: Right now I go from the object space down to the window space, 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):

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?

1

1 Answers

0
votes

The most direct approach I could think of would be to simply transform your object space position (let this be called vector x in the following) into eye space, construct a ray from the origin to that eye-space coordinates and calculate the intersection between that ray and the near plane z_eye=-near.

Another approach would be to fully transfrom into the clip space. Since the near plane is z_clip = - w_clip there, you can just set the z coordinate of the result to -w, and project that back to eye space by using the inverse projection matrix.

In both cases, the result will be meaningless if the point lies behind the camera, or at the camera plane z_eye = 0.