views:

618

answers:

3

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!

A: 

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.

Aaron Digulla
A: 

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);
sharvey
Found a Vec3f implementation here: http://wwwx.cs.unc.edu/~lastra/Courses/COMP238_F2000/Code/vec3f.hppAnyway, this didn't work.
KeniF
A: 

Hey there,

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.

Tartley
you are right, but my objects are NOT in the origin. Therefore, rotating twice means the second rotation is affected by the first.
KeniF
Hey there. I don't think that should matter, so long as you do all your rotations before you do your translation.Perhaps I'm misunderstanding what you need. Is my 'anti-aircraft gun' example a good comparison? Or not so much?Thanks!
Tartley
Now that I come to look at this, months later, a much belated but possibly constructive comment occurs to me: In order to get the rotations right, it is common to make sure all your objects start at the origin. Do the rotations as I describe, then tranlate the object to its position in the world. All of this is done by the single modelview matrix, so it is not any more expensive than just doing the rotations.
Tartley