Hello Everyone,
I have a problem using glReadPixels function. Let me post little code segment:
if (state == GLUT_DOWN)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
headSkinPatch->renderPatch();
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLfloat winX, winY, winZ;
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
GLdouble posX, posY, posZ;
gluUnProject(winX,winY,winZ, modelview, projection, viewport, &posX, &posY, &posZ);
As you see in the code piece, after issuing glClear function, I only draw the object that I want to check with glReadPixels. After the checking is performed, I clear the buffer again and do the other drawing stuff. So on the screen, “the other drawing stuff” is shown. In this code piece, I expect to capture the coordinate information of the single drawn object, however I could not get the correct information.
What is wrong with my thinking? I would be grateful if you show me a solution…
Thanks in advance…