• Advertisement
Sign in to follow this  

Mouse, 3d terrain, coordinates, OpenGL

This topic is 3665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to translate the mouse coordinates (x,y of the window) to x,y,z coordinates of my world. I have a terrain that can be scrolled and rotated but I haven't been able to select a point on this map with a mouse. The terrain is not flat but there is only one z for each (x,y) pair. I managed to calculate a solution but it is not accurate (it misses the selected spot a bit). Perheaps more information about the viewing frustum would help me. Knowing the parameters of the frustum: glFrustum ( GLdouble left , GLdouble right , GLdouble bottom , GLdouble top , GLdouble zNear , GLdouble zFar ) how can I calculate the angle that frustum walls make with the z axis? (I need it for my algorithm) ___________ far \0000|0000/ 0\___|___/ near 00\00|00/ 000\0|a/ I need this angle. 0000\|/ 00000V Perheaps what I'm doing is not necessary because there exists a special opengl function or a library I do not know about... If so tell me.

Share this post

Link to post
Share on other sites
As I suspected. It's a basic problem. I can use gluUnProject. I've found the answer here http://students.iiit.ac.in/~vkrishna/data/unproj.html

In case it dissappears:

Using gluUnProject (OpenGL, gluUnProject, glut)

I have been using OpenGL since a long time and a fundamental problem many would like is how to find out where the user clicked in the 3D-world given the mouse co-ordinates. OpenGL and GLU functions can be used to effectively solve this problem. Since i could not find any good tutorials regarding this i thought i'd put up one by myself.

There is a simple function that glu provides. It's called gluUnProject, this function takes in 6 arguments.. which include the window x,y,z co-ordinates, modelview matrix, projection matrix, viewport and X,Y,Z for storing the world co-ordinates of that particular point. Dont' get worried about window z co-ordinate which is given to this function. This value is obtained by using glReadPixels( x, viewport[3]-y, GL_DEPTH_COMPONENT, GL_FLOAT, &z ); We need to use viewport[3]-y (this is equivalent to windowheight-y where x,y are the mouse location co-ordinates) instead of just y because the glut window has origin at top left corner where as OpenGL (glReadPixels) has the origin at bottom left corner.

Once you get the z value of the window (using glReadPixels), we can pass the window x,y,z values to the gluUnProject and get the world co-ordinates. In the example here a red cube is moved to the location where the user clicks on the green plane.

double objx, objy, objz;
//The drawing function glutDisplayFunc( )
void display( )
//Clearing the screen with black and clearing the depth buffer

//The green polygon
glBegin( GL_POLYGON );
glColor3f( 0, 1, 0 );
glVertex3f( -100, 0, 100 );
glVertex3f( -100, 0, -100 );
glVertex3f( 100, 0, -100 );
glVertex3f( 100, 0, 100 );
glEnd( );

//store the current transformation matrix
//and translate the cube to the world location
glPushMatrix( );
glColor3f( 1, 0, 0 );
glTranslatef( objx, objy, objz );
glutSolidCube( 10 );
glPopMatrix( );

glFlush( );
glutSwapBuffers( );

//The function that handles the mouse clicks glutMouseFunc( )
void mouse( int button, int state, int x, int y )
double modelview[16], projection[16];
int viewport[4];
float z;
//get the projection matrix
glGetDoublev( GL_PROJECTION_MATRIX, projection );
//get the modelview matrix
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
//get the viewport
glGetIntegerv( GL_VIEWPORT, viewport );

//Read the window z co-ordinate
//(the z value on that point in unit cube)
glReadPixels( x, viewport[3]-y, 1, 1,

//Unproject the window co-ordinates to
//find the world co-ordinates.
gluUnProject( x, viewport[3]-y, z, modelview,
projection, viewport, &objx, &objy, &objz );

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement