Jump to content
  • Advertisement
Sign in to follow this  
the_cyberlord

OpenGL GLUT, OpenGL & mouse coordinates

This topic is 2247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
Try posting the whole source (within [ source ] tags) and I'll take a look. Don't know about any tutorials on this...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!