/********************************************************/ /* cube.c */ /********************************************************/ /* Affiche a l'ecran un cube en 3D */ /********************************************************/ /* inclusion des fichiers d'en-tete Glut */ #include #include /* Notre structure point */ typedef struct { float x; float y; float z; float r; float g; float b; } point; point p[8]={ {-0.5,-0.5, 0.5,1.0,0.0,0.0}, {-0.5, 0.5, 0.5,0.0,1.0,0.0}, { 0.5, 0.5, 0.5,0.0,0.0,1.0}, { 0.5,-0.5, 0.5,1.0,1.0,1.0}, {-0.5,-0.5,-0.5,1.0,0.0,0.0}, {-0.5, 0.5,-0.5,0.0,1.0,0.0}, { 0.5, 0.5,-0.5,0.0,0.0,1.0}, { 0.5,-0.5,-0.5,1.0,1.0,1.0}}; int f[6][4]={ {0,1,2,3}, {3,2,6,7}, {4,5,6,7}, {0,1,5,4}, {1,5,6,2}, {0,4,7,3}}; char presse; int anglex,angley,x,y,xold,yold; /* Prototype des fonctions */ void affichage(); void clavier(unsigned char touche,int x,int y); void reshape(int x,int y); void idle(); void mouse(int bouton,int etat,int x,int y); void mousemotion(int x,int y); int main(int argc,char **argv) { /* initialisation de glut et creation de la fenetre */ glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition(200,200); glutInitWindowSize(250,250); glutCreateWindow("cube"); /* Initialisation d'OpenGL */ glClearColor(0.0,0.0,0.0,0.0); glColor3f(1.0,1.0,1.0); glPointSize(2.0); glEnable(GL_DEPTH_TEST); /* enregistrement des fonctions de rappel */ glutDisplayFunc(affichage); glutKeyboardFunc(clavier); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(mousemotion); /* Entree dans la boucle principale glut */ glutMainLoop(); return 0; } void showMatrix() { double matrix[16]; glGetDoublev(GL_MODELVIEW_MATRIX, matrix); std::cout << "_________________________________" << std::endl; for(int i = 0 ; i < 4 ; i++) { std::cout << "|"; for(int j = 0 ; j < 4 ; j++) std::cout << matrix[j+4*i] << "\t|"; std::cout << std::endl; std::cout << "---------------------------------" << std::endl; } } void affichage() { int i,j; /* effacement de l'image avec la couleur de fond */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); showMatrix(); glRotated(-angley,1.0,0.0,0.0); glRotated(-anglex,0.0,1.0,0.0); glScaled(0.25, 4E38, 0.5); showMatrix(); /* Dessin du cube */ for (i=0;i<6;i++) { glBegin(GL_POLYGON); for (j=0;j<4;j++) { glColor3f(p[f[i][j]].r,p[f[i][j]].g,p[f[i][j]].b); glVertex3f(p[f[i][j]].x,p[f[i][j]].y,p[f[i][j]].z); } glEnd(); } glFlush(); /* On echange les buffers */ glutSwapBuffers(); } void clavier(unsigned char touche,int x,int y) { switch (touche) { case 'p': /* affichage du carre plein */ glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glutPostRedisplay(); break; case 'f': /* affichage en mode fil de fer */ glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glutPostRedisplay(); break; case 's' : /* Affichage en mode sommets seuls */ glPolygonMode(GL_FRONT_AND_BACK,GL_POINT); glutPostRedisplay(); break; case 'd': glEnable(GL_DEPTH_TEST); glutPostRedisplay(); break; case 'D': glDisable(GL_DEPTH_TEST); glutPostRedisplay(); break; //case 'q' : /*la touche 'q' permet de quitter le programme */ //exit(0); } } void reshape(int x,int y) { if (x