# desperate for help with upvector rotation

HI have had no luck in trying to solve this problem.

I have a model loaded into the origin 0,0,0 of an opengl window. I want the user to be able to rotate the model by draging the mouse cursor left/right and updown on the window. I want to always rotate left/right around the current up vector, and updown around the current right vector. This is easy when its the default axis alignment, but how do you calculate these rotation vectors once the model has been moved out of the default axis alignment. I am trying to find a way to calc the vectors i want, then use a glrotate command to rotate around each of my desired vectors.

I’ve wrestled with this before, and while I don’t have my code in front of me, here’s how I did it:

``````// Assume x and y are relative mouse motion and
// that this code only executes when dragging
//
{
int x, y;
static float RotX, RotY;

RotX += y; RotY += x; //I know...trust me

if (RotX > 360) RotX %= 360;
// do same for Y

if (RotX < 0) RotX+=360;
// do same for Y

if (RotX > 90 && RotX < 270) /* backside */
RotY -= (2 * y)  /* go other way
``````

Sorry if I’ve forgotten some things. Oh, obviously apply those RotX and Y to the Modelview matrix.

Good Luck,
Matt

Sorry, but i think you miss understood what im asking. The glrotate command rotates around a specific vector, that you specify. Once the model has been rotated out of the default axis alignment the upvector is no longer (0,1,0) ie y axis it is something else. So when the user drags the mouse left and right across the screen i want to rotate around this new upvector. How can i find what it is ?

Originally posted by spooky:
… The glrotate command rotates around a specific vector, that you specify. Once the model has been rotated out of the default axis alignment the upvector is no longer (0,1,0) ie y axis it is something else. So when the user drags the mouse left and right across the screen i want to rotate around this new upvector. How can i find what it is ?
You don’t NEED to find what this new vector is. A call to glRotate doesn’t rotate your object, it rotates the object’s axes and then draws the object with respect to the new axes. So, if you call glRotate with an axis of (0,1,0) after the default axis alignment has changed, the rotation will occur about the new upvector.

Perhaps I’ve been unclear. Of course glRotate works around a specified vector. That’s what those RotX and RotY are for in my code. Here’s the idea:

``````// In display() or whatever
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glRotatef( RotX, 1, 0, 0 0 );
glRotatef( RotY, 1, 0, 0 0 );
glPopMatrix();
``````

The other possibility is of course that I am misunderstanding you and you need to be stacking rotations like this:

``````GLfloat rotate;

glPushMatrix();
glGetFloatv( GL_MODELVIEW_MATRIX, rotate );
glRotatef(...
``````

Now rotate that matrix whenever you need to move and just Load or MultMatrix to apply it in display.

Im sorry, i am willing to conceed i am totaly wrong about this, but i dont agree with you.

The glRotate function computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z).

The above explanation from opengl help file clearly states it rotates around vector XYZ.

Imagine this, default upVector=(0,1,0). If you rotate this 90deg ccw on the yz plane your new up vector will be (0,0,-1). Once the model is in this position any left/right drag of the mouse to uspdate the rotation around the (0,1,0) axis will make it rotate around the old axis, which is now pointing in a wrong direction. At his point when i move the mouse left and right the model should rotate around (0,0,1) which is the -z axis, not around the old y axis

Curandero,

i think your idea of stacking rotations might be what im after, any chance of more info or a link to tutorial ?

would appreciate it

thanks

It might help to note that (0,0,0) is not much of a vector…
You may want to try something more like this:
glRotatef(X_Angle, 1.0, 0.0, 0.0); // X Axis
glRotatef(Y_Angle, 0.0, 1.0, 0.0); // Y Axis

Store 3 vectors for your object: right, up, and forward. Write a function to rotate the vectors yourself. To make the object rotate around the up vector, you rotate the right and forward vectors around the up vector. Do it similarly for rotation around the other 2 vectors. Use the cross product to keep the vectors orthogonal. When it is time to draw, you dont use glRotate at all, but instead put the right, up, and forward vectors into the 3x3 part of a 4x4 matrix. Put the translation amount in the fourth row. Use one call to glMultMatrix, and you are ready to draw your object with the correct transformation.