Mouse Click to 3d

Hi.
I need to get a mouse click position to 3d coordinates.
I have the following code for my mouseclick:


void mouseClick(int button, int state, int x, int y)
{
  if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  {
     point[1].x = x;
     point[1].y = y;
     point[1].z = 0;
  }
  
}

but its only doing it on the top right quadrant because (0,0) is centre of screen.

Is there a way to convert 2d click to 3d coordinate. I just need the x,y ( i dont mind the dept(z)).

Thanks

Take your window-space “mouse click” position at the near or far clip plane, and transform it back into whatever space you’re interested in (OBJECT, WORLD, or EYE SPACE). This is one use for gluUnProject, though you could do it yourself with the transform matrices. A few related links: link, link. Also search the OpenGL forums for references to gluUnProject: link

Is the projection you’re using perspective or orthographic? If perspective, then note that your mouse click doesn’t correspond to a specific X,Y in OBJECT, WORLD, or EYE coordinates without presuming a depth value (to make it a specific point in space)? But if orthographic, then it “could” correspond to a specific X,Y in your target space “if” your VIEWING transform has Z in your target space pointing into/out of the screen.