简体   繁体   中英

Opengl rotate an object using the mouse (C++)

I have an object that I want to rotate using the mouse in Opengl (I'm using glut). I'm keeping track of the mouse movement and rotate according to the change in x and y of the mouse. But the problem is that the object doesn't move how I want it to. For instance, when I move in x and then move in y and then move in x again, the object seems to move diagonally, while I want it to move from left to right. I know that this is because the newer rotations get done before the older rotations (resulting in unintuitive rotation) because of how matrix multiplication works.

But I have no idea of how to tackle this issue. One way I thought of is to change the axes about which I rotate according to the current rotation, but I have no idea if this will yield the correct result. Another thing I thought of is to make sure the latest rotation gets done last (but before glulookat translates the scene), but I don't know how to do this. So what would be the best way to solve this?

There are two main approaches you can take.

  • Use quaternions by projecting mouse coordinates onto a sphere (the arcball approach). Make the sphere the size of the window, so you don't have to worry about clicking an object.

  • Rotate about y for horizontal mouse movement. Then, for vertical mouse movement, rotate around the cross product of the view vector and y (be careful if the cross product is zero). This is the approach used by Maya and others for orienting an editing view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM