gluUnProject and terrain generation

Started by
5 comments, last by stecooper123 15 years, 1 month ago
Hi i have created a terrain generation program which reqires mouse picking. I have followed the tutorial on NeHe http://nehe.gamedev.net/data/articles/article.asp?article=13 GLint viewport[4]; GLdouble modelview[16]; GLdouble projection[16]; GLfloat winX, winY, winZ; GLdouble posX, posY, posZ; glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); glGetDoublev( GL_PROJECTION_MATRIX, projection ); glGetIntegerv( GL_VIEWPORT, viewport ); winX = (float)x; winY = (float)viewport[3] - (float)y; glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ ); gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); return CVector3(posX, posY, posZ); I am not currently at my pc but i have made sure that my code is the same as the one above. There seems to be a problem though with the winZ part, it only returns a value from 0 to 1 and i have a z screen size of 40000 and i am scaling the z amount relative to the value returned. the problem i am having is that the values being returned to cVector seem totally random values which have no relevence to what i am pointing to. Is there anthing extra which needs to be done. i was talking to a lecturer who mentioned something about writing to the depth buffer but i am not sure what that is. Could any one who has done mouse picking for terrain in opengl lend me a hand with this as it is the last part of my project to be completed. Thank you i will post my actual code with more detail on monday when i get back.
Advertisement
It's normal for winZ to be 0.0 to 1.0. It is a normalized float value.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by V-man
It's normal for winZ to be 0.0 to 1.0. It is a normalized float value.


when coming out of glReadPixels winZ should be between 0 - 1 this is the depth of the pixel in 0 - 1 range, so at you near plane the value is 0 and at the far plane the value is 1.

This is because when projection takes place using homogeneous coordinates the depth of the pixel is kept after perspective divide takes place...and is useful in many cases ie when using depth testing.

gluUnProject reverses the projection process, so from the pixel position on the screen (x,y,z) and the modelview projection matrices, the original x,y,z world space coordinate of the pixel can be obtained. gluUnProject should return the correct original x,y,z coordinate of the pixel.

you don't have to scale the winz value by 40000, as the un-project process does this for you. In this case pos x,y and z should be correct..

I think i am going about this the wrong way. i though that using the above method would return the xyz position of the object that you are pointing at. it seems though that the values i am getting returned to me are relative to the center of the screen and not world space. i have taken out the scale now and i constantly get a z value of -9986.5 no matter where i point.
Hi i realised that this function is only part of what i need. and that this code needs to be used in conjunction with a raycast. Hope it works.

Thank you
Quote:Original post by stecooper123
Hi i realised that this function is only part of what i need. and that this code needs to be used in conjunction with a raycast. Hope it works.

Thank you


this shouldn't be the case, passing in window x,y,z to un-project should return the correct value... all i can think of is that your trying to get the world space coordinate at the wrong time, you must perform glReadPixels after you have rendered to the screen and before you swap buffers.

but you definitely don't have to do a raycast.
BlackSeeds i just want to say . . . .. YOU ARE A GOD

i was doing it in the render section of my code and the second i moved it into update it worked. thank you so much. I was just about to start messing with raycasts.

Thank you

This topic is closed to new replies.

Advertisement