View Full Version : Using the mouse in OpenGL scene

03-15-2016, 05:15 AM
Is there an easy way to take the mouse 2D pixel coordinates to rotate around and pan in the OpenGL scene? I am using the basic OpenGL with no extentions for my project and I need to be able to manipulate the scene area. Any example code would be helpful.



03-16-2016, 01:44 AM
what is your environment ? Do you use GLUT or do you use opengl within an mfc application ?
Usually with glut you can set a callback function to fetch the mouse position and button states. Within an mfc application you can add message handlers for
mouse events. Take a look at some glut tutorials. Actually capturing mouse positions is decoupled from what you want to do in your scene.
Here are the two functions that capture mouse positions and buttons within the GLUT library.


03-16-2016, 04:21 AM
I do not use GLUT or mfc. I am using only the OPENGL standard extensions on Windows operating system. I have no problem capturing the mouse position in screen coordinates (Pixels). What I need is to convert those pixels into the actual scene coordinates so that I can have the ability to move the camera around within the scene. I have created something basic, but it does not work so well. I was hoping that there was some standard function that handles this since it is a common thing to do in the graphics environment. Why re-invent the wheel???? We are suppose to strengthen ourselves on code reuse, so I am looking for said functions to remove that portions of my task.



03-16-2016, 09:07 AM
In modern OpenGL there is no concept of a 'scene' since you have to send your custom matrices. From mouse position or mouse speed on both axis, it is easy to influence polar coordinates, then convert them to cartesian coordinates and build your view matrix from there.

03-16-2016, 02:07 PM
Hello James,
of course there is some source how to handle that. But there are also differnet ways to do.
Here is a link to a great series of videos that handle moste of modern opengl topics. If you follow the link
you should move to a video that implements a first person style camera class. There is also a link to the complete source

You can use the mouse delta between new x and y position and old x and y position to
determine how fast rotate you camera left and up.