PDA

View Full Version : How to code mouse controlled rotation?

Ren
08-15-2007, 07:32 AM
Hi, I wrote a program which loads a obj mesh file and render it. Now I wanted to be able to use mouse to rotate the model, in all directions. Exactly like in 3D modelling software Maya and Blender.

I think I need three values to store rotation in all direction. but how do I relate mouse moves to the rotatation degree change? any little demo examples would be good as well

holdeWaldfee
08-15-2007, 07:52 AM
http://en.wikipedia.org/wiki/Rotation_matrix

Virtually every useful 3D application is using rotation - right?
So why do you think that this question is related to advanced OpenGL coding?!

Ren
08-15-2007, 10:37 PM
I do understand the rotation matrix, however, what I wanted is how to use mouse to control rotation. E.g. How much the object rotate horizontally when mouse moved a couple of pixels?

ZbuffeR
08-16-2007, 12:56 AM
Even better :p
Try half a degree for one pixel.

iznogoud
08-20-2007, 09:08 PM
(first post in a long time for me)

I think it can be considered an advanced question as
I understand it. And, it is more of a widget+GL type
question than just GL.

WHat I do is use the Xlib (on unix and X11) to trap
mouse motion and key press _and_ release events. On
a mouse-press event (and when my program is at a
particular state) the rotation is activated. Then,
pointer motion is trapped and for every pixel of
motion I rotate by 1 degree and redraw as often as
the event loop cycles. Upon release of the button,
I get out of the rotating motion.

A tricky part is to get quaternions working for
rotations and not Euler angles. You integrate the
quaternion to get the rotation and then revert to
direction cosines so that you can construct the
rotation matrix directly (or its transpose, I do
not remember).

Do google searches for all words you do not quite
understand. You may be able to find example code
online as well, but what I described is quite
basic and will work if implimented correctly. Good
luck.

iznogoud
08-20-2007, 09:09 PM
One more thing. If you are using some Windows
machine or whatever, use SDL to handle the events
that you need to trap. I prefer to use my own event
loop so I do not use SDL, but it is good for the
person who asks questions like yours.

game_cy
08-21-2007, 02:32 PM
If you are not using your mouse for anything else, you might find it cool to use the two mouse buttons as follows:

left mouse button pressed: move lookat point towards mouse cursor, ie turn left , right or move forward, backward

right mouse button down: rotate camera pos around lookat point.

This way you get to navigate around your scene with the LMB and rotate around the object with the RMB.