GLUT, OpenGL & mouse coordinates

Started by
17 comments, last by L. Spiro 11 years, 10 months ago
How the hell (sorry I thought of evrything but I couldn't think of a way) can you make it so that an object, say a triangle allways lies under the mouse pointer? Please explain clearly, because I still haven't got any advanced math lessons.
Advertisement
Use glutPassiveMotionFunc to keep track of mouse movements.
Then you have a couple of choices.
You could just use the callback to set a couple of global variables for mouse position (and call glutPostRedisplay().)
Then in your display function, you'd switch over to orthographic mode, such that each pixel corresponds to one OpenGL unit (gluOrtho2D(0,width,heigh,0).) Then just draw a triangle at the mouse position.
If you want to move an object with perspective projection, you could use gluUnProject (in the passive motion callback) to transform the mouse coordinates to a world coordinate. Then, in your display function, you'd just translate and draw.

Well, that's it. Check out the callback, and think it over. If you need more help, just post here [smile]
Hey, I'm not that dumb that I can't understand global variables and mouse positions... just to say that, because others will spoil their energy on telling simple things.
OK, I got a question, can you change to orthographic view, draw the thing and then change back to perspective view?
And if not, how does that gluUnProject function exactly work?

Thanks for the replies
Quote:Original post by the_cyberlord
OK, I got a question, can you change to orthographic view, draw the thing and then change back to perspective view?

Yes.
  glMatrixMode(GL_PROJECTION);  glPushMatrix();  glLoadIdentity();  gluOrtho2D(0,width-1,height-1,0);  // Here we use orthographic projection  glPopMatrix();  // Back to the projection we were using before  glMatrixMode(GL_MODELVIEW);  // Best switch back to modelview


Quote:And if not, how does that gluUnProject function exactly work?

It is used to go from window coordinates to world coordinates.
       GLint gluUnProject( GLdouble winX,                           GLdouble winY,                           GLdouble winZ,                           const GLdouble *model,                           const GLdouble *proj,                           const GLint *view,                           GLdouble* objX,                           GLdouble* objY,                           GLdouble* objZ )

(winX,winY,winZ) are the window coordinates, where winX=mouseX, winY=1-mouseY, winZ=depth value in the range [0,1] where 0 is the near plane and one the far one.
model and proj should be the viewing and projection transformations used to setup your scene. view is your viewport.
(objX,objY,objZ) will contain the unprojected coordinates.
Here's an example of its use: link
Quote:OK, I got a question, can you change to orthographic view, draw the thing and then change back to perspective view?


Yes, very simply. I do this when drawing my GUI system. Use glPushMatrix() with the projection matrix active, call glIdentity() and glOrtho() to reset and create an Orthogonal view, then glPopMatrix() to restory your original view point.

Generally, it's best to do this at the end of your main render loop.
Are there examples on the perspective >> ortho >> perspective manner?
Why doesn't this work?


double w_mouse_x,w_mouse_y,w_mouse_z;
void MousePassive(int x, int y) {
GLint viewport[4];
GLdouble modelview[16],projection[16];
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
gluUnProject(mouse_x,mouse_y,-5,modelview,projection,viewport,&w_mouse_x,&w_mouse_y,&w_mouse_z); }

object triangle;

GLvoid DrawGLScene()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glLoadIdentity();
glTranslatef(0.0,0.0,-5);
triangle.x = w_mouse_x; triangle.y = w_mouse_y;

triangle.draw();


glPopMatrix();

glFlush();
glutSwapBuffers();
}
gluUnProject(mouse_x,mouse_y,-5

Change the above to
gluUnProject(x,viewport[3]-y,0


it doesn't do anything
Are there tutorials on this?
Try posting the whole source (within [ source ] tags) and I'll take a look. Don't know about any tutorials on this...

This topic is closed to new replies.

Advertisement