View Full Version : GluUnProject & GluProject

01-13-2002, 11:15 PM
I am having a problem getting gluUnProject and glProject to work. The problem is, that the program halts with an ilegal operation everytime I try to make a call to either function. The program code looks something like this:
// Get the transformation matrices
glGetDoublev(GL_MODELVIEW_MATRIX, ModelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, ProjMatrix);
glGetIntegerv(GL_VIEWPORT, Viewport);

// Read back the Z buffer
glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, Zdata);

Now I wish to calculate the world coordinates by unprojecting the camera coordinates(i.e. using the x,y from the screen and the data in the Z buffer):

ans = gluUnProject((double)x, (double)y, (double)eyeCam->Zdata[(x * windowX+y)], eyeCam->ModelMatrix, eyeCam->ProjMatrix, eyeCam->Viewport, Xworld, Yworld, Zworld);

This call terminates the program with an illegal op. I can't see anything wrong with it. Does anyone have an idea? I even tried the call with simple values for x,y and z and the identity matrices for modelview and projection matrices without any other result.

Any help would be appreciated, since I'm quite stuck and can't get on with the program until this is solved.

01-14-2002, 12:55 AM
The last three parameters is supposed to be pointers to a double. Since you don't have the &-sign, I assume they are pointers. But do they actuallt point to a valid memory area?

01-14-2002, 01:09 AM
I figured out the problem, but I have another question. My original code looked like this:

GLdouble *Xworld, *Yworld, *Zworld;

ans = gluUnProject((GLdouble)j, (GLdouble)i, (GLdouble)eyeCam->Zdata[(i * windowX+j)], eyeCam->ModelMatrix, eyeCam->ProjMatrix, eyeCam->Viewport, Xworld, Yworld, Zworld);

Then I tried this instead:

GLdouble Xworld, Yworld, Zworld;

ans = gluUnProject((GLdouble)j, (GLdouble)i, (GLdouble)eyeCam->Zdata[(i * windowX+j)], eyeCam->ModelMatrix, eyeCam->ProjMatrix, eyeCam->Viewport, &Xworld, &Yworld, &Zworld);

The second one works, the first one does not. I don't quite understand it, because to me it seems to be the same. I'm passing the function the addresses of the last three variables in both cases. It should be the same, but obviously it is not. Could someone explain the difference, because there must be a subtle point that I've missed.


[This message has been edited by Anders (edited 01-14-2002).]

01-14-2002, 01:54 AM
The second code defines three variables, and they have a small block reserved in the memory where you store their values. To gluUnProject you pass the address to this memory area.

The first code defines pointers to doubles. They point somewhere in the memory. If they don't point to a valid memory area, your program will crach when you try to dereference the pointers. Along with a pointer, you do not get a memory area where you can store values. You must allocate this memory yourself. If you pass uninitialized pointers to gluUnProject, it tries to store the result in memory that is not valid.

01-14-2002, 02:13 AM
Thanks Bob. That cleared it up.


01-25-2002, 09:46 PM
Hi, Ander
If you come back to read this post. Please e-mail me Kiatt2000@hotmail.com or write your e-mail address in this post. I have many problem to ask you about the point on 3D scene.

thanks a lot,