PDA

View Full Version : How to get the Opengl X & Y points in a mouse click ? not screen X & Y points



Jerry
12-31-2001, 08:48 PM
Hi...

I created an object by the combination of large number of triangles using glBegin function. My question is how to get the x & y(z if possible)OpenGL coorinates of the mouse clicked point to put a color in the clicked point? not the X & Y screen points in the mouse click .Is it possible ?
Pls help me if any one knows.

Thanks in advance for Ur replay

Regards,
Jerry

SirKnight
12-31-2001, 09:12 PM
I think what you need is to use the selection buffer. Its explained in the opengl superbible and the redbook. Im sure there are tutorials on the subject as well.

-SirKnight

[This message has been edited by SirKnight (edited 12-31-2001).]

okapota
01-01-2002, 10:19 AM
i would recommend building a ray from the camera position along the clicked point, and cutting the ray with the triangles.
building the ray is easy, use gluUnProject();

jcgp
01-01-2002, 01:23 PM
gamgi_float z_2d;
gamgi_int viewport[4];
gamgi_double projection[16];
gamgi_double modelview[16];

glReadPixels (x_2d, y_2d, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z_2d);
if (z_2d > 0.9) z_2d = 0.9;

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

gluUnProject ((GLdouble) x_2d, (GLdouble) y_2d, (GLdouble) z_2d,
modelview, projection, viewport, x, y, z);
}