PDA

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



airseb
07-30-2002, 11:05 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 ;

}
}

fresh
07-30-2002, 12:11 PM
Break your transformation up into 3 stages.

float angles[3] = {0,0,0};

void render()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-angles[2],0,0,1); // roll
glRotatef(-angles[0],1,0,0); // pitch
glRotatef(-angles[1],0,1,0); // yaw
// draw stuff
}

In your keypress function you can increment/decrement your angles[] array and it should rotate properly now.

Also you don't need that glFlush() after the swapbuffer call.

[This message has been edited by fresh (edited 07-30-2002).]