Captures the implementation of openGL

I can already find the world coordinates of the place I clicked on and also checks this with the depth buffer. To do this, use 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);

where x_cursor and y_cursor are my cursor coordinates.

So far so good, and when you print x, y, z they seem good!

But now ... After parsing my files containing all the polygons / triangles. I put everything into OpenGL DISPLAY lists. So my program basically just calls lists and translates / rotates them. Each object has a unique name. So, all I hold is lists, I don't save points / triangles of every object

My question is:

So, I have my world coordinates where I clicked, but how can I figure out which object corresponds to this position !?

+5
4

2x overdraw:

1)

2) ( ), → .

3) , .

4) .

+4

OpenGL Picking Tutorial. . , , , , (, , ). , .

void list_hits(GLint hits, GLuint *names)
{
    int i; int bestmatch; GLuint bestdistance;
    if (hits < 1) {
      return;
    }
    bestmatch = 0;
    bestdistance = names[bestmatch*4+1]; // min distance to object.
    for (i = 1; i < hits; i++)
    {
        distance = names[i*4+1];
        if (distance < bestdistance) {
           bestmatch = i; bestdistance = distance;
        }
    }
    printf("Hit: %d.\n", bestmatch);
 }

. , .

0

.

xyz .

, :

enter image description here

, unproject, unproject ( )

, , raytracer , . ( , ).

0

All Articles