/* Cylindre modelise par facettes */ /* avec materiel et eclairage */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2010 */ #include #include #include #include #include #include /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ #ifndef M_PI #define M_PI 3.14159 #endif static int scn = 0; static float rx = 0.0F; static float ry = 0.0F; static float rz = 0.0F; static float p = 2.0F; static float r = 0.0F; static float ouv = 10.0F; static const float noir[] = { 0.0F,0.0F,0.0F,1.0F }; static const float grisClair[] = { 0.7F,0.7F,0.7F,1.0F }; static const float grisFonce[] = { 0.3F,0.3F,0.3F,1.0F }; static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static const float vert[] = { 0.0F,1.0F,0.0F,1.0F }; static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; static const float bleuatre[] = { 0.4F,0.4F,1.0F,1.0F }; static const float jaunatre[] = { 0.6F,0.6F,0.0F,1.0F }; static const float jauneClair[] = { 1.0F,1.0F,0.7F,1.0F }; static int n = 72; static int m = 72; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ void init(void) { glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); glEnable(GL_LIGHTING); } /* Scene dessinee */ void mySolidCylinder(int n,int m) { glPushMatrix(); for ( int j = 0 ; j < m ; j++ ) { float hi = 1.0F-j*2.0F/m; float hf = hi-2.0F/m; glBegin(GL_QUAD_STRIP); for( int i = 0 ; i <= n ; i++ ){ float a = (2*M_PI*i)/n; float sn = -sin(a); float cs = cos(a); glNormal3f(cs,0.0F,sn); glVertex3f(cs,hi,sn); glVertex3f(cs,hf,sn); } glEnd(); } glPopMatrix(); } void scene() { glPushMatrix(); mySolidCylinder(n,m); glPopMatrix(); } void affichageLumiere(void) { switch ( scn ) { case 2 : { glDisable(GL_LIGHTING); glutSolidSphere(0.05,36,36); glEnable(GL_LIGHTING); } break; case 3 : { const GLfloat pos[] = { 1.0,0.0,1.0,1.0 }; glPushMatrix(); glDisable(GL_LIGHTING); glTranslatef(pos[0],pos[1],pos[2]); glutSolidSphere(0.05,36,36); glEnable(GL_LIGHTING); glPopMatrix(); } break; case 4 : { const GLfloat pos[] = { 2.0,0.0,2.0,1.0 }; const GLfloat dir[] = { -1.0,0.0,-1.0 }; glPushMatrix(); glDisable(GL_LIGHTING); glTranslatef(pos[0],pos[1],pos[2]); glutSolidSphere(0.05,36,36); glBegin(GL_LINES); glVertex3f( 0.0F,0.0F, 0.0F); glVertex3f(-0.4F,0.0F,-0.4F); glEnd(); glEnable(GL_LIGHTING); glPopMatrix(); } break; case 5 : { const GLfloat pos[] = { p,0.0,p,1.0 }; glPushMatrix(); glDisable(GL_LIGHTING); glTranslatef(pos[0],pos[1],pos[2]); glutSolidSphere(0.05,36,36); glEnable(GL_LIGHTING); glPopMatrix(); } break; case 6 : { const GLfloat pos[] = { 2.0,0.0,2.0,1.0 }; const GLfloat dir[] = { -1.0,0.0,-1.0 }; glPushMatrix(); glDisable(GL_LIGHTING); glRotatef(r,0.0F,1.0F,0.0F); glTranslatef(pos[0],pos[1],pos[2]); glBegin(GL_LINES); glVertex3f( 0.0F,0.0F, 0.0F); glVertex3f(-0.4F,0.0F,-0.4F); glEnd(); glutSolidSphere(0.05,36,36); glEnable(GL_LIGHTING); glPopMatrix(); } break; } } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.75,0.75,0.75,1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); glRotatef(rz,0.0F,0.0F,1.0F); switch ( scn ) { case 0 : { glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,noir); glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,noir); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,bleuatre); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,jaunatre); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,64.0F); const GLfloat dir[] = { 1.0,0.0,1.0,0.0 }; glLightfv(GL_LIGHT0,GL_POSITION,dir); glLightfv(GL_LIGHT0,GL_DIFFUSE,jauneClair); glLightfv(GL_LIGHT0,GL_SPECULAR,jauneClair); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0F); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0); } break; case 1 : { { glMaterialfv(GL_FRONT,GL_AMBIENT,noir); glMaterialfv(GL_FRONT,GL_EMISSION,noir); glMaterialfv(GL_FRONT,GL_DIFFUSE,bleuatre); glMaterialfv(GL_FRONT,GL_SPECULAR,jaunatre); glMaterialf(GL_FRONT,GL_SHININESS,64.0F); } { glMaterialfv(GL_BACK,GL_AMBIENT,noir); glMaterialfv(GL_BACK,GL_EMISSION,noir); glMaterialfv(GL_BACK,GL_DIFFUSE,grisClair); glMaterialfv(GL_BACK,GL_SPECULAR,grisFonce); glMaterialf(GL_BACK,GL_SHININESS,110.0F); } const GLfloat dir[] = { 1.0,0.0,1.0,0.0 }; glLightfv(GL_LIGHT0,GL_POSITION,dir); glLightfv(GL_LIGHT0,GL_DIFFUSE,jauneClair); glLightfv(GL_LIGHT0,GL_SPECULAR,jauneClair); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0F); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); } break; case 2 : { { glMaterialfv(GL_FRONT,GL_AMBIENT,noir); glMaterialfv(GL_FRONT,GL_EMISSION,noir); glMaterialfv(GL_FRONT,GL_DIFFUSE,bleuatre); glMaterialfv(GL_FRONT,GL_SPECULAR,jaunatre); glMaterialf(GL_FRONT,GL_SHININESS,64.0F); } { glMaterialfv(GL_BACK,GL_AMBIENT,noir); glMaterialfv(GL_BACK,GL_EMISSION,noir); glMaterialfv(GL_BACK,GL_DIFFUSE,grisClair); glMaterialfv(GL_BACK,GL_SPECULAR,grisFonce); glMaterialf(GL_BACK,GL_SHININESS,110.0F); } const GLfloat pos[] = { 0.0,0.0,0.0,1.0 }; glLightfv(GL_LIGHT0,GL_POSITION,pos); glLightfv(GL_LIGHT0,GL_DIFFUSE,jauneClair); glLightfv(GL_LIGHT0,GL_SPECULAR,jauneClair); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0F); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); } break; case 3 : { { glMaterialfv(GL_FRONT,GL_AMBIENT,noir); glMaterialfv(GL_FRONT,GL_EMISSION,noir); glMaterialfv(GL_FRONT,GL_DIFFUSE,bleuatre); glMaterialfv(GL_FRONT,GL_SPECULAR,jaunatre); glMaterialf(GL_FRONT,GL_SHININESS,64.0F); } { glMaterialfv(GL_BACK,GL_AMBIENT,noir); glMaterialfv(GL_BACK,GL_EMISSION,noir); glMaterialfv(GL_BACK,GL_DIFFUSE,grisClair); glMaterialfv(GL_BACK,GL_SPECULAR,grisFonce); glMaterialf(GL_BACK,GL_SHININESS,110.0F); } const GLfloat pos[] = { 1.0,0.0,1.0,1.0 }; glLightfv(GL_LIGHT0,GL_POSITION,pos); glLightfv(GL_LIGHT0,GL_DIFFUSE,jauneClair); glLightfv(GL_LIGHT0,GL_SPECULAR,jauneClair); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0F); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); } break; case 4 : { { glMaterialfv(GL_FRONT,GL_AMBIENT,noir); glMaterialfv(GL_FRONT,GL_EMISSION,noir); glMaterialfv(GL_FRONT,GL_DIFFUSE,bleuatre); glMaterialfv(GL_FRONT,GL_SPECULAR,jaunatre); glMaterialf(GL_FRONT,GL_SHININESS,64.0F); } { glMaterialfv(GL_BACK,GL_AMBIENT,noir); glMaterialfv(GL_BACK,GL_EMISSION,noir); glMaterialfv(GL_BACK,GL_DIFFUSE,grisClair); glMaterialfv(GL_BACK,GL_SPECULAR,grisFonce); glMaterialf(GL_BACK,GL_SHININESS,110.0F); } const GLfloat pos[] = { 2.0,0.0,2.0,1.0 }; const GLfloat dir[] = { -1.0,0.0,-1.0 }; glLightfv(GL_LIGHT0,GL_POSITION,pos); glLightfv(GL_LIGHT0,GL_DIFFUSE,jauneClair); glLightfv(GL_LIGHT0,GL_SPECULAR,jauneClair); glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,dir); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,ouv); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); } break; case 5 : { { glMaterialfv(GL_FRONT,GL_AMBIENT,noir); glMaterialfv(GL_FRONT,GL_EMISSION,noir); glMaterialfv(GL_FRONT,GL_DIFFUSE,bleuatre); glMaterialfv(GL_FRONT,GL_SPECULAR,jaunatre); glMaterialf(GL_FRONT,GL_SHININESS,64.0F); } { glMaterialfv(GL_BACK,GL_AMBIENT,noir); glMaterialfv(GL_BACK,GL_EMISSION,noir); glMaterialfv(GL_BACK,GL_DIFFUSE,grisClair); glMaterialfv(GL_BACK,GL_SPECULAR,grisFonce); glMaterialf(GL_BACK,GL_SHININESS,110.0F); } const GLfloat pos[] = { p,0.0,p,1.0 }; glLightfv(GL_LIGHT0,GL_POSITION,pos); glLightfv(GL_LIGHT0,GL_DIFFUSE,jauneClair); glLightfv(GL_LIGHT0,GL_SPECULAR,jauneClair); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0F); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); } break; case 6 : { { glMaterialfv(GL_FRONT,GL_AMBIENT,noir); glMaterialfv(GL_FRONT,GL_EMISSION,noir); glMaterialfv(GL_FRONT,GL_DIFFUSE,bleuatre); glMaterialfv(GL_FRONT,GL_SPECULAR,jaunatre); glMaterialf(GL_FRONT,GL_SHININESS,64.0F); } { glMaterialfv(GL_BACK,GL_AMBIENT,noir); glMaterialfv(GL_BACK,GL_EMISSION,noir); glMaterialfv(GL_BACK,GL_DIFFUSE,grisClair); glMaterialfv(GL_BACK,GL_SPECULAR,grisFonce); glMaterialf(GL_BACK,GL_SHININESS,110.0F); } glPushMatrix(); const GLfloat pos[] = { 2.0,0.0, 2.0,1.0 }; const GLfloat dir[] = { -1.0,0.0,-1.0 }; glRotatef(r,0.0F,1.0F,0.0F); glLightfv(GL_LIGHT0,GL_POSITION,pos); glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,dir); glPopMatrix(); glLightfv(GL_LIGHT0,GL_DIFFUSE,jauneClair); glLightfv(GL_LIGHT0,GL_SPECULAR,jauneClair); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,ouv); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); } break; } scene(); affichageLumiere(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL\n",error); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en file d'attente */ void idle1(void) { rx += 0.14256F; ry += 0.06117F; rz += 0.04174F; glutPostRedisplay(); } void idle2(void) { static float d = -0.002F; p += d; if ( ( p > 2.0F ) || ( p < -2.0F ) ) d *= -1.0F; idle1(); } void idle3(void) { r += 0.1F ; idle1(); } /* 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() ; gluPerspective(10.0F,(float) x/y,1.0,40.0) ; glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch (key) { case 'o' : ouv -= 0.1F; if ( ouv < 0.0F ) ouv = 0.0F; glutPostRedisplay(); break; case 'O' : ouv += 0.1F; if ( ouv > 90.0F ) ouv = 90.0F; glutPostRedisplay(); break; case 'n' : n--; if ( n < 3 ) n = 3; glutPostRedisplay(); break; case 'N' : n++; glutPostRedisplay(); break; case 'm' : m--; if ( m < 1 ) m = 1; glutPostRedisplay(); break; case 'M' : m++; glutPostRedisplay(); break; case 'c' : { static int culling = 0; culling = !culling; if ( culling ) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); } glutPostRedisplay(); break; case 'f' : { static int face = 1; face = !face; glPolygonMode(GL_FRONT_AND_BACK,( face ) ? GL_FILL : GL_LINE); } glutPostRedisplay(); break; case 0x0D : scn = (scn+1)%7; switch ( scn ) { case 0 : case 1 : case 2 : case 3 : case 4 : glutIdleFunc(idle1); break; case 5 : glutIdleFunc(idle2); break; case 6 : glutIdleFunc(idle3); break; } glutPostRedisplay(); break; case 0x20 : { static int anim = 1; anim = !anim; switch ( scn ) { case 0 : case 1 : case 2 : case 3 : case 4 : glutIdleFunc(( anim ) ? idle1 : NULL); break; case 5 : glutIdleFunc(( anim ) ? idle2 : NULL); break; case 6 : glutIdleFunc(( anim ) ? idle3 : NULL); break; } } break; case 0x1B : exit(0); break; } } /* Fonction executee lors de l'appui */ /* d'une touche de curseur ou d'une touche */ /* page up ou page down */ void special(int key,int x,int y) { switch(key) { case GLUT_KEY_UP : rx++; glutPostRedisplay() ; break; case GLUT_KEY_DOWN : rx--; glutPostRedisplay() ; break; case GLUT_KEY_LEFT : ry++; glutPostRedisplay() ; break; case GLUT_KEY_RIGHT : ry--; glutPostRedisplay() ; break; case GLUT_KEY_PAGE_UP : rz++; glutPostRedisplay() ; break; case GLUT_KEY_PAGE_DOWN : rz--; glutPostRedisplay() ; break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Cylindre par facettes avec matériel et lumière"); init(); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutIdleFunc(idle1); glutDisplayFunc(display); glutMainLoop(); return(0); }