3D Terrain coords selection.

Started by
7 comments, last by X5-Programmer 19 years, 5 months ago
Hi there. I have been trying to make an selection on an 3Dterrain in opengl using the mouse to receve the x,z coords on the world map, but I havent got it to work, I need this for moveing objects to the x,z coords I revc from the map using the mouse like warcraft game. I realy need help with this I have been trying to get this to work in a long time now and realy what to get it to work. tutorials / examples / coments are welcome. hope anyone can point me somewere with my problem. thx.
-[ thx ]-
Advertisement
Here's a complete function to take care of what you need, simply pass in your mouse x and y coordinates and the result is stored in posX, posY, and posZ.

void GetGLPos(int x, int y){	static GLint viewport[4];	static GLdouble modelview[16];	static GLdouble projection[16];	static GLfloat winX, winY, winZ;	glGetDoublev( GL_MODELVIEW_MATRIX, modelview );	glGetDoublev( GL_PROJECTION_MATRIX, projection );	glGetIntegerv( GL_VIEWPORT, viewport );	winX = (float)x;					// Holds The Mouse X Coordinate	winY = (float)viewport[3] - (float)y;	glReadPixels(int(winX), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );	gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);     	// now posX, posY, and posZ contain the actual 3D world coordinates of the mouse}
Author Freeworld3Dhttp://www.freeworld3d.org
oconnellseanm:

Thanks.
-[ thx ]-
oconnellseanm:

one problem I have with this is I get all the coords on the world how do I do if I only whant coords from terrain plane?
-[ thx ]-
I do something like this now..

glLoadIdentity();

GetGLPos(m_cursor.x, m_cursor.y);
m_Terrain.render();

But I get like this values:

X = 1145235253.0f
Z = 121905012.0f

cant I just get the from like say 0, 0 on the terrain then incress the longer up I hold the mouse cursor or something like that..

If you have time write me an example and explain.

thx.
-[ thx ]-
I think that you should render your terrain before calling GetGLPos(). It worked for me that way - thanks for the great code oconnellseanm.
Allrigt.. Now I do something like this.

CVector player;

m_terrain.render();
GetGLPos(m_cursor.x, m_cursor.y);

glPushMatrix();
glTranslatef(player.x, m_terrain.GetHeight(player.x, player.z), player.z);

DrawCube(10.0f, 10.0f, 10.0f);
glPopMatrix();

GetGLPos return players pos.

but It doesnt draw on the posision my cursor is on the Terrain.

help plz :)
-[ thx ]-
Try printing out the returned world coordinates to the screen or print them to your window's caption. Then you can see exactly what they are.....try positioning your cursor right at the beginning corner of the terrain where you know exactly what the coords are.
Author Freeworld3Dhttp://www.freeworld3d.org
Yes I have done that but it doesnt print out the right coords. dont know why, what can be wrong?
-[ thx ]-

This topic is closed to new replies.

Advertisement