/* Une animation en OpenGL */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2012 */ #include #include #include #include #include #include "Damier.h" /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ static int image = 0; static const float noir[] = { 0.0F,0.0F,0.0F,1.0F }; static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static const float vert[] = { 0.0F,1.0F,0.0F,1.0F }; static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; static const GLfloat light0_position[] = { 0.0, 1.0, 0.0, 1.0 }; static const GLfloat light1_position[] = { -1.0, 1.0, 1.0, 0.0 }; static const GLfloat light2_position[] = { 1.0,-1.0, 1.0, 0.0 }; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ static void init(void) { const GLfloat shininess[] = { 50.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,noir); glMaterialfv(GL_FRONT,GL_SHININESS,shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge); glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune); glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); } /* Une roue */ static void roue(void) { glPushMatrix(); glPushMatrix(); glScalef(1.0F,1.0F,2.0F); glutSolidTorus(0.1,0.9F,18,72); glPopMatrix(); for ( int i = 0 ; i < 8 ; i++ ) { glPushMatrix(); glRotatef(22.5F*i,0.0F,0.0F,1.0F); glScalef(1.8F,0.04F,0.04F); glutSolidCube(1.0); glPopMatrix(); } glPopMatrix(); } /* Un essieu */ static void essieu(void) { glPushMatrix(); glPushMatrix(); glScalef(0.1F,0.1F,2.2F); glutSolidCube(1.0); glPopMatrix(); glTranslatef(0.0F,0.0F,1.1F); glScalef(0.4F,0.4F,0.4F); roue(); glPopMatrix(); glPushMatrix(); glTranslatef(0.0F,0.0F,-1.1F); glScalef(0.4F,0.4F,0.4F); roue(); glPopMatrix(); } /* Une automobile stylisee */ static void mobile(float rotationRoue) { glPushMatrix(); glPushMatrix(); glTranslatef(0.0F,0.6F,0.0F); glPushMatrix(); glScalef(5.0F,0.7F,1.7F); glutSolidSphere(0.65,72,72); glPopMatrix(); glPushMatrix(); glTranslatef(2.5F,0.3F,0.0F); glScalef(0.3F,0.3F,0.3F); glutSolidSphere(0.25,72,72); glPopMatrix(); glPushMatrix(); glTranslatef(0.5F,0.35F,0.0F); glScalef(2.8F,0.7F,1.7F); glutSolidSphere(0.5,72,72); glPopMatrix(); glPopMatrix(); glPushMatrix(); glTranslatef(1.7F,0.4F,0.0F); glRotatef(rotationRoue,0.0F,0.0F,1.0F); essieu(); glPopMatrix(); glPushMatrix(); glTranslatef(-1.8F,0.4F,0.0F); glRotatef(rotationRoue,0.0F,0.0F,1.0F); essieu(); glPopMatrix(); glPopMatrix(); } ////////////////////////////////////////////////// /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClearColor(0.25F,0.25F,0.25F,0.25F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); gluLookAt(20.0,30.0,60.0,0.0,0.0,0.0,0.0,1.0,0.0); glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glLightfv(GL_LIGHT1,GL_POSITION,light1_position); glLightfv(GL_LIGHT2,GL_POSITION,light2_position); damier(); glRotatef(-image,0.0F,1.0F,0.0F); glTranslatef(0.0F,0.0F,15.0F); mobile(image/10.0F); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention erreur %d\n",error); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ static void idle(void) { image++; glutPostRedisplay(); } ////////////////////////////////////////////////// /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ static void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(20.0F,(float) x/y,1.0,1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ static void keyboard(unsigned char key,int x,int y) { switch (key) { case ' ' : { static int anim = 1; anim = !anim; glutIdleFunc(( anim ) ? idle : NULL); } break; case 0x1B : exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Un mobile"); init(); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return(0); }