PDA

View Full Version : help me, with the rotations according to global axis



airseb
07-30-2002, 11:24 AM
i have made a program that do turn a sphere
but the sphere turn according to local axis and not according to global (that i want), how can i do for that it becomes according to global axis ?

void display ()
{
static GLfloat tableau_entrelace [24][3] = {{1.0, 1.0, 1.0}, {-X, 0.0, Z}, {1.0, 1.0, 1.0}, {X, 0.0, Z},
{1.0, 1.0, 1.0}, {-X, 0.0, -Z}, {1.0, 1.0, 1.0}, {X, 0.0, -Z}, {1.0, 1.0, 1.0}, {0.0, Z, X},
{1.0, 1.0, 1.0}, {0.0, Z, -X}, {1.0, 1.0, 1.0}, {0.0, -Z, X}, {1.0, 1.0, 1.0}, {0.0, -Z, -X},
{1.0, 1.0, 1.0}, {Z, X, 0.0}, {1.0, 1.0, 1.0}, {-Z, X, 0.0}, {1.0, 1.0, 1.0}, {Z, -X, 0.0},
{1.0, 1.0, 1.0}, {-Z, -X, 0.0} }; //ceci est un tableau entrelacé de couleurs et de coordonnées de sommets

glInterleavedArrays (GL_C3F_V3F, 0, tableau_entrelace) ; //lecture dans le tableau entrelacé

/*definition des polygones par le numéro des sommets*/

static GLuint indices [20][3]= {1,4,0,4,9,0,4,5,9,8,5,4,1,8,4,1,10,8,10,3,8,8,3,5 ,3,2,5,
3,7,2,3,10,7,10,6,7,6,11,7,6,0,11,6,1,0,10,1,6,11, 0,9,2,11,9,5,2,9,11,2,7};

glPolygonMode (GL_FRONT, GL_LINE) ; //la face avant des polygones sera rendue en fil de fer
glFrontFace (GL_CCW) ; //spécifie la face avant
glEnable (GL_CULL_FACE) ;
glCullFace (GL_BACK) ; //suppression des faces arrières



glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glRotatef ( nbr_degre, j, k, l ) ;


for (GLint i= 0 ; i<20 ;i++)
{

subdivision (&tableau_entrelace [indices[i][0]*2+1][0], &tableau_entrelace [indices[i][1]*2+1][0], &tableau_entrelace [indices[i][2]*2+1][0]); //display the sphere
}


glutSwapBuffers() ; //permutte le back buffer avec le front buffer
glFlush () ;

}


void reshape (int w, int h)
{
glViewport (0, 0, w, h) ;
glMatrixMode (GL_PROJECTION) ;
glLoadIdentity () ;
glFrustum(-10.0 , 10.0, -7.5, 7.5, 15.0, 100.0);
glMatrixMode(GL_MODELVIEW); //la matrice active de modelisation/visualisation sera affectée
glLoadIdentity(); //charge la matrice identité

gluLookAt (0.0, 0.0, 25.0, 0.0,0.0,0.0, 0.0, 1.0, 0.0) ; //caméra placée sur l'axe des Z et regardant vers l'origine
}


void keyboard (unsigned char key, int x, int y)
{



switch (key)
{
case 'd' : //fait tourner selon l'axe des Y
case 'D' :
nbr_degre = 4.0;
j = 0.0 ;
k = 1.0 ;
l = 0.0 ;
glutPostRedisplay () ;
break ;

case 's' :
case 'S' :
nbr_degre = 4.0; //fait tourner selon l'axe des -Y
j = 0.0 ;
k = -1.0 ;
l = 0.0 ;
glutPostRedisplay () ;
break ;

case 'e' : //fait tourner selon l'axe des X
case 'E' :
nbr_degre = 4.0;
j = -1.0 ;
k = 0.0 ;
l = 0.0 ;
glutPostRedisplay () ;
break ;

case 'x' : //fait tourner selon l'axe des X
case 'X' :
nbr_degre = 4.0;
j = 1.0 ;
k = 0.0 ;
l = 0.0 ;
glutPostRedisplay () ;
break ;

}
}

nexusone
07-30-2002, 12:15 PM
As your code is now writen both your local and global axis are the same.

Your objects axis is 0,0,0
Your current global axis is 0,0,0

To change this you need to first rotate the object then translate your object.

example:

glRotate( Angle, 1,0,0) // rotate world around X axis.

glTranslatef( 5.0, 0.0, 0.0) // move object x units from X world axis.

glRotate( Object_angle, 1, 0, 0) // rotate on objects X axis.

draw_object();

Jambolo
07-30-2002, 11:30 PM
airseb:

The only way to do it is to keep track of the orientation yourself instead of letting OpenGL accumulate rotation increments. Then in the display function, rotate the object accordingly after glLoadIdentity and gluLookAt.

Also, the reshape function is a bad place to put gluLookAt, especially in this code. Everytime you resize the window, your rotation is going to reset.


Originally posted by nexusone:
As your code is now writen both your local and global axis are the same.


Actually, it's not. He accumulates a rotation increment each time a key is typed. Notice there is no glLoadIdentity in the display function.

nexusone
07-31-2002, 05:15 AM
I think you may have hit on his problem in the rotation, in not having a glLoadIdentity.
If he had a set angle of rotation, you would have a nice rotation with each keypress.
But since he is changing the angle, with each keypress, he is getting bigger amounts of rotation each time.

he also needs to add to his display() routine:

example:

display()
{
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);


}


Originally posted by Jambolo:
airseb:

The only way to do it is to keep track of the orientation yourself instead of letting OpenGL accumulate rotation increments. Then in the display function, rotate the object accordingly after glLoadIdentity and gluLookAt.

Also, the reshape function is a bad place to put gluLookAt, especially in this code. Everytime you resize the window, your rotation is going to reset.

Actually, it's not. He accumulates a rotation increment each time a key is typed. Notice there is no glLoadIdentity in the display function.