/******** MINISQUEL : squelette de programme OpenGL **********\ \* cc minisquel.c -o minisquel -lglut -lGLU -lGL -lXmu -lX11 -lm */ #include #include /* contient déjà et */ #include int SIZEX,SIZEY; /* taille de la fenêtre */ double Cphi = 0., Ctheta = 0.; /* orientation de la caméra */ GLfloat diffuse_color[4] = { 1.5, 1.5, 1.5, 1.}; GLfloat light_pos[4] = { 0., 0., 1., 0.}; /* à l'infini */ int objet = 0; #define NB_OBJ 3 double Ophi [NB_OBJ] = {0., 0., 0.}; /* orientation des objets */ double Otheta[NB_OBJ] = {0., 0., 0.}; float scale = 3.; /*************** interface (ligne commande, clavier, souris, menus) */ void reshape(int x,int y) { /* la taille de la fenetre change */ SIZEX=x; SIZEY=y; glViewport(0,0,SIZEX,SIZEY); } /* ----- gestion des touches du clavier */ void key(unsigned char c,int x,int y) { switch (c) { case 27: case 'q': exit (0); case 'o': objet = (objet+1)%3; break; } glutPostRedisplay(); } /* ----- gestion de la souris */ int clic = 0; void mouse_motion(int x,int y) { /* mouvement de la souris */ if (clic) { Cphi += .1 * (x/(float)SIZEX - .5); Ctheta += .1 * (.5 - y/(float)SIZEY); glutPostRedisplay(); } } void mouse_click(int button,int state,int x,int y) { /* clic de souris */ clic = (button==GLUT_LEFT_BUTTON) && (state==GLUT_DOWN); } /*************** affichage ******************************************/ /* description géométrique de l'objet 3 (pyramide). Ici, on a choisi de mettre des normales aux faces et non aux sommets (i.e. objet volontairement polyèdrique, et non pas lisse), et des couleurs aux sommets et non pas aux faces (i.e. interpolées le long des faces). */ #define SQRT2 1.4142138 /* sqrt(2) */ #define SQRT3S2 .8660254 /* sqrt(3)/2 */ #define nb_faces 4 float coords[nb_faces][3] = { { 0, SQRT2, 0}, {-SQRT3S2,0,-.5}, { SQRT3S2,0,-.5}, { 0 ,0, 1.} }; float colors[nb_faces][3] = { {.4,.4,.4},{1,0,0},{0,1,0},{0,0,1} }; int faces[nb_faces][3] = { {0,1,2}, {0,2,3}, {0,3,1}, {3,2,1} }; float normals[nb_faces][3] = { { 0, 1/3., -2*SQRT2/3}, { SQRT2/3, 1/3., SQRT3S2*SQRT2/3}, {-SQRT2/3, 1/3., SQRT3S2*SQRT2/3}, { 0, -1., 0} }; void draw_scene() { /* tracé de la scène */ int i; glPushMatrix(); glTranslatef(-4.,0.,0.); glRotatef(Otheta[0]*180/M_PI, 1., 0., 0.); /* on oriente l'objet 1 */ glRotatef( Ophi[0]*180/M_PI, 0., 1., 0.); glColor3f(1.,.5,.5); glutSolidTorus(1.0,2.0,20,30); glPopMatrix(); glPushMatrix(); glTranslatef(4.,0.,0.); glRotatef(Otheta[1]*180/M_PI, 1., 0., 0.); /* on oriente l'objet 2 */ glRotatef( Ophi[1]*180/M_PI, 0., 1., 0.); glColor3f(.5,.5,1.); glutSolidCube(3.); glPopMatrix(); glPushMatrix(); glTranslatef(0.,0.,-3.); glRotatef(Otheta[2]*180/M_PI, 1., 0., 0.); /* on oriente l'objet 3 */ glRotatef( Ophi[2]*180/M_PI, 0., 1., 0.); glScalef(scale,scale,scale); glBegin(GL_TRIANGLES); for(i=0; i