Jump to content
  • Advertisement
Sign in to follow this  
stecooper123

OpenGL gluUnProject and terrain generation

This topic is 3443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!