3D Terrain coords selection.
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.
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}
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?
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?
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.
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.
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 :)
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 :)
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement