/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2004 */ /* Un programme OpenGL fonctionnel */ #include #include #include #include #include static int l = 1; void key(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : l = !l ; glutPostRedisplay(); break; case 0x1B : exit(0); } } void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho((double) -tx/ty,(double) tx/ty,-1.0,1.0,-1.0,1.0) ; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void scene(void) { glPushMatrix(); glutSolidSphere(0.8,36,36); glPopMatrix(); } void display(void) { glClearColor(0.8F,0.8F,0.8F,1.0F) ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ; glEnable(0); if ( l ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); glPushMatrix(); scene(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } void init(void) { GLfloat l_pos[] = { 0.0F,0.0F,1.0F,0.0F }; 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); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); glutInitWindowSize(200,200); glutInitWindowPosition(50,50); glutCreateWindow("OpenGL"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutDisplayFunc(display); glutMainLoop(); return(0); }