lwjgl3 world to screen coordinates?

I have been looking for a solution how could I get 2d screen coordinates of a 3d point in 3d world. I have tried to find some kind of answer that is “clear enough for me”. Most of the stuff that I find are how screen coordinates are translated to world coordinates but I need the opposite. Could somebody please give me some direction? I would be very grateful!

Maybe it can help you…

This is called projection. Old GLU should have a gluProject(…) method. Go check it out

Oh I actually solved this by “accident”… It took way too long to do this, but I am very noob in this matrix stuff… ::slight_smile:

Decided to put this here if some as unskilled person as me is wondering the same thing…

public Vector2f getScreenCoords(Matrix4f viewMatrix, Matrix4f projectionMatrix) {

		Matrix4f modelViewMatrix = new Matrix4f();
		modelViewMatrix = Matrix4f.mul(viewMatrix, transformationMatrix, modelViewMatrix);
		
	     viewProjectionMatrix = Matrix4f.mul(projectionMatrix, modelViewMatrix, viewProjectionMatrix);
	     
	     
	     Matrix4f worldCoords = viewProjectionMatrix;
	    
	     float x = worldCoords.m00 + worldCoords.m10 + worldCoords.m20 + worldCoords.m30;
	     float y = worldCoords.m01 + worldCoords.m11 + worldCoords.m21 + worldCoords.m31;
	     float z = worldCoords.m02 + worldCoords.m12 + worldCoords.m22 + worldCoords.m32;
	     float w = worldCoords.m03 + worldCoords.m13 + worldCoords.m23 + worldCoords.m33;
	     
	     float finalX = (x / z);
	     float finalY = (y / z);
	     
	     
	     System.out.println(y);
	     
	     return new Vector2f(finalX, finalY);
	}

…At least it seems to work just fine :persecutioncomplex:

As a working alternative when using JOML (since 1.1.7 June, 6th 2015):
When you want to obtain the window-space coordinates for a 3D point in world coordinates for a given viewport, then do:


Matrix4f viewProjMatrix = ...;
Vector3f worldPosition = ...;
int[] viewport = ...;
Vector3f winCoords = viewProjMatrix.project(worldPosition, viewport, new Vector3f());

When you want to obtain the NDC coordinates for a 3D point in world coordinates, then do:


Matrix4f viewProjMatrix = ...;
Vector3f worldPosition = ...;
Vector3f ndcCoords = viewProjMatrix.transformProject(worldPosition, new Vector3f());