PDA

View Full Version : Mapping mouse points to OpenGL



jowins
12-29-2002, 01:21 AM
Helloo,
I have a 3D object drawn in an OpenGL window.Based on the mouse selection I want to get the exact GL point to that mouse point. For exapmle If i clicked the mouse in a line drwan with GL, I want to check whether the mouse I clicked is over that line I drwan with Open GL.
Thanks and regards
Jowins

Dexter
12-29-2002, 06:18 AM
cast a ray from mouse position and check for intersection with the line or use gl selection buffer

Plato
12-29-2002, 02:25 PM
try code that looks like this:

GLdouble x, y, z;

gluUnProject(m_iMouseX,
m_iMouseY,
1.0,
m_glModelMatrix,
m_glProjectionMatrix,
m_glViewPort,
&x, &y, &z);

Just remember that m_iMouseY is computed by <height of screen> - <mouseclick pixel>.

You also have to have gotten the ModelMatrix, ProjectionMatrix, and ViewPort matrices before calling gluUnProject.

jowins
12-29-2002, 06:56 PM
Hi Plato,
Thanks for the response,I have tried this already but in getting the modelmatrix and projection matrix values for passing it to gluUnproject, it was giving some junk values.
Could you please tell me the reasons its giving like that
i tried this way
GLdouble modelmatrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX,modelmatrix)
GLdouble projmatrix[16];
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix)
Is this correct, but I was getting junk values for that modelmatrix and projmatrix
Thanks and regards
Jowins

Plato
12-31-2002, 05:26 PM
I got the matrices just after I set up my projection matrix and viewport, I use this code:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX,
m_glModelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX,
m_glProjectionMatrix);
glGetIntegerv(GL_VIEWPORT,
m_glViewPort);

glPopMatrix();
glMatrixMode(GL_MODELVIEW);

Which gives me the correct matrices. It's also important to make sure your viewport is actually the size you say it is (this can be a problem if you're displaying the windows bar at the top - it takes up some of your window client area) otherwise you will get values that are slightly off.

I only had to get them once at the start, not every time I do a mouse click. There's probably something I'm doing that's redunant (like switching to PROJECTION mode), but it's only called once so I don't really care...

[This message has been edited by Plato (edited 12-31-2002).]