/********************************************************/ /* cube.c */ /********************************************************/ /* Affiche a l'ecran un cube en 3D */ /********************************************************/ /* inclusion des fichiers d'en-tete Glut */ #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_SINGLE | 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 affichage() { int i,j; /* effacement de l'image avec la couleur de fond */ /**********************************/ /**********************************/ /**********************************/ // here we ask to draw on front buffer //glDrawBuffer(GL_FRONT); /**********************************/ /**********************************/ /**********************************/ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(-angley,1.0,0.0,0.0); glRotatef(-anglex,0.0,1.0,0.0); /* 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