View Full Version : I need code for moving object with mouse.

12-19-2010, 08:15 PM
I am learning how to move a specific object with mouse in opengl. Please send me a complete code so that I can deeply learn how it works. I want to move a specific object of my choice from different objects. For example if you want to move a piece in ludo game from one location to another location with mouse.


12-20-2010, 03:30 PM
Mouse input is entirely dependent on the language and platform your coding on.

For example in java you would use MouseListener
With c++ and win32 API you would look at WM_KEYDOWN etc.....
You also could just use GLUT.

For moving objects you generally first need to "select" that object and than figure out how you want to move it.
For example it might be simple as select an object than click elsewhere to place that object there.

12-20-2010, 11:07 PM
I am using c++ and windowsXp. I have made object and now I want to drag it by mouse.

12-21-2010, 04:20 PM
Aladin, you can "deeply" learn from one of the plethora of tuts out there. Try NeHe or some other resource.

12-22-2010, 05:41 PM
Your question is quite vague.
First you should decide (as said) in which way you want to get mouse information.
In my opinion the simplest ways to do that is either by using GLUT (many tutorials out there) or by FLTK2 (which is a lean toolkit that also supports windows, buttons and stuff and also an OpenGL window that does the initialization stuff for you).
Then you have to specify how your objects are displayed. If you use orthogonal projection (i.e. 2D look) then it is not very hard to match the mouse position to a certain pixel to find out which object shall be tracked. If you are in a 3D environment where a 2D position could stand for multiple objects in 3D space that overlap due to the screen projection you should first convert the 2D mouse position back to a 3D position. This can for example be done by using gluUnProject where the variable Z value could for example be read from the Z-buffer (not THAT easy anymore).
The next question is how your objects are represented in your program. A fast way for example would be to render all objects in a second buffer where they are encoded by a unique (but object wide constant) color...