Picking with gluUnProject() always returns zero

Started by
5 comments, last by Dustin Hopper 11 years, 11 months ago
I've found several posts about this problem with google, but nothing has solved the issue for me.

gluUnProject will always return zero and posX/Y/Z will also always be zero. I'm calling gluUnProject with some test data instead of a mouse position for the sake of a test case that I can share and is easy to interpret.


void Renderer::DrawGLScene( void ) {
glLoadIdentity();

GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX=0, winY=0, winZ=0;
GLdouble posX=0, posY=0, posZ=0;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

gluUnProject( 123, viewport[3] - 45, 0, modelview, projection, viewport, &posX, &posY, &posZ);

return;
}


this is called one time at program start

void Renderer::ResizeGLScene( const int width, const int height) {
if ( height==0 )
res.y=1;
else
res.y=height;

if ( width==0 )
res.x=1;
else
res.x=width;

game->sdl.SetVideoMode( width, height, fullscreen, 32, 0 );

glViewport(0,0,res.x,res.y);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(90.0f,(float)res.x/res.y,0.0f,600.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

SDL_PumpEvents();
}

Advertisement
Your call to gluPerspective is not correct. You cannot give znear a value of 0.

http://www.opengl.or...depthbuffer.htm

EDIT: mathematical explanation.

gluPerspective constructs the projection matrix as such: (a = width / height)

e 0 0 0
0 ea 0 0
0 0 -(f+n)/(f-n) 2fn/(f-n)
0 0 -1 0

resulting in the depth value being computed as: (in HCC)

z' = x*0 + y*0 - z(600)/(600) + w*0/(600) = -z
w' = x*0 + y*0 + z*-1 + w * 0 = -z


During perspective division, the value for the depth to be stored in the depth buffer (in NDC) would be computed as:

Z = z / w = -z / -z = 1

which, in this case, since n was set to 0, would always be 1, and therefore outside the range of depth and never rendered, or never stored in the depth buffer. All calls to gluUnProject would then return 0.
[size=2]hopper.dustin@gmail.com
thank you. I set it to 1 and I'm getting some values now from gluUnProject(). Not sure what it means, though.

I know various vector math and collision stuff, but Matrices and whatnot elude me. I might as well be reading greek when it comes to pages like this: http://www.opengl.or...Perspective.xml
It's no problem at all, haha. I still have problems with math from time to time.

Side note: you may want to check this out.

http://nehe.gamedev.net/article/using_gluunproject/16013/

And, I believe, without an object drawn at the location of the cursor when you click, gluUnProject wouldn't return anything helpful.
[size=2]hopper.dustin@gmail.com
thank you for all the info. I read the nehe thing about this a few times, hacked through the code and I think I understand what it does basically. I'm just trying to get a vector straight "into the screen" so that I can do my own collision checking. for example, being ablt to raise/lower the ground tiles, moving things around, etc, like you see in this screenshot:

20120524.jpg
It works SPLENDIDLY!

I draw a line to see if its clicking in the right spot. sure enough, I click, and then I pan the screen over *jsut a tad* and bam, there's my line. for a second I couldnt figure out why I had to pan over, then I realized it goes *straight into the screen* and a point technically has no width. But the start and end points are right under my mouse and right in the correct spot given any rotation or transformation I do. wahoo!

smile.png
Glad I helped. :)

Good luck.
[size=2]hopper.dustin@gmail.com

This topic is closed to new replies.

Advertisement