PDA

View Full Version : gluUnProject returning GL_FALSE



Scribe
11-30-2007, 01:08 PM
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?

Thankyou.

Trenki
11-30-2007, 02:04 PM
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.

[ www.trenki.net (http://www.trenki.net) | vector_math (3d math library) (http://www.trenki.net/content/view/16/36/) | software renderer (http://www.trenki.net/content/view/18/38/) ]

CatDog
11-30-2007, 05:06 PM
Coincidentally, I've got that mesa file on my screen:


GLint GLAPIENTRY
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);

in[0]=winx;
in[1]=winy;
in[2]=winz;
in[3]=1.0;

/* 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];
return(GL_TRUE);
}
Explains itself, I think.

Scribe
11-30-2007, 07:36 PM
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.

Trenki
12-01-2007, 02:46 AM
You can never set the near z distance in gluPerspective to 0!!! This is an error.

[ www.trenki.net (http://www.trenki.net) | vector_math (3d math library) (http://www.trenki.net/content/view/16/36/) | software renderer (http://www.trenki.net/content/view/18/38/) ]

CatDog
12-01-2007, 05:53 AM
Any idea why? A lesson well worth learning i think.

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.

Scribe
12-01-2007, 09:48 AM
lol der, cheers for that, I looked at my other diagram and then it made sense lol.