Jump to content
  • Advertisement
Sign in to follow this  

3D terrain picking

This topic is 2224 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

Does anybody know how to do 3D terrain picking?

I tried this example but it doesn't work:

CVector3 GetMouse3DCoords()
static POINT IMousePos;
IMousePos.x = g_mousex;
IMousePos.y = g_mousey;
int IViewPort[4];
glGetIntegerv(GL_VIEWPORT, IViewPort);
unsigned int IDepth;
// Get the Z coordinate for the pixel location
glReadPixels( IMousePos.x, (IViewPort[3] - IMousePos.y ), 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &IDepth);
int zdepth;
float IDistTermInv;
glGetIntegerv(GL_DEPTH_BITS, &zdepth);
case 16:
IDistTermInv = (float) 1.5259018967e-5; //65535
IDistTermInv = (float) 2.32830643708e-10; // 4294967295
double IDistance = (float) IDepth * IDistTermInv;

double IModelMatrix[16], IProjMatrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX, IModelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, IProjMatrix);
double IPos[3];
gluUnProject(IMousePos.x, (IViewPort[3] - IMousePos.y), IDistance, IModelMatrix, IProjMatrix, IViewPort, &IPos[0], &IPos[1], &IPos[2]);
CVector3 ITarget = CVector3( IPos[0], IPos[1], IPos[2] );

return ITarget;

Share this post

Link to post
Share on other sites
I use CML get it here....


static inline cml::vector3d GetScreenPosition(cml::vector3f& point)
cml::matrix44d_c projection, modelview, viewport;
GLint vp[4];
double* proj = projection.data();
double* mv = modelview.data();
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetDoublev(GL_MODELVIEW_MATRIX, mv);
glGetIntegerv(GL_VIEWPORT, vp);
point[1] = float(vp[3]) - point[1];
glReadPixels(int(point[0]), int(point[1]), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &point[2]);
cml::matrix_viewport(viewport, double(vp[0]),
0.0, 1.0);
return cml::unproject_point(modelview, projection, viewport, point);

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!