/* 64 cubes de coté 0.5 places en un cube */ /* de 4 x 4 x 4 cubes */ /* et colores en fonction de leur position */ /* dans ce cube */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2019 */ #include #include #include #include #include /* Variables et constantes globales */ 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 cyan[] = { 0.0F,1.0F,0.0F,1.0F }; static const float magenta[] = { 1.0F,0.0F,1.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 float noir[] = { 0.0F,0.0F,0.0F,1.0F }; static const float grisMoyen[] = { 0.5F,0.5F,0.5F,1.0F }; static double px = 0.0; static double py = 0.0; static double pz = 25.0; static float rxs = 0.0; static float rys = 0.0; static float rzs = 0.0; static int affichage = 1; static int question = 0; static int animation = 0; static int coloration = 0; static int culling = 0; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ static void init(void) { glClearColor(0.25F,0.25F,0.25F,1.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc); glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); } ////////////////////////////////////////////////// static void carreTroue(double c) { float c2 =(float) c/2.0; float c4 =(float) c/4.0; glNormal3f(0.0F,0.0F,1.0F); glBegin(GL_QUAD_STRIP); glVertex3f( c2, c2,0.0F); glVertex3f( c4, c4,0.0F); glVertex3f( c2,-c2,0.0F); glVertex3f( c4,-c4,0.0F); glVertex3f(-c2,-c2,0.0F); glVertex3f(-c4,-c4,0.0F); glVertex3f(-c2, c2,0.0F); glVertex3f(-c4, c4,0.0F); glVertex3f( c2, c2,0.0F); glVertex3f( c4, c4,0.0F); glEnd(); } static void mySolidBox(double largeur,double hauteur,double profondeur) { glPushMatrix(); glScalef(largeur,hauteur,profondeur); glutSolidCube(1.0); glPopMatrix(); } static void structureElementaire(void) { glPushMatrix(); glPushMatrix(); glTranslatef(5.0F,0.0F,5.0F); mySolidBox(2.0F,12.0F,2.0F); glPopMatrix(); glPushMatrix(); glTranslatef(0.0F,5.0F,5.0F); mySolidBox(8.0F,2.0F,2.0F); glPopMatrix(); glPushMatrix(); glTranslatef(0.0F,-5.0F,5.0F); mySolidBox(8.0F,2.0F,2.0F); glPopMatrix(); glPopMatrix(); } static void cubeStylise(void) { glPushMatrix(); structureElementaire(); glRotatef(90.0F,0.0F,1.0F,0.0F); structureElementaire(); glRotatef(90.0F,0.0F,1.0F,0.0F); structureElementaire(); glRotatef(90.0F,0.0F,1.0F,0.0F); structureElementaire(); glPopMatrix(); } ////////////////////////////////////////////////// static void cubeStyliseColore(void) { glPushAttrib(GL_LIGHTING_BIT); glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc); structureElementaire(); glRotatef(90.0F,0.0F,1.0F,0.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge); structureElementaire(); glRotatef(90.0F,0.0F,1.0F,0.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,vert); structureElementaire(); glRotatef(90.0F,0.0F,1.0F,0.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune); structureElementaire(); glPopMatrix(); glPopAttrib(); } /* Scene dessinee, question 2 */ static void sceneQuestion2(void) { glPushMatrix(); carreTroue(15.0); glPopMatrix(); } /* Scene dessinee, question 3 */ static void sceneQuestion3(void) { glPushMatrix(); switch (coloration) { case 0 : cubeStylise(); break; case 1 : cubeStyliseColore(); break; } glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const GLfloat light0_position[] = { 0.4F,0.2F,1.0F,0.0F }; glPolygonMode(GL_FRONT_AND_BACK,(affichage) ? GL_FILL : GL_LINE); if ( affichage ) { glEnable(GL_LIGHTING); } else { glDisable(GL_LIGHTING); } if ( culling ) { glEnable(GL_CULL_FACE); } else { glDisable(GL_CULL_FACE); } glPushMatrix(); gluLookAt(px,py,pz,0.0,0.0,0.0,0.0,1.0,0.0); glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glRotatef(rxs,1.0F,0.0F,0.0F); glRotatef(rys,0.0F,1.0F,0.0F); glRotatef(rzs,0.0F,0.0F,1.0F); switch (question) { case 0: sceneQuestion2(); break; case 1: sceneQuestion3(); break; } glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL: %d\n",error); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* -> Ajustement de la camera de visualisation */ static void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); double ratio =(double) tx/ty; if ( ratio >= 1.0 ) gluPerspective(50.0,ratio,1.0,100.0); else gluPerspective(50.0/ratio,ratio,1.0,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee en tache de fond */ static void idle(void) { rxs += 0.71F; rys += 0.52F; rzs += 0.33F; glutPostRedisplay(); } /* Fonction executee lors de la frappe */ /* d'une touche speciale du clavier */ static void special(int key,int x,int y) { switch ( key ) { case GLUT_KEY_UP : py += 0.1; glutPostRedisplay(); break; case GLUT_KEY_DOWN : py -= 0.1; glutPostRedisplay(); break; case GLUT_KEY_LEFT : px -= 0.1; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : px += 0.1; glutPostRedisplay(); break; case GLUT_KEY_PAGE_UP : pz -= 0.1; glutPostRedisplay(); break; case GLUT_KEY_PAGE_DOWN : pz += 0.1; glutPostRedisplay(); break; case GLUT_KEY_F1 : px = 0.0; py = 0.0; pz = 25.0; glutPostRedisplay(); break; case GLUT_KEY_F2 : rxs = 0.0; rys = 0.0; rzs = 0.0; glutPostRedisplay(); break; case GLUT_KEY_F3 : rxs = 30.0; rys = 30.0; rzs = 0.0; glutPostRedisplay(); break; } } /* Fonction executee lors de la frappe */ /* d'une touche du clavier */ static void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x20 : affichage = (affichage+1)%2; glutPostRedisplay(); break; case 0x0D : question = (question+1)%2; glutPostRedisplay(); break; case 'a' : animation = (animation+1)%2; switch (animation) { case 0 : glutIdleFunc(NULL); break; case 1 : glutIdleFunc(idle); break; } break; case 'c' : coloration = (coloration+1)%2; glutPostRedisplay(); break; case '&' : culling = !culling; glutPostRedisplay(); 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(360,360); glutInitWindowPosition(50,50); glutCreateWindow("Epreuve TD n°1"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }