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);
}
}
}