Advertisement

Article 13 - Using gluUnProject (with LWJGL)

Started by March 22, 2010 04:02 PM
0 comments, last by Kazade 14 years, 7 months ago
Hi There First off, awesome lessons and articles. Love NeHe! I've started working with the LWJGL for Java, and spent some time figured out how to rewrite the code example in Article 13 about using gluUnProject for shooting a ray from mouse click to get a point in 3D. Thought that I might save some people the trouble of rewriting it if they need it for LWJGL. All credit goes to Luke Benstead for orginal code. Here goes! You need these imports: import org.lwjgl.opengl.GL11; import org.lwjgl.util.glu.GLU; import org.lwjgl.BufferUtils; And this is the actually method: static public FloatBuffer getMousePosition(int mouseX, int mouseY) { IntBuffer viewport = BufferUtils.createIntBuffer(16); FloatBuffer modelview = BufferUtils.createFloatBuffer(16); FloatBuffer projection = BufferUtils.createFloatBuffer(16); FloatBuffer winZ = BufferUtils.createFloatBuffer(1); float winX, winY; FloatBuffer position = BufferUtils.createFloatBuffer(3); GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview ); GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection ); GL11.glGetInteger( GL11.GL_VIEWPORT, viewport ); winX = (float)mouseX; winY = (float)viewport.get(3) - (float)mouseY; GL11.glReadPixels(mouseX, (int)winY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ); GLU.gluUnProject(winX, winY, winZ.get(), modelview, projection, viewport, position); return position; } If you get a DirectBuffer exception when reading the position from the FloatBuffer, just use position.get(0) for X, position.get(1) for Y and position.get(2) for Z. Enjoy!
Hi Crede,

Thanks for porting, nice to see that code is still useful to people - I wrote it so long ago!

Luke. ;)
Member of the NeHe team.

This topic is closed to new replies.

Advertisement