/* Une scene OpenGL simple : un bras robot */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2009 */ #include #include #include #include #include /* Variables et constantes globales */ static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float gris[] = { 0.7F,0.7F,0.7F,1.0F }; static float r1 = 30.0F; static float r2 = -50.0F; static int objet = 0; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ void init(void) { const GLfloat mat_shininess[] = { 50.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,blanc); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,gris); glLightfv(GL_LIGHT1,GL_DIFFUSE,gris); glLightfv(GL_LIGHT2,GL_DIFFUSE,gris); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); } /* Scene dessinee avec des cubes */ void scene5() { glPushMatrix(); glRotatef(r1,0.0F,1.0F,0.0F); glTranslatef(1.5F,0.0F,0.0F); glPushMatrix(); glScalef(3.0F,1.0F,1.0F); glutSolidCube(1.0); glPopMatrix(); glTranslatef(1.5F,0.0F,0.0F); glRotatef(r2,0.0F,1.0F,0.0F); glTranslatef(1.5F,0.0F,0.0F); glPushMatrix(); glScalef(3.0F,0.8F,0.8F); glutSolidCube(1.0); glPopMatrix(); glPopMatrix(); } /* Scene dessinee avec des cylindres */ void cylindre(double h,double r,int n,int m) { glPushMatrix(); glRotatef(90.0F,1.0F,0.0F,0.0F); glTranslatef(0.0F,0.0F,-h/2); GLUquadricObj *qobj = gluNewQuadric(); gluQuadricDrawStyle(qobj,GLU_FILL); gluCylinder(qobj,r,r,h,n,m); gluDeleteQuadric(qobj); glPopMatrix(); } void scene6() { glPushMatrix(); glRotatef(r1,0.0F,1.0F,0.0F); glTranslatef(1.5F,0.0F,0.0F); glPushMatrix(); glRotatef(90.0F,0.0F,0.0F,1.0F); cylindre(3.0,0.5,12,12); glPopMatrix(); glTranslatef(1.5F,0.0F,0.0F); glRotatef(r2,0.0F,1.0F,0.0F); glTranslatef(1.5F,0.0F,0.0F); glPushMatrix(); glRotatef(90.0F,0.0F,0.0F,1.0F); cylindre(3.0,0.4,12,12); glPopMatrix(); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const GLfloat light0_position[] = { 1.0,1.0,1.0,0.0 }; const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 }; const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 }; glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glLightfv(GL_LIGHT1,GL_POSITION,light1_position); glLightfv(GL_LIGHT2,GL_POSITION,light2_position); glPushMatrix(); switch (objet) { case 0 : scene5(); break; case 1 : scene6(); break; } glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL\n",error); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* -> Ajustement de la camera de visualisation */ void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION) ; glLoadIdentity() ; gluPerspective(15.0F,(float) x/y,1.0,50.0) ; glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(2.5,10.0,15.0,2.5,0.5,0.0,0.0,1.0,0.0); } /* Fonction executee lors de la frappe */ /* d'une touche du clavier */ void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : objet = (objet+1)%2; glutPostRedisplay(); 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(450,300); glutInitWindowPosition(50,50); glutCreateWindow("Bras robot partiel"); init(); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }