/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2007 */ /* Un programme de test des objets de GLUT */ #include #include #include #include #include #include "ModuleCylindres.h" #include "ModuleAxes.h" /* Variables globales pour l'activation */ /* de la gestion des ombrages et d'une rotation */ static int l = 1; static int r = 0; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ void init(void) { const GLfloat l_pos[] = { 1.0F,1.0F,1.0F,0.0F }; const GLfloat c[4] = { 0.5F,0.2F,0.6F,1.0F }; glMaterialfv(GL_FRONT,GL_DIFFUSE,c); glLightfv(GL_LIGHT0,GL_POSITION,l_pos); glEnable(GL_LIGHT0); glColor4fv(c) ; glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); } /* Scene dessinee */ void mySolidCube(double w,double h,double d) { glPushMatrix(); glScalef(w,h,d); glutSolidCube(1.0); glPopMatrix(); } void scene(void) { glPushMatrix(); glPushMatrix(); glTranslatef(1.5F,1.5F,0.0F); glutSolidSphere(1.0,36,36); glPopMatrix(); glPushMatrix(); glTranslatef(-1.5F,1.5F,0.0F); mySolidCube(2.0,2.0,2.0); glPopMatrix(); glPushMatrix(); glTranslatef(-1.5F,-1.5F,0.0F); glutSolidCone(1.0,2.0,36,10); glPopMatrix(); glPushMatrix(); glTranslatef(1.5F,-1.5F,0.0F); solidCylindre(1.0,2.0,36,10); glPopMatrix(); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.8F,0.8F,0.8F,1.0F) ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ; if ( l ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); glPushMatrix(); if ( r ) { glRotatef(20.0F,1.0F,0.0F,0.0F); glRotatef(-30.0F,0.0F,1.0F,0.0F); } axes(); scene(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } /* Fonction executee lors de la frappe */ /* d'une touche du clavier */ void key(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : l = !l ; glutPostRedisplay(); break; case 0x20 : r = !r ; glutPostRedisplay(); break; case 0x1B : exit(0); } } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho((double) -3.0*tx/ty,(double) 3.0*tx/ty,-3.0,3.0,-5.0,5.0) ; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); glutInitWindowSize(350,350); glutInitWindowPosition(50,50); glutCreateWindow("Test objets"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutDisplayFunc(display); glutMainLoop(); return(0); }