gluUnProject returning GL_FALSE

Hi there guys,

As the title indicates i’m getting a false value returned and my object x,y,z values remain unwritten to.

I’ve looked on the net but can’t find anywhere the possible reasons that could cause the function to return false and/or any common errors that tend to occur.

Could someone point me in the right direction or offer advice that would help me in debugging my error?


Weird. Maybe it can’t invert the matrix? Are you sure you pass the correct parameters? Maybe looking at the source of Mesa or the SGI OpenGL sample implementation for the function gluUnProject can point you to the right direction.

[ | vector_math (3d math library) | software renderer ]

Coincidentally, I’ve got that mesa file on my screen:

gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz,
		const GLdouble modelMatrix[16], 
		const GLdouble projMatrix[16],
                const GLint viewport[4],
	        GLdouble *objx, GLdouble *objy, GLdouble *objz)
    double finalMatrix[16];
    double in[4];
    double out[4];

    __gluMultMatricesd(modelMatrix, projMatrix, finalMatrix);
    if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(GL_FALSE);


    /* Map x and y from window coordinates */
    in[0] = (in[0] - viewport[0]) / viewport[2];
    in[1] = (in[1] - viewport[1]) / viewport[3];

    /* Map to range -1 to 1 */
    in[0] = in[0] * 2 - 1;
    in[1] = in[1] * 2 - 1;
    in[2] = in[2] * 2 - 1;

    __gluMultMatrixVecd(finalMatrix, in, out);
    if (out[3] == 0.0) return(GL_FALSE);
    out[0] /= out[3];
    out[1] /= out[3];
    out[2] /= out[3];
    *objx = out[0];
    *objy = out[1];
    *objz = out[2];

Explains itself, I think.

Cheers for the help guys, believe it or not but when i changed my near and far z distances in gluPerspective from 0 and 1 to 1 and 200 it started working. Any idea why? A lesson well worth learning i think.

You can never set the near z distance in gluPerspective to 0!!! This is an error.

[ | vector_math (3d math library) | software renderer ]

Think harder. Setting zNear=0 means, the eye is located in the near clip plane. The plane degenerates to a single point. Obviously the area is 0. How can you map world coords onto a plane, that has no area? You can’t.

lol der, cheers for that, I looked at my other diagram and then it made sense lol.