/* 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 int affichage = 1; static int version = 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); glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); } /* Scene dessinee */ static void scene() { glPushMatrix(); for ( int x = -3 ; x <= 3 ; x += 2 ) for ( int y = -3 ; y <= 3 ; y += 2 ) for ( int z = -3 ; z <= 3 ; z += 2 ) { glPushMatrix(); glTranslatef((float) x,(float) y,(float) z); float couleur[4] = { (x+3)/6.0F,(y+3)/6.0F,(z+3)/6.0F,1.0F }; glMaterialfv(GL_FRONT,GL_DIFFUSE,couleur); glutSolidCube(0.5); glPopMatrix(); } glPopMatrix(); } ////////////////////////////////////////////////// static double px = 0.0; static double py = 0.0; static double pz = 10.0; /* 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.0,0.0,10.0,1.0 }; glPolygonMode(GL_FRONT_AND_BACK,(affichage) ? GL_FILL : GL_LINE); glPushMatrix(); switch (version) { case 0 : glLightfv(GL_LIGHT0,GL_POSITION,light0_position); gluLookAt(px,py,pz,0.0,0.0,0.0,0.0,1.0,0.0); break; case 1 : gluLookAt(px,py,pz,0.0,0.0,0.0,0.0,1.0,0.0); glLightfv(GL_LIGHT0,GL_POSITION,light0_position); break; } scene(); 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(80.0,ratio,1.0,20.0); else gluPerspective(80.0/ratio,ratio,1.0,20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* 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 = 10.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 : version = (version+1)%2; 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("64 cubes"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }