window pos to gl pos

how can i convert my window position(mouse pos from mouse event) to my “world” position.

i googled an example using gluUnProject but it doesn’t return “good” values.

so what i need is to convert my current mouse pos to scene pos so i can cast a ray to pick object under my mouse.

any pointer’s to tutorial or example code would be helpfull.

Saying it doesn’t return good values doesn’t help much - wrong values how? You should be able to project two points and create a ray from them.

Otherwise, try using gluProject as a debugging aid to see what values you should be providing to gluUnProject…

doesn’t return “good” value mean’s that it alway’s return’s

x:0.0
y:0.0
z:0.0

sorry for not mention it.

That sounds mighty odd - are you sure your matricies are setup correctly? And that you’re retreiving your values correctly?

Hows about posting the code? My telepathy isn’t working today :wink:

;D


float camx = posV.x;
float camy = posV.y;
float camz = posV.z;
int viewport[] = new int[4];
double modelMatrix[] = new double[16];
double projMatrix[]=new double[16];
double rRay[]=new double[3];
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelMatrix);
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projMatrix);
glu.gluUnProject(
      x,            //mouse x
      y,            //mouse y
      1.0,            //z but which is the correct value????
      modelMatrix,
      projMatrix,
      viewport,
      rRay,
      rRay,
      rRay);

One thing which is wrong for sure is triple rRay at the end. You should pass 3 single-element arrays instead of passing single 3-element array three times.

Aye, try changing it to this:

glu.gluUnProject( 
 x,  //mouse x 
 y,  //mouse y 
 1.0,  //z but which is the correct value???? 
 modelMatrix, 
 projMatrix, 
 viewport, 
 rRay, 
 rRay+4, 
 rRay+8); 

The z value is going to be app dependant, but you can typically chose two arbitrary values such as 1 and 10 and use the two points to construct your ray.

Edit: Oops, Jogl code, not lwjgl :-[ So you’ll need to make fresh arrays for the extras, not use a pointer offset.

so the last line becomes:


 glu.gluUnProject(
 x,  //mouse x
 y,  //mouse y
 1.0,  //z but which is the correct value????
 modelMatrix,
 projMatrix,
 viewport,
 rRay_x,
 rRay_y,
 rRay_z); 

right?

with rRay_x,y,z being array of size 3.

why’s that? i’m used to have command like this©:


gluUnProject ((GLfloat)x,(GLfloat)y, 1.0, modelMatrix, 
projMatrix, viewport, &rRay[0], rRay[1], &rRay[2]);

so this is becose of pointers?
anything else in jogl has this kind of tricks?

but it still return’s values 0.0,0.0,0.0
and gluUnProject return’s GL_FALSE
so that’s the reason for returning zero’s.
but why it can’t project?

i’m calling this method every time mouse has been clicked
on canvas

and when i check error’s with glError after gluUnProject
it give’s invalid operation.

and when i put DebugGL to canvas it complain’s about every gl.glEnd();
and when i remove them it work’s but when i click on canvas
so that it call’s to method that contains gluUnproject
it give’s me out of memory exception.

this is bit odd why it give’s error from glEnd()?

now i know the problem,it isn’t gluUnProject that is failing,
it’s glGetDoublev and glGetIntegerv.
those return’s full of zero’s array
for GL.GL_MODELVIEW_MATRIX,GL.GL_PROJECTION_MATRIX and GL.GL_VIEWPORT.

now i need to know why those fail’s and how to fix this. :slight_smile:

Post your code for them then :slight_smile:

some init stuff


            double model_view[] = new double[16];;
            double projection[] = new double[16];
            int viewport[] = new int[4];

and the code to get the values


gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, model_view);
int glError = gl.glGetError();
//this gives iinvalid operation string when using gluErrorString
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection);
glError = gl.glGetError();
//this gives invalid operation string when using gluErrorString
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);
glError = gl.glGetError();

I tried the glGet calls exactly as you had them written, and they worked fine - then I noticed you said this. Odds are you’re calling glGet from a different thread that the GL context is currently active in. There is a way to change this (i noticed it in the Animator source) but you’ll probably have nasty race conditions if you start tinkering with that.

My advice would be to set a flag to indicate that your GL thread should project the point, and possibly do the work there, then you avoid most of these issues.

thanks,that was the problem.