Jump to content
  • Advertisement
Sign in to follow this  

OpenGL gluUnProject with JOGL -java

This topic is 3083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,
I searched the forum but I couldnt find the solutions. So I want to explain my problem maybe someone help me.I am using JOGL (Opengl bindings for Java) I want to get the world coordinates from the mouse click. I create a terrain. When I click somewhere.I will find the real position on the terrain. so I cant find the nearest point.

my init function()

public void init(GLAutoDrawable drawable) {

gl = drawable.getGL();// get the current GL object
glu = new GLU // get the current GLU object

gl.glEnable (GL.GL_RGB);// single buffer always in JOGL, RGB color mode
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // set display window color to white

/* Set mode for constructing geometric transformation matrix. */

gl.glMatrixMode (GL.GL_MODELVIEW);
glu.gluLookAt (100,100,100, -10,-10,-10,0,0,1);

// set parameters for world-coordinate clipping window

gl.glLoadIdentity(); // sets projection matrix to identity
gl.glFrustum (xwMin, xwMax, ywMin, ywMax, dnear, dfar); // set frustum (-100,-100,100,100,45,125)


and I use this codes in display function


this is the my function wich find the value of world coordinate system

public void FindMousePoint(GL gl,int x,int y)

gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix, 0);
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix, 0);
/* note viewport[3] is height of window in pixels */
realy = viewport[3] - (int) y ;

System.out.println("Coordinates at cursor are (" + x + ", " + realy);

FloatBuffer fb=ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asFloatBuffer();

gl.glReadPixels(y, realy, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, fb);

glu.gluUnProject((double)x (double)realy,0,mvmatrix,0,projmatrix,0,viewport,0,wcoordnear,0);
System.out.println("World coords at z=0.0 are ( " //
+ wcoordnear[0] + ", " + wcoordnear[1] + ", " + wcoordnear[2]
+ ")");

glu.gluUnProject((double) x, (double) realy, 1.0, //
mvmatrix, 0,
projmatrix, 0,
viewport, 0,
wcoordFar, 0);
System.out.println("World coords at z=1.0 are (" //
+ wcoordFar[0] + ", " + wcoordFar[1] + ", " + wcoordFar[2]
+ ")");


I dont know it is wrong way or not.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!