[SOLVED] [LWJGL] 3d World co-ordinates to 2d screen co-ordinates

So I’ve been trying to convert 3d world co-ordinates to 2d screen co-ordinates for a few days. I’ve done a bit of reading, I’ve seen that you need to multiple the Projection Matrix by the View Matrix to get the Projection View Matrix. You then have to times the Projection View Matrix by the 3d world position.

I saw that you have to do something like this:


        Vector4f point4 = new Vector4f(point.x, point.y, point.z, 1);
        point4 = Matrix4f.transform(viewMatrix, point4, null);
        point4 = Matrix4f.transform(projectionMatrix, point4, null);
        point = new Vector3f(point4);

        point.x /= point.z;
        point.y /= point.z;

        point.x = (point.x + 1) * Display.getWidth() / 2;
        point.y = (point.y + 1) * Display.getHeight() / 2;

        return new Vector2f(point.x, point.y);

But the result is off by a bit and it depends on each location. Here’s the log of one of them:


Point: Vector3f[96.8019, 4.925174, -49.371254]
Mouse X and Y: 373 397
Returned Mouse Value: Vector2f[364.56116, 398.16907]

Hope you can help me

  • Will

Everything is correct what you said, except that you must divide the resulting 4D-vector in clip space (after matrix multiplications) by its ‘w’ component (and not ‘z’!) to get from homogeneous coordinates/space to real 3D coordinates in normalized device coordinates.

Thanks so much! Out of curiosity, do you know what this process is called?

“Projection”

Oh yeah… duh. Sorry :stuck_out_tongue: