Jump to content
  • Advertisement
Sign in to follow this  

OpenGL getting the coordinates of the world when picking

This topic is 5045 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

hi, i have a project with matrix based camera i.e. - no glulookat, and i render a city like world with huge amouts of polygons and allow user to pick the individual objects, but i would like to know where did the user actualy click (XYZ) coordinates, and if there is an easy solution for this, basicly all i have is the camera matrix and the hit distance that i get from opengl picking rutines but i need the coordinates of the hit :(

Share this post

Link to post
Share on other sites
Hi there, you can try to read the pixels.
This is how I do it.

Vector3 WorldCoords;

void GetGLPos(int x, int y)
static GLint viewport[4];
static GLdouble modelview[16];
static GLdouble projection[16];
static GLfloat winX, winY, winZ;

double posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (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);

WorldCoords.x = (float)posX;
WorldCoords.y = (float)posY;
WorldCoords.z = (float)posZ;

the GetGLPos function takes the mouse x,y 2D coords, then return the 3D coords xPos, yPoz, zPos to the WorldCoords vector.

hope it helps.

Share this post

Link to post
Share on other sites
this seem to do something, i used this pascal equalent:

function TGLView.getdepthhitat(X, Y: LongWord): TVector3f;
viewport: TVector4i;
modelview: TMatrix4d;
projection: TMatrix4d;

winX, winY, winZ: GLfloat;
posX, posY, posZ: double;

glGetDoublev( GL_MODELVIEW_MATRIX, @modelview );
glGetDoublev( GL_PROJECTION_MATRIX, @projection );
glGetIntegerv( GL_VIEWPORT, @viewport );

winX:= x;
winY:= y;

glReadPixels( X, Y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, @winZ );
gluUnProject( winX, winY, winZ, @modelview, @projection, @viewport, posX, posY, posZ);

result[0]:= posX;
result[1]:= posY;
result[2]:= posZ;


but the coordinates are all wrong, sometimes it looks like that they are
relative to something else?

Share this post

Link to post
Share on other sites
How do you call the function?

for example how to get the coords on an terrain, you do something like this.


GetGLPos(cursor.x, cursor.y);


Now you get the right coords for the terrain, so you dont call the GetGLpos
before the render.

Share this post

Link to post
Share on other sites
why do i have to pop the matrixes?
my state of renderer when i call the func is in the
modelview and all stacks are popped back to zero already :/

Share this post

Link to post
Share on other sites
this is what i do on my terrain when i want to get the world coordinates. first you have to pass in the mouse position before converting such as:

void myGlutMouse(int button, int button_state, int x, int y)
if ( button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN )
if(current_terrain->world_coords(x, y, world_pick))
current_terrain->get_world(world_pick[0], world_pick[1], world_pick[2]);


Now in my class those two functions:

bool myClass::world_coords(GLuint x, GLuint y, GLdouble *wpos)
GLdouble model[16], proj[16];
GLint view[4];
GLfloat z;
glClear(GL_DEPTH_BUFFER_BIT); //clear the depth buffer for reading

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

y = view[3] - (GLfloat) 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(terrain)
return FALSE;

gluUnProject(x, y, z, model, proj, view, wpos, wpos+1, wpos+2);

return TRUE;


if you clicked on the terrain successfully the gluUnProject returns to you the XYZ world coords back to main if you look carefully. the get_world function from main simply assigns the XYZ world coordinates to a private array in my class and is used for things like drawing a sphere at the place i picked on my terrain or simply some information from the data that is displayed from the screen. hope this helps!

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!