/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2003 */ /* Placage de texture sur un cube */ #include #include #include #include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846264338327950288 #endif /* 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 blanc[] = { 1.0F,1.0F,1.0F,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 GLubyte *image; static char *filename = "Image-256-256.raw"; GLubyte *makeImage(void) { GLubyte *tab =(GLubyte *) malloc(256*256*3*sizeof(GLubyte)); FILE *f = fopen(filename,"rb"); fread(tab,sizeof(GLubyte),256*256*3,f); fclose(f); return(tab); } void myinit(void) { glClearColor(0.6F,0.6F,0.6F,0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LESS); image = makeImage(); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,image); 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); glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); } void cube(){ float c = 0.5F; glPushMatrix(); glBegin(GL_QUADS); { glNormal3f(0.0F,0.0F,-1.0F); glTexCoord2f(0.0F,0.0F); glVertex3f(c,c,-c); glTexCoord2f(1.0F,0.0F); glVertex3f(c,-c,-c); glTexCoord2f(1.0F,1.0F); glVertex3f(-c,-c,-c); glTexCoord2f(0.0F,1.0F); glVertex3f(-c,c,-c); } { glNormal3f(0.0F,0.0F,1.0F); glTexCoord2f(0.0F,1.0F); glVertex3f(c,c,c); glTexCoord2f(0.0F,0.0F); glVertex3f(-c,c,c); glTexCoord2f(1.0F,0.0F); glVertex3f(-c,-c,c); glTexCoord2f(1.0F,1.0F); glVertex3f(c,-c,c); } { glNormal3f(-1.0F,0.0F,0.0F); glTexCoord2f(0.0F,1.0F); glVertex3f(-c,c,-c); glTexCoord2f(1.0F,1.0F); glVertex3f(-c,-c,-c); glTexCoord2f(1.0F,0.0F); glVertex3f(-c,-c,c); glTexCoord2f(0.0F,1.0F); glVertex3f(-c,c,c); } { glNormal3f(1.0F,0.0F,0.0F); glTexCoord2f(0.0F,1.0F); glVertex3f(c,c,c); glTexCoord2f(1.0F,1.0F); glVertex3f(c,-c,c); glTexCoord2f(1.0F,0.0F); glVertex3f(c,-c,-c); glTexCoord2f(0.0F,0.0F); glVertex3f(c,c,-c); } { glNormal3f(0.0F,-1.0F,0.0F); glTexCoord2f(0.0F,1.0F); glVertex3f(-c,-c,c); glTexCoord2f(1.0F,1.0F); glVertex3f(-c,-c,-c); glTexCoord2f(1.0F,0.0F); glVertex3f(c,-c,-c); glTexCoord2f(0.0F,0.0F); glVertex3f(c,-c,c); } { glNormal3f(0.0F,1.0F,0.0F); glTexCoord2f(1.0F,0.0F); glVertex3f(c,c,c); glTexCoord2f(0.0F,0.0F); glVertex3f(c,c,-c); glTexCoord2f(0.0F,1.0F); glVertex3f(-c,c,-c); glTexCoord2f(1.0F,1.0F); glVertex3f(-c,c,c); } glEnd(); glPopMatrix(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); glRotatef(rz,0.0F,0.0F,1.0F); cube(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL\n",error); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ void idle(void) { rx += 0.1256F; ry += 0.2117F; rz += 0.1174F; glutPostRedisplay() ; } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION) ; glLoadIdentity() ; gluPerspective(10.0F,(float) x/y,1.0,40.0) ; glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0.0,0.0,10.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 */ void keyboard(unsigned char key,int x,int y) { switch (key) { case 0x0D : { static int anim = 0; anim = !anim; glutIdleFunc(( anim ) ? idle : NULL); } break; case 0x1B : free(image); exit(0); break; } } /* Fonction executee lors de l'appui */ /* d'une touche de curseur ou d'une touche */ /* page up ou page down */ 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; } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(260,260); glutInitWindowPosition(50,50); glutCreateWindow("Un cube avec texture"); myinit(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutDisplayFunc(display); glutMainLoop(); return(0); }