Sign in to follow this  
polyfrag

3D terrain picking

Recommended Posts

Does anybody know how to do 3D terrain picking?

I tried this example but it doesn't work:

[code]
CVector3 GetMouse3DCoords()
{
static POINT IMousePos;
//GetCursorPos(&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);
switch(zdepth)
{
case 16:
IDistTermInv = (float) 1.5259018967e-5; //65535
break;
default:
IDistTermInv = (float) 2.32830643708e-10; // 4294967295
break;
}
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;
}[/code]

Share this post


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

http://cmldev.net/

[code]
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]),
double(vp[2]),
double(vp[1]),
double(vp[3]),
cml::z_clip_neg_one,
0.0, 1.0);
return cml::unproject_point(modelview, projection, viewport, point);
}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this