/* Texturage d'une facette carree */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2012 */ #include #include #include #include #include #include #include /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ static float rx = 0.0F; static float ry = 0.0F; static float rz = 0.0F; static const float gris[] = { 0.7F,0.7F,0.7F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static const float vert[] = { 0.0F,1.0F,0.0F,1.0F }; static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; static int enCouleur = 0; static int face = 1; static int lum = 1; static int tex = 1; ////////////////////////////////////////////////// static const float noir[] = { 0.0F,0.0F,0.0F,1.0F }; static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ static int fileSize(char *filename) { struct _finddata_t c_file; intptr_t hFile; if( (hFile = _findfirst(filename,&c_file)) == -1L ) return(-1); int size = c_file.size; _findclose(hFile); return(size); } static unsigned char *loadRawImage(char *filename) { int size = fileSize(filename); if ( size == -1 ) return(NULL); unsigned char *img =(unsigned char *) malloc(size); if ( !img ) return(NULL); FILE *f = fopen(filename,"rb"); if ( !f ) { free(img); return(NULL); } fread(img,1,size,f); fclose(f); return(img); } static void init(void) { const GLfloat shininess[] = { 50.0 }; glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,noir); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,blanc); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,noir); glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shininess); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); glPixelStorei(GL_UNPACK_ALIGNMENT,1); { unsigned char *img = loadRawImage("Image.raw"); glTexImage2D(GL_TEXTURE_2D,0,3,64,64,0,GL_RGB,GL_UNSIGNED_BYTE,img); free(img); } glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); } /* Scene affichee */ static void scene() { glPushMatrix(); glBegin(GL_QUADS); glTexCoord2f(0.0F,0.0F); glVertex2f(2.0F,2.0F); glTexCoord2f(1.0F,0.0F); glVertex2f(-2.0F,2.0F); glTexCoord2f(1.0F,1.0F); glVertex2f(-2.0F,-2.0F); glTexCoord2f(0.0F,1.0F); glVertex2f(2.0F,-2.0F); glEnd(); glPopMatrix(); } ////////////////////////////////////////////////// /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClearColor(0.75F,0.75F,0.75F,1.0F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); const GLfloat light0_dir[] = { 0.7, 0.3, 0.7, 0.0 }; const GLfloat light1_dir[] = { -0.7, 0.0, 0.7, 0.0 }; const GLfloat light2_dir[] = { 0.7,-0.3, 0.7, 0.0 }; glLightfv(GL_LIGHT0,GL_POSITION,light0_dir); glLightfv(GL_LIGHT1,GL_POSITION,light1_dir); glLightfv(GL_LIGHT2,GL_POSITION,light2_dir); if ( enCouleur ) { glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge); glLightfv(GL_LIGHT1,GL_DIFFUSE,vert); glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu); } else { glLightfv(GL_LIGHT0,GL_DIFFUSE,gris); glLightfv(GL_LIGHT1,GL_DIFFUSE,gris); glLightfv(GL_LIGHT2,GL_DIFFUSE,gris); } glPolygonMode(GL_FRONT_AND_BACK,( face ) ? GL_FILL : GL_LINE); if ( lum ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); if ( tex ) glEnable(GL_TEXTURE_2D); else glDisable(GL_TEXTURE_2D); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); glRotatef(rz,0.0F,0.0F,1.0F); scene(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL: %d\n",error); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ static void idle(void) { rx += 0.14256F ; ry += 0.18117F ; rz += 0.23174F ; glutPostRedisplay() ; } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ static void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION) ; glLoadIdentity() ; gluPerspective(70.0F,(float) x/y,1.0,40.0) ; glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ static void keyboard(unsigned char key,int x,int y) { switch (key) { case ' ' : tex = !tex; glutPostRedisplay(); break; case 'c' : enCouleur = !enCouleur; glutPostRedisplay(); break; case 'l' : lum = !lum; glutPostRedisplay(); break; case 'f' : face = !face; glutPostRedisplay(); break; case 0x0D : { static int anim = 1; anim = !anim; glutIdleFunc(( anim ) ? idle : NULL); } break; case 0x1B : exit(0); break; } } /* Fonction executee lors de l'appui */ /* d'une touche de curseur ou d'une touche */ /* page up ou page down */ static void special(int key,int x,int y) { switch(key) { case GLUT_KEY_UP : rx++; glutPostRedisplay() ; break; case GLUT_KEY_DOWN : rx--; glutPostRedisplay() ; break; case GLUT_KEY_LEFT : ry++; glutPostRedisplay() ; break; case GLUT_KEY_RIGHT : ry--; glutPostRedisplay() ; break; case GLUT_KEY_PAGE_UP : rz++; glutPostRedisplay() ; break; case GLUT_KEY_PAGE_DOWN : rz--; glutPostRedisplay() ; break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Texturage d'une facette carrée"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return(0); }