/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2007 */ /* Une sphere texturee avec une image */ /* de la terre */ #include #include #include #include #include #include "ChargePngFile.h" #include "ModuleSpheres.h" /* Variables globales */ static int disc = 72; static int texturage = 1; static int sphere = 0; static int mode = 1; static float angle = 0.0F; /* Variables globales de gestion */ /* de l'interactivite clavier et souris */ static int clic = 0; static int mx; static int my; static float rx = 0.0F; static float ry = 0.0F; void init(void) { const GLfloat mat_shininess[] = { 100.0 }; const GLfloat blanc[] = { 1.0,1.0,1.0,1.0 }; int resx; int resy; GLubyte *image; glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc); glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); image = chargeImagePng("Terre-02048.png",&resx,&resy); if ( image ) { glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexImage2D(GL_TEXTURE_2D,0,3, resx,resy,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); free(image); } } /* Scene dessinee */ void scene(void) { glPushMatrix(); const GLfloat light0_position[] = { 1.0,0.0,0.0,0.0 }; glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glRotatef(-23.0F,0.0F,0.0F,1.0F); glRotatef(angle,0.0F,1.0F,0.0F); switch ( sphere ) { case 0 : glutSolidSphere(4.0,disc,disc); break; case 1 : mySolidSphere(4.0,disc,disc); break; } glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glPolygonMode(GL_FRONT,(mode) ? GL_FILL : GL_LINE); if ( texturage ) 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); scene(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } /* Fonction executee lors d'un clic de souris */ /* dans la fenetre */ void mouse(int bouton,int etat,int x,int y) { if ( bouton == GLUT_LEFT_BUTTON ) { if ( etat == GLUT_DOWN ) { clic = 1; mx = x; my = y; } if ( etat == GLUT_UP ) { clic = 0; } } } /* Fonction executee lors d'un deplacement */ /* de la souris sur la fenetre */ /* avec un bouton appuye */ void motion(int x,int y) { if ( clic ) { ry += (x-mx); rx += (y-my); mx = x; my = y; 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(); glOrtho(-5.0,5.0,(double) -5.0*y/x,(double) 5.0*y/x,-5.0,5.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void idle(void) { angle += 0.5F; glutPostRedisplay(); } /* Fonction executee lors de la frappe */ /* d'une touche alphanumerique du clavier */ void key(unsigned char key,int x,int y) { switch ( key ) { case 43 : disc++; glutPostRedisplay(); break; case 45 : disc--; if ( disc < 1 ) disc = 1; glutPostRedisplay(); break; case 'f' : mode = !mode; glutPostRedisplay(); break; case 0x20 : sphere = !sphere; glutPostRedisplay(); break; case 0x0D : texturage = !texturage; glutPostRedisplay(); break; case 'a' : { static int anim = 1; anim = !anim; glutIdleFunc((anim) ? idle : NULL); } break; case 0x1B : exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(260,260); glutInitWindowPosition(50,50); glutCreateWindow("La terre"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMotionFunc(motion); glutMouseFunc(mouse); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return(0); }