problem with gluUnProject

Hi,

I tried to convert coordinates from a GLAnimCanvas to the object coordinates when the user clicks with the mouse using gluUnProject. Unfortunately, it seems that the operation failed because it returns GL_FALSE…

Do you have an idea of what could make failed the operation…

Thanks a lot

Mat

More information required, including the values of your unproject point etc. and the code that is causing the trouble…

Originally posted by Robbo:

More information required, including the values of your unproject point etc. and the code that is causing the trouble…

I give you the more information I can:

My GLAnimCanvas (Java) has a size of 900*600 and he is incerted in a JPanel of the same size. The user will click everywhere on this GLAnimCanvas that will generate an event. I pick up from this event the position where the user clicked for example 400-300. Then the Zone method is called it will make the convertion:

/***************************************/
public void mousePressed(MouseEvent e) {

Pos_X_Dep = e.getX();	
Pos_Y_Dep = e.getY();
Canvas.Zone(Pos_X_Dep, Pos_Y_Dep);
	}
}

/***************************************/
public void Zone(int X, int Y){

gl.glGetIntegerv(GL_VIEWPORT, viewport );
gl.glGetDoublev(GL_MODELVIEW_MATRIX,
mvmatrix);
gl.glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);

glu.gluUnProject((double)X,(double)Y,0.0,mvmatrix,projmatrix,viewport,x,y,z));

}
/**************************************/

The method Zone below should make the conversion but it failed.
I initialised the GLAnimCanvas like this:

//
gl.glViewport(0,0,width,height); //width et height size of the GLAnimCanvas which is size of the JPanel
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(Angle_View_Degree, width/height, 0.0f, 10.0f);
gl.glMatrixMode(GL_MODELVIEW);
gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl.glLoadIdentity();
/
/

I draw many rectangles in my GLAnimcanvas they represent a pipeline. For seeing all the pipeline, I calculate an ideal Z position and I use glTranslated for setting the new position. The user can also drag with the mouse to move the scene. I also use glTanslated:
/**************************************/

gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gl.glTranslated(X_Value,Y_Value,Z_Value);
// I change the value of the variables X Y and Z to see the new view of the scene

for (int i=0 ; i < Pipe_Values.size();i++){// For each part of the pipe

pipe = (Pipe)Pipe_Values.get(i);
// Take the pipe
Lenght = (float)pipe.getLenght();
// Take his lenght
Angle = (float)(pipe.getAngle());//*180/Math.PI);
// Take his angle

gl.glRotatef (Angle, 0.0f,0.0f,1.0f); // Rotate the direction of the pipe depending the angle value
gl.glMatrixMode(GL_MODELVIEW);
gl.glBegin(GL_QUADS); // Draw a quad
gl.glColor4f(0.75f,0.0f,0.0f,0.9f); gl.glVertex3f(0.0f,(float)(-Diameter/21.2),0.0f); gl.glVertex3f(Lenght,(float)(-Diameter/21.2),0.0f); gl.glVertex3f(Lenght,(float)(Diameter/21.2),0.0f); gl.glVertex3f(0.0f,(float)(Diameter/21.2),0.0f); gl.glEnd(); // End of the quad

gl.glTranslated Lenght+3*Diameter/2,0.0f,0.0f); // Move the cursor to the next pipe to draw
gl.glRotatef (-Angle,0.0f,0.0f,1.0f); // Reinitialise the angle to 0
gl.glFlush();
}

gl.glLoadIdentity();	

/**************************************/

I hope i have the most complete I could and wish you can help me Robbo…

Thanks in advance
Mat

Hmmm. I’m not 100% sure (never used Java before), but I have a sneaking suspicion your modelview matrix might not be setup correctly when you execute your `zone’ method.

You are being neat and tidy and resetting the matrix to identity after use. I suspect the matrix is identity when you unproject. Print out the values in the modelview matrix, if they are something like

1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1

Then you have an identiy matrix and thus your inverse transform won’t be correct to the view you are rendering into.

Originally posted by Robbo:
[b]
Hmmm. I’m not 100% sure (never used Java before), but I have a sneaking suspicion your modelview matrix might not be setup correctly when you execute your `zone’ method.

You are being neat and tidy and resetting the matrix to identity after use. I suspect the matrix is identity when you unproject. Print out the values in the modelview matrix, if they are something like

1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1

Then you have an identiy matrix and thus your inverse transform won’t be correct to the view you are rendering into.

[/b]

Hi Robbo and thanks for your reply

I tried what you told me and exactly the matrix was the original and it was not the good. Nevertheless it still don’t work… I tried with the ortho mode and then it works more or less correctly but it still failed in gluPerspective mode !!

Mathieu

Hi,
What’s z? In order to use gluUnProject, you need the correct z value. Check out glreadpixels to obtain the correct z value, then call gluUnProject. That should help.

lobstah…

Originally posted by lobstah:
[b]Hi,
What’s z? In order to use gluUnProject, you need the correct z value. Check out glreadpixels to obtain the correct z value, then call gluUnProject. That should help.

lobstah…[/b]

Hi lobstah thanks for your help…

I don’t know if I use the method glReadPixel correctly because it alway’s return the 0 value
Can you tell me more about how using this funtion

Thanks a lot
MAt

I used the glReadPixel like that

gl.glReadPixels(X,Y,0,0,GL_DEPTH_COMPONENT,GL_FLOAT,depthbuffer);

with X Y the coordinates where the user clicked…

Mat

glReadPixels (x, real_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);

Make sure the y component (real_y here) is oriented correctly (Windows and OGL treat the orientation of the y component of the window differently (opposite)) and pass z by reference. Also, the dimensions of the pixel should be set to 1; in your call they’re set to 0.

I think that should do it.

lobstah…

I still have the same result with a value equal to 0…

and with this value my glUnProject still failed…

I don’t know what it could be…and why it works with a ortho projection… If I know how to do a zoom effect in orto mode I could continue with ortho mode…

Mat

When a matrix is passed to gluUnProject, etc., I use global matrices I’ve populated at the time the call is made to the particular one. For example, the projection matrix, once I’ve set my projection, I grab the contents of the project matrix. For the model/view matrix, after I’ve made my call the gluLookAt, I’ll grab the model/view matrix.

Essentially, you want the contents of the matrix just prior to rendering.

This is about all I can think of.

Hope this helps.

lobstah…

[This message has been edited by lobstah (edited 05-06-2002).]