Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got stuck implementing openGL picking

I'm already able to find the world coordinates of the place i clicked, and it also checks this with the depth buffer. For this is used the following code :

GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX,winY;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);

// obtain the Z position (not world coordinates but in range 0 - 1)
GLfloat z_cursor;
winX = (float)x_cursor;
winY = (float)viewport[3]-(float)y_cursor;
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z_cursor);

// obtain the world coordinates
GLdouble x, y, z;
gluUnProject(winX, winY, z_cursor, modelview, projection, viewport, &x, &y, &z);

with x_cursor and y_cursor being my cursor coordinates.

So far so good and when printing x,y,z they seem to be good !

But now... After parsing my files containing all the polygons/triangles. I put everyting in openGL DISPLAY Lists. So my program basically just calls the lists, and translates/rotates them. Every object als has a unique name. So all i keep are the lists, I don't keep the points/triangles of every object

My Question :

So i have my world coordinates of where i clicked, but how can I figure out which object matches that position !?

like image 592
Joseph Avatar asked Aug 11 '11 15:08

Joseph


2 Answers

If you just want the object ID and are willing to tolerate 2x overdraw:

1) Clear color and depth buffers

2) Draw all objects with different solid colors (disable lighting and texturing), maintaining a color -> object ID map.

3) Read color buffer at mouse position, note color and look up in color map.

4) Clear color and depth buffers and re-render scene as normal.

like image 196
genpfault Avatar answered Sep 24 '22 16:09

genpfault


I think you might be interested in: http://www.gpwiki.org/index.php/OpenGL:Tutorials:Picking

like image 37
the_source Avatar answered Sep 22 '22 16:09

the_source