/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2008 */ /* Un programme OpenGL de placement */ /* d'une camera au sein d'une scene */ #include #include #include #include #include #include /* Variables globales */ static double dist; /* Fonction de modelisation de la scene */ void element(GLfloat *c1,GLfloat *c2,GLfloat *c3) { glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,c1); glutSolidTorus(0.75,10.0,90,90); glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,c2); glTranslatef(6.0F,0.0F,0.0F); glScalef(12.0F,1.5F,1.5F); glutSolidCube(1.0); glPopMatrix(); glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,c3); glTranslatef(-6.0F,0.0F,0.0F); glScalef(12.0F,1.5F,1.5F); glutSolidCube(1.0); glPopMatrix(); glPopMatrix(); } void scene(void) { const GLfloat l_pos0[] = { 1.0F,1.0F,1.0F,0.0F }; const GLfloat l_pos1[] = { -1.0F,1.0F,1.0F,0.0F }; const GLfloat blanc[4] = { 1.0F,1.0F,1.0F,1.0F }; const GLfloat gris[4] = { 0.6F,0.6F,0.6F,1.0F }; const GLfloat rouge[4] = { 1.0F,0.0F,0.0F,1.0F }; const GLfloat vert[4] = { 0.0F,1.0F,0.0F,1.0F }; const GLfloat bleu[4] = { 0.0F,0.0F,1.0F,1.0F }; const GLfloat jaune[4] = { 1.0F,1.0F,0.0F,1.0F }; const GLfloat cyan[4] = { 0.0F,1.0F,1.0F,1.0F }; const GLfloat magenta[4] = { 1.0F,0.0F,1.0F,1.0F }; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glLightfv(GL_LIGHT0,GL_POSITION,l_pos0); glLightfv(GL_LIGHT1,GL_POSITION,l_pos1); glLightfv(GL_LIGHT0,GL_DIFFUSE,gris); glLightfv(GL_LIGHT1,GL_DIFFUSE,gris); glPushMatrix(); element((GLfloat *) blanc,(GLfloat *) rouge,(GLfloat *) jaune); glPopMatrix(); glPushMatrix(); glRotatef(90.0F,1.0F,0.0F,0.0F); glRotatef(90.0F,0.0F,0.0F,1.0F); element((GLfloat *) blanc,(GLfloat *) vert,(GLfloat *) cyan); glPopMatrix(); glPushMatrix(); glRotatef(90.0F,0.0F,1.0F,0.0F); glRotatef(90.0F,0.0F,0.0F,1.0F); element((GLfloat *) blanc,(GLfloat *) bleu,(GLfloat *) magenta); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.6F,0.6F,0.6F,1.0F) ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ; /* Affichage de la scene */ glPushMatrix(); glTranslatef(-100.0F,-100.0F,-100.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 changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection en perspective */ void reshape(int tx,int ty) { dist = 100.0*sqrt(3.0); glViewport(0,0,tx,ty); double ratio =(double) tx/ty; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(8.0,ratio,dist-15.0,dist+15.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(-54.76F,-1.0,1.0,0.0); } /* Fonction executee lors de la frappe */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x1B : exit(0); } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(250,250); glutInitWindowPosition(50,50); glutCreateWindow("Placement de camera"); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return(0); }