glreadPixels & gluUnProject problem

SYMPTOM:
I want pick some points by means of glreadPixels and gluUnProject. Wherever I put down left button of mouse, the value of pixel returned by glreadPixels is always 1. With the same place of mouse the value of Y and Z returned by gluUnProject is becoming bigger and bigger every time I call the function of gluUnProject when I push down mouse.
PRESCRIPTION?
Codes as follows:
void CProjectView::PickColorPoints(CPoint point)
{
int mouse_x;
int mouse_y;
CString m_str;
double objX,objY,objZ;
static count=0;
mouse_x=point.x;
mouse_y=point.y;

GLdouble pModelmatrix[16],projectionmatrix[16];
float fWindowZ,distance;
int workproject;
GLint viewport[4];
CRect rect;

glTranslated(SYSVIEWX, SYSVIEWY, SYSVIEWZ);
glRotatef(viewAngle_Z, 0.0, 0.0, 1.0);
glRotatef(viewAngle_Y, 0.0, 1.0, 0.0);
//////////////////////////
glGetDoublev(GL_MODELVIEW_MATRIX,pModelmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projectionmatrix);
glGetIntegerv(GL_VIEWPORT,viewport);
glReadBuffer(GL_BACK);

GetClientRect(&rect);
glReadPixels((GLdouble)mouse_x, (GLdouble) (rect.Height()-mouse_y-1),1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &fWindowZ);
workproject=gluUnProject ((GLdouble)mouse_x, (GLdouble)(rect.Height()-mouse_y-1), (GLdouble)fWindowZ, pModelmatrix,projectionmatrix,viewport, &objX, &objY, &objZ);
if(workproject==GL_FALSE)
return;
for(int i=0;i<nProDataLength/6;i++)
{
glPointSize(2.0);
glBegin(GL_POINTS);
glVertex3f((float)(pProcess[i*6]-pProcess[0]),(float)(pProcess[i*6+1]-pProcess[1]),(float)(pProcess[i*6+2]-pProcess[2]));
glEnd();

  distance=sqrt((pProcess[i*6]-pProcess[0]-objX)*(pProcess[i*6]-pProcess[0]-objX)+(pProcess[i*6+1]-pProcess[1]-objY)*(pProcess[i*6+1]-pProcess[1]-objY)+	(pProcess[i*6+2]-pProcess[2]-objZ)*(pProcess[i*6+2]-pProcess[2]-objZ));	  	 

if(distance<5.0)
{
m_str.Format("%d£¡
",i);
AfxMessageBox(m_str);
//TRACE(m_str);
}
}

}

Looks like you are doing a translation followed by a couple of rotations but there is no push and pop.

Why is there a recent surge in questions about gluUnproject/gluProject?

do you actually glPushMatrix() before/glPopMatrix() after when you call that PickColorPoints function, 'coz if not – the transformation and rotation then accumulate the values, so the problem could be with that as well…

=)

Yeah,I forget glPushMatrix and glPopMatrix
between rotation and translation.But after
I add it, the value of pixel returned
by glreadPixels is always 1 and
the value of Y and Z returned by
gluUnProject is same according
to the same mouse place but not
correct.Thanks for reply!

I have used this function and it almost gives correct results. The results depend on the depth buffer bits of course. I had 24 bpp for the depth.

However, using a intersection test with your geometry is more reliable. Just pretend a ray shoots out from where you click the mouse.

yeah, actually i didn’t get what exactly for do you use this schem? If you’re putting differently painted objects into a back buffer to perform object picking/selection, then why just don’t use a selection buffer instead – that would be a much more reliable way to perform these tasks, avoiding any related PITA with reading the color buffer involved.