PDA

View Full Version : Please Help.. Picking on 3D Terrain..



cutting_crew
01-21-2004, 12:18 PM
i am in need of dire help and i know i am close to finishing this but i have been looking at the code for too too long now i think. i have a 3D terrain drawn with GL_TRIANGLE_STRIPS and with variating depths, so when you click on it you get a litttle probe where you clicked with the mouse with the corresponding depth value. i do this through the gluUnProject call -- i shall give you the code here:

//give the mouse coordinates and the vector wpos will be set to the world coordinates under the pointer
bool world_coords(GLuint x, GLuint y, GLdouble *wpos)
{
GLdouble model[16], proj[16];
GLint view[4];
GLfloat z;

glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);

y = view[3] - (GLfloat) y - 1;

//if((x >= 0 && x <= 5) && (y >= -1 && y <= 1))
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); //get the pixel(in 2D) w/respect to 3D Coordinates

if(z == 1.0) //depth buffer max depth 1.0 is the default, PIXEL at mouse pointer is below the heightmap plane
return FALSE;

gluUnProject(x, y, z, model, proj, view, wpos, wpos+1, wpos+2);
return TRUE;
}
wpos is updated and the depth is printed out. what i NEED to do for myself if figure out what actual point on the heightmap corresponds to that certain depth. so lets say the terrain has 1,000 points in it. when i click and receive a depth value of the terrain i also want to print out what point it is. so if i get a depth of 456(feet) maybe point # 278 would be the point at or closest to this depth value. how would i go about getting this point? thanks in advance and i hope i was clear and maybe even helped someone with that snippet of code.

rgpc
01-21-2004, 09:53 PM
Google for ray casting

dorbie
01-21-2004, 10:32 PM
The best way (if you insist on reading the depth buffer at a pick point for this) is to take the ray vector in eye space that the xy screen coords correspond to, and adjust the magnitude of that vector to be the same as the depth of the ray to give you the xyz at the end of the ray. This ray can then be transformed back to world space before or after the magnitude adjustment.

One gotcha is that you need to adjust linear z depth to radial depth using the angle between the forward vector and the ray vector.

The other issue is of course adjusting the zbuffer value to be eye space linear z using the near and far frustum values in the usual equation. Search the archives for this, it comes up all the time.

P.S. other poster is right, you should probably just raycast, but that may require additional data structures you don't have, so it's up to you.

[This message has been edited by dorbie (edited 01-21-2004).]