Rotating OpenGL scene in 2 axes

2019-06-26 03:47发布

问题:

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!

回答1:

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.



回答2:

You could probably use something like this:

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


回答3:

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.