PDA

View Full Version : the resulting object coordinates of the mouse position after gluUnproject



10-26-2005, 05:06 AM
hello,
i want to fix the position of the mouse ,so i use the gluUnproject function.here is my program:
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
glRotatef(20.0,1.0,0.0,0.0);
glRotatef(-45.0,0.0,1.0,0.0);
glPushMatrix();
glColor4f(1.5,0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,2.0,0.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(2.0,0.0,0.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,0.0,2.0);

glEnd();
glPopMatrix();

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, dmatModelview);
glGetDoublev(GL_PROJECTION_MATRIX, dmatProjection);
y = viewport[3] - point1_y;
glReadPixels(point1_x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &fWindowZ);
gluUnProject((GLdouble) point1_x, (GLdouble) y, (GLdouble) fWindowZ,dmatModelview, dmatProjection, viewport,&dWorldX, &dWorldY, &dWorldZ);
printf("(%f, %f, %f)\n",dWorldX,dWorldY,dWorldZ);
glPushMatrix();
glTranslated(dWorldX,dWorldY,dWorldZ);
glutSolidSphere(0.05,20,18);
glPopMatrix();
after running ,a sphere will be drawn rightly at the mouse position.but what i am so confused is that the output coordinates are always positive wherever i press my mouse button.how to explain this? :confused:
thank you very much!