/* Auteur: Nicolas JANEY */ /* Avril 2001 */ /* Texturage OpenGL d'un teapot */ /* avec du marbre */ #include #include #include #include #include #include #include #define iw 128 #define ih 128 #define tf "Marbre.raw" static GLubyte im[3*iw*ih]; static GLfloat view_rotx=50.0F ; static GLfloat view_roty=20.0F ; static GLfloat view_rotz=10.0F ; 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) ; } } void CALLBACK display(void) { glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); glPushMatrix(); glEnable(GL_DEPTH_TEST); glRotatef(view_rotx,1.0,0.0,0.0); glRotatef(view_roty,0.0,1.0,0.0); glRotatef(view_rotz,0.0,0.0,1.0); auxSolidTeapot(5) ; glPopMatrix(); glFlush(); auxSwapBuffers(); } void CALLBACK myReshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if( w <= h ) { float y = 7.F*(float)h/(float)w ; glOrtho(-7.,7.,-y,y,-17.,17.); } else { float x = 7.F*(float)w/(float)h ; glOrtho(-x,x,-7.,7.,-17.,17.); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void initlights(void) { GLfloat ambient[] = { 0.2F,0.2F,0.2F,1.0F }; GLfloat diffuse[] = { 3.2F,3.2F,3.2F,1.0F }; GLfloat pos[] = { 0.0F,0.0F,-1.0F,0.0F }; GLfloat spec[] = { 1.0F,1.0F,1.0F,1.0F }; GLfloat shininess[] = { 50.0F }; glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0,GL_AMBIENT,ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,spec); glMaterialfv(GL_FRONT,GL_SHININESS,shininess); } void myinit(void) { glClearColor(0.2F,0.2F,0.2F,1.0F); 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_AUTO_NORMAL); glEnable(GL_NORMALIZE); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); initlights(); } void CALLBACK keyx(void) { view_rotx += 2 ; } void CALLBACK keyX(void) { view_rotx -= 2 ; } void CALLBACK keyy(void) { view_roty += 2 ; } void CALLBACK keyY(void) { view_roty -= 2 ; } void CALLBACK keyz(void) { view_rotz += 2 ; } void CALLBACK keyZ(void) { view_rotz -= 2 ; } void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH); auxInitPosition(0,0,300,300); auxInitWindow("Teapot avec texture"); myinit(); auxKeyFunc(AUX_x,keyx) ; auxKeyFunc(AUX_X,keyX) ; auxKeyFunc(AUX_y,keyy) ; auxKeyFunc(AUX_Y,keyY) ; auxKeyFunc(AUX_z,keyz) ; auxKeyFunc(AUX_Z,keyZ) ; auxReshapeFunc(myReshape); auxMainLoop(display); }