#include "windows.h" #include #include #include #include #include /* ************************************************** */ #define iw 128 #define ih 128 #define tf "Marbre.raw" static int w = 0 ; static int h = 0 ; static float rx = 0 ; static float ry = 0 ; static float rz = 0 ; static GLubyte im[3*iw*ih]; /* ************************************************** */ void lectureTexture(char *fichier,int dx,int dy) { FILE *f = fopen(fichier,"rb") ; if ( f ) { for ( int i = 0 ; i < dx ; i++ ) for ( int j = 0 ; j < dy ; j++ ) fread(&im[(j*dy+i)*3],1,3,f) ; fclose(f) ; } } /* -------------------------------------------------- */ /* La fonction myinit initialise les fonctionnalites */ /* suivantes : */ /* - un eclairage par une source lumineuse blanche */ /* - une gestion des parties cachees */ /* - un gestion de texture (marbre) */ /* -------------------------------------------------- */ void myinit(void) { GLfloat light_ambient[] = { 0.0F,0.0F,0.0F,1.0F }; GLfloat light_diffuse[] = { 1.0F,1.0F,1.0F,1.0F }; GLfloat light_specular[] = { 1.0F,1.0F,1.0F,1.0F }; GLfloat light_position[] = { 1.0F,1.0F,1.0F,0.0F }; glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); glLightfv(GL_LIGHT0,GL_POSITION,light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); lectureTexture(tf,iw,ih) ; glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glEnable(GL_MAP2_TEXTURE_COORD_2); glTexImage2D(GL_TEXTURE_2D, 0,3,iw,ih,0, GL_RGB, GL_UNSIGNED_BYTE, im); glEnable(GL_TEXTURE_2D); glEnable(GL_NORMALIZE); } /* -------------------------------------------------- */ /* Dessin d'un cube de cote cote, avec gestion des */ /* ombrages et d'une texture */ /* -------------------------------------------------- */ void myAuxSolidCube(double cote) { // sommets du cube static GLfloat vdata[8][3] = { { 0.5, 0.5,-0.5},{ 0.5,-0.5,-0.5}, {-0.5,-0.5,-0.5},{-0.5, 0.5,-0.5}, { 0.5, 0.5, 0.5},{ 0.5,-0.5, 0.5}, {-0.5,-0.5, 0.5},{-0.5, 0.5, 0.5}} ; // indexes des sommets des facettes static GLint t[6][4] = { {0,1,2,3}, {7,6,5,4}, {5,6,2,1}, {0,3,7,4}, {6,7,3,2}, {4,5,1,0}} ; // normales aux facettes static GLfloat ndata[6][3] = { { 0.0, 0.0,-1.0}, { 0.0, 0.0, 1.0}, { 0.0,-1.0, 0.0}, { 0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}, { 1.0, 0.0, 0.0}} ; // empilement de la transformation courante glPushMatrix(); // mise a l'echelle d'un facteur cote glScaled(cote,cote,cote) ; // dessin de chaque facette for ( int i = 0 ; i < 6 ; i++ ) { // facette a quatre sommets glBegin(GL_QUADS) ; // definition de la normale a la facette (ombrage) glNormal3fv(&ndata[i][0]); // definitions successives des quatre sommets et // de leurs positions dans la texture glTexCoord2f(0.0F,0.0F) ; glVertex3fv(&vdata[t[i][0]][0]); glTexCoord2f(1.0F,0.0F) ; glVertex3fv(&vdata[t[i][1]][0]); glTexCoord2f(1.0F,1.0F) ; glVertex3fv(&vdata[t[i][2]][0]); glTexCoord2f(0.0F,1.0F) ; glVertex3fv(&vdata[t[i][3]][0]); glEnd() ; } // depilement de la transformation courante glPopMatrix(); } /* -------------------------------------------------- */ /* La fonction display affiche deux cubes : */ /* - le premier genere par auxSolidCube */ /* - le deuxieme par la nouvelle fonction */ /* Les deux cubes tournent sur eux-memes (idle) */ /* -------------------------------------------------- */ void CALLBACK display(void) { glClearColor(0.3F,0.3F,0.3F,0.0F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glPushMatrix(); glTranslatef(120.0F,0.0F,0.0F) ; glRotatef(rx,1.0F,0.0F,0.0F) ; glRotatef(ry,0.0F,1.0F,0.0F) ; glRotatef(rz,0.0F,0.0F,1.0F) ; auxSolidCube(100.0) ; glPopMatrix(); glPushMatrix(); glTranslatef(-120.0F,0.0F,0.0F) ; glRotatef(rx,1.0F,0.0F,0.0F) ; glRotatef(ry,0.0F,1.0F,0.0F) ; glRotatef(rz,0.0F,0.0F,1.0F) ; myAuxSolidCube(100.0) ; glPopMatrix(); glPopMatrix(); glFlush(); auxSwapBuffers(); } /* -------------------------------------------------- */ void CALLBACK myReshape(int ww,int hh) { w = ww ; h = hh ; glViewport(0,0,ww,hh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-w/2,w/2,-h/2,h/2,-100.0,100.0); glMatrixMode(GL_MODELVIEW); } /* -------------------------------------------------- */ void CALLBACK idle(void) { rx += 1 ; ry += 2 ; rz += 3 ; display() ; } /* -------------------------------------------------- */ void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGB|AUX_DEPTH); auxInitPosition(10,10,500,300); auxInitWindow("Exercice 1 : Le cube"); myinit(); auxIdleFunc(idle); auxReshapeFunc(myReshape); auxMainLoop(display); } /* ************************************************** */