Java LWJGL OpenGL converts a 3D point to a 2d point

I am trying to convert a 3d dot in OpenGL to a 2d dot on the screen in order to display the security panel for the little game that I am writing. However, I am having some problems getting the x coordinate for drawing the property bar. In principle, the security panel should be taller than the player, but should always have the same width / height relative to the screen.

I changed the piece of code that I found from the accepted answer to Converts a three-dimensional location to a two-dimensional screen point. (XYZ => XY) and now I have this

public static int[] getScreenCoords(double x, double y, double z) { FloatBuffer screenCoords = BufferUtils.createFloatBuffer(4); IntBuffer viewport = BufferUtils.createIntBuffer(16); FloatBuffer modelView = BufferUtils.createFloatBuffer(16); FloatBuffer projection = BufferUtils.createFloatBuffer(16); // int[] screenCoords = new double[4]; // int[] viewport = new int[4]; // double[] modelView = new double[16]; // double[] projection = new double[16]; GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelView); GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection); GL11.glGetInteger(GL11.GL_VIEWPORT, viewport); boolean result = GLU.gluProject((float) x, (float) y, (float) z, modelView, projection, viewport, screenCoords); if (result) { return new int[] { (int) screenCoords.get(3), (int) screenCoords.get(1) }; } return null; } 

It seems to work fine with the y coordinate, however x always returns 0 regardless of the angle.

Thank you very much in advance!

+2
java opengl lwjgl
source share
1 answer

screenCoords.get(3) must be screenCoords.get(0) because the x position is stored at index 0. You also actually need the screenCoords capacity for 3 floats, not 4.

+3
source share

All Articles