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.