Rotating OpenGL scene in 2 axes

I have a scene which contains objects located anywhere in space and I'm making a trackball-like interface.

I'd like to make it so that I can move 2 separate sliders to rotate it in x and y axes respectively:

glRotatef(drawRotateY,0.0,1.0f,0);
glRotatef(drawRotateX,1.0f,0.0,0.0);
//draw stuff in space

However, the above code won't work because X rotation will then be dependent on the Y rotation.

How can I achieve this without using gluLookAt()?

Edit: I'd like to say that my implementation is even simpler than a trackball interface. Basically, if the x slider value is 80 and y slider is 60, rotate vertically 80 degrees and horizontally 60 degrees. I just need to make them independent of each other!


This code should get you started: http://www.cse.ohio-state.edu/~crawfis/Graphics/VirtualTrackball.html

It explains how to implement a virtual trackball using the current mouse position in the GL window.


你可以使用这样的东西:

Vector v = new Vector(drawRotateX, drawRotateY, 0);
float length = v.length();
v.normalize();
glRotatef(length, v.x, v.y, v.z);

When you say rotate vertically and horizontally, do you mean like an anti-aircraft gun - rotate around the vertical Z axis, to face in a particular compass heading (yaw) and then rotate to a particular elevation (pitch)?

If this is the case, then you just need to do your two rotations in the right order, and all will be well. In this example, you must do the 'pitch' rotation first, and then the 'yaw' rotation. All will work out fine.

If you mean something more complicated (eg. like the 'Cobra' spaceship in Elite) then you will need a more fiddly solution.

链接地址: http://www.djcxy.com/p/14342.html

上一篇: 基于鼠标移动的旋转四元数(OpenGL和Java)

下一篇: 在2个轴上旋转OpenGL场景