problems with gluUnproject ()?

Hello,

iam using gluUnproject () function to retrieve the object coordinates from the screen coordinates.

when i click on a point (2,2,2) i get the output from the function as(2.218335,2.226619,-0.000000)

when i click on a point (3,3,3) i get the output as (2.218335,2.226619,-0.000000)

when i click on a point (4,4,4) i get the output as
(3.079899,3.084041,-0.000000)

when i click on a point (5,5,5) i get the output as
(3.079899,3.084041,-0.000000)

why is there such a discrepancy in the actual values and the returned values of the function??

is it the nature of the function itself or there is some other problem with my code??

i want to retrieve atleast the x-coordinate more or less exactly??

plz help me ,may be with any other possible solution.

thanking you,
vamsee.

the code is as follows:

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

void display (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glLoadIdentity();
 //   glu.gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0);
 //  glu.gluLookAt(0.0,0.5,0,0,0.0,0.05,0,0,1);
 glOrtho(0.0, 5.0, 0.0, 5.0, 0.0, 5.0);
 //glTranslatef(-5.0f, 0.0f, 0.0f);
 //   gl.glRotatef(rtri,0,6.0f,0);
// glu.gluLookAt(camerax, cameray, cameraz, 0, 0.0, 0.05, 0, 1, 1);
 glPointSize(5);

// for (i = 0, j = i + 1; i < array11.size() - 1; i++, j++)
// {
 //  if (arraya1[j] == arraya1[i] + 1)
  // {
     glBegin(GL_POINTS);
         glVertex3d(1,1,1);
     glVertex3d(2,2,2);
     glVertex3d(3,3,3);
         glVertex3d(4,4,4);
     glVertex3d(5,5,5);
     glEnd();
  // }
// }
  glFlush();

}

void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w ,(GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLfloat) w / (GLfloat) h,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse (int button,int state,int x,int y)
{
GLint viewport[4];
GLdouble mvmatrix[16],projmatrix[16];
GLint realy;
GLdouble wx,wy,wz;
int returnvalue1=0,returnvalue2=0;
// glLoadIdentity();
switch (button)
{
case GLUT_LEFT_BUTTON:
if( state ==GLUT_DOWN)
{
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
realy = viewport[3]- (GLint) y -1;
printf("Coordinates at cursor are (%4d,%4d) \n ",x ,realy);
returnvalue1 = gluUnProject((GLdouble) x,(GLdouble) realy,0.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz);
printf(“returnvalue1 is %d \n”,returnvalue1);
printf(“World coords at z= 0.0 are (%f,%f,%f) \n”,wx,wy,wz);
returnvalue2 = gluUnProject((GLdouble) x,(GLdouble) realy,1.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz);
printf(“return value2 is %d \n”,returnvalue2);
printf(“World coords at z= 1.0 are (%f,%f,%f) \n”,wx,wy,wz);
}

        break;
  case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
        exit(0);
        break;
  default:
        break;
  }

}

  int main(int argc , char** argv)
  {
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
        glutInitWindowSize(500,500);
        glutInitWindowPosition(100,100);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutMouseFunc(mouse);
        glutMainLoop();
        return 0;
  }

I am guessing this is what the problem is…

You shouldn’t specify the z value as 0.0 or 1.1 for the click coordinate. You should use:

// get z
glReadPixels( x, really, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z );

then call gluUnproject with x,really,z

All points need to be converted or you aren’t really asking for what you think you are asking for.