• Create Account

## Picking with gluUnProject() always returns zero

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1caibbor  Members

Posted 24 May 2012 - 07:28 PM

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 ) {

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

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

glMatrixMode(GL_MODELVIEW);

SDL_PumpEvents();
}



Edited by caibbor, 24 May 2012 - 07:34 PM.

### #2trotlinebeercan  Members

Posted 24 May 2012 - 08:38 PM

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.

Edited by trotlinebeercan, 24 May 2012 - 09:00 PM.

hopper.dustin@gmail.com

### #3caibbor  Members

Posted 24 May 2012 - 08:49 PM

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

Edited by caibbor, 24 May 2012 - 08:50 PM.

### #4trotlinebeercan  Members

Posted 24 May 2012 - 09:04 PM

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.
hopper.dustin@gmail.com

### #5caibbor  Members

Posted 24 May 2012 - 09:17 PM

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:

Edited by caibbor, 24 May 2012 - 09:17 PM.

### #6caibbor  Members

Posted 24 May 2012 - 09:47 PM

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!

Edited by caibbor, 24 May 2012 - 10:10 PM.

### #7trotlinebeercan  Members

Posted 24 May 2012 - 11:07 PM