Skip to main content
gamedev.net
🔒 Locked

Please Help...Terrain Rendering....

Started by OpenGL_Guru 7 replies 650+ views
Original Post
OpenGL_Guru
OpenGL_Guru
i have a small terrain rendered using GL_TRIANGLE_STRIPS and i use gluUnProject so i can find out what my 3-D World Coordinates are when i click anywhere on the terrain with the mouse. my terrain is made up of only 300 points.. but what i seem to have trouble with is when i call gluUnProject to get my World coordinates how do i relate that to the particular point at which it was drawn. so lets say i click anywhere on the terrain map and i get a depth value , whatever that is.. i want to be able to say, "ok that depth value corresponds to point #240. have i been working on this too long and overseeing something easy? thanks in advance.
heh
iNsAn1tY
iNsAn1tY
Er, that's not how gluUnProject works. You call gluUnProject twice with your x and y mouse coords, once with a z value of 0.0f, and once with a value of 1.0f. This gives you two 3D points, located at the near and far clipping planes of the view frustum respectively. These two points represent a ray. You take the ray, and do a line/polygon intersection test with each triangle in your terrain. When you get a hit, that's the triangle the user clicked. You can find a tutorial on line/polygon intersection at www.gametutorials.com, in the OpenGL Tutorials section...


Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can't stand 1 bit of competition.


[edited by - iNsAn1tY on January 21, 2004 3:06:08 PM]
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
OpenGL_Guru
OpenGL_Guru
ive already gotten the gluUnProject to work..i click on the terrain and i print out by the mouse the corresponding depth value. i was asking after this call how would i go about determing which actual point on the terrain was clicked.. i didnt really think gluUnProject would give me this answer..
heh
VolkerG
VolkerG
I would take the pick-ray (given by the mouse position as described by iNsAn1tY) and intersect it with the terrain, resulting in a ray/triangle intersection test.
It can be tuned later on.

On the other hand, if you have the depth value, what else do you want exactly?

[edited by - VolkerG on January 21, 2004 3:49:18 PM]
OpenGL_Guru
OpenGL_Guru
this function is called when i pick anywhere on the terrain with the left Mouse Button, for those that may need the code.

/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;
}
heh
OpenGL_Guru
OpenGL_Guru
i need to be more specific than just the triangle, i need an almost close to exact match on the point of the terrain.. but maybe thats what you meant anyway..
heh
jaba
jaba
You can use
glReadPixels(screen.x, viewport[3] - screen.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mouseZ);
to obtain the z coord, and then
gluUnProject(screen.x,viewport[3]- screen.y, mouseZ,modelviewMatriz,projectionMatrix,viewport,&obX,&obY,&obZ);
This is a easier way, but only works when you are drawing filled polygons
jaba
jaba
You can use
glReadPixels(screen.x, viewport[3] - screen.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mouseZ);
to obtain the z coord, and then
gluUnProject(screen.x,viewport[3]- screen.y, mouseZ,modelviewMatriz,projectionMatrix,viewport,&obX,&obY,&obZ);
This is a easier way, but only works when you are drawing filled polygons
iNsAn1tY
iNsAn1tY
quote:
Original post by OpenGL_Guru
this function is called when i pick anywhere on the terrain with the left Mouse Button, for those that may need the code.

[Code which contains glReadPixels]


glReadPixels? You don't need to use that.

quote:
Original post by OpenGL_Guru
i need to be more specific than just the triangle, i need an almost close to exact match on the point of the terrain.. but maybe thats what you meant anyway..

The way I suggested above (with the picking ray) will give you the exact 3D point where the mouse was clicked on the terrain. It will find the triangle, as you state above, but then, with a little extra math, you can find the exact 3D point on the triangle where the mouse was clicked. Post again if you can't find that math in the GameTutorials downloads, and I'll explain it some more...


Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can't stand 1 bit of competition.


[edited by - iNsAn1tY on January 22, 2004 9:39:21 AM]
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.