gluUnproject errors (JOGL)

Started by
1 comment, last by LilBudyWizer 18 years, 5 months ago
I'm trying to get the world position from a given position on the openGL canvas, accesible by a mouse click. this is then transformed in to a 3d Position. ////////////////////////////// //GLEVENTLISTENER implementation ////////////////////////////// public void display(GLDrawable drawable) { if( mouseClicked){ render.get3DPoint(oglMouseX, oglMouseY, drawable); mouseClicked = false; } } now the the gluUnproject code: gl = d.getGL(); glu = d.getGLU(); double[] mvmatrix = new double[16]; double[] projmatrix = new double[16]; int[] viewport = new int[4]; double[] objPos = new double[3]; double dClickY; gl.glGetIntegerv(GL.GL_VIEWPORT, viewport); gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix); gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix); dClickY = (double) (viewportHeight - y); glu.gluUnProject( (double)x, dClickY, 0.0, mvmatrix, projmatrix, viewport, objPos); but the objPos always seems to be (0,0,0); which seems coz this are the values of the matrices: PROJECTION MATRIX -1.7018005847930908 -0.12783338129520416 0.6429447531700134 0.6429447531700134 -1.436849594116211 0.1514054834842682 -0.7615019679069519 -0.7615019679069519 0.0 2.4060678482055664 0.08207805454730988 0.08207805454730988 0.0 0.0 0.0 0.0 MODELVIEW MATRIX 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 VIEWPORT 0 0 594 548 thanks in advance
Cosmic Keys to my Creations & Times
Advertisement
If you want to use gluUnproject, you must make sure you use the proper modelview, projection and viewport information. You want to transform the mouse cursor to world space. Look at the typical matrix concatenation:
<window space> viewport_transform <screen space> projection_matrix <camera space> view_matrix <world space> model_matrix <model space>

You have a point in <window space> and wish to transform it to <world space>. Looking at the required matrices in between you'll need: viewport_transform (if the render part of your window is 594x548 it's ok), projection_matrix (hard to tell from the numbers, but the matrix you print is definitely not made with a glFrustum/gluPerspective/glOrtho call: the end of this Red Book appendix shows what the shape of the projection matrix should be), view_matrix (you need the view part of the matrix, for example made with gluLookAt, but it's identity, so that's definately wrong). You should make sure your projection matrix stack contains the same projection matrix as used when rendering the scene and the modelview matrix should contain the view matrix that was used to render your scene (and no model matrix). If the projection matrix you print is the one you use when rendering, you may want to make sure that it is constructed using a single call to one of these functions: glFrustum, gluPerspective or glOrtho.

Tom

[Edited by - dimebolt on November 3, 2005 7:49:39 AM]
Did you check for errors? Logically to unproject it needs the inverse of the projection matrix, but that particular one doesn't have an inverse. You can tell that easily by the row/column of zeroes at the end.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement