L'exécutable

ExamTD120062007Exo2-01.gif (18890 octets)

ExamTD120062007Exo2-02.gif (19917 octets)

ExamTD120062007Exo2.cpp

/* Auteur: Nicolas JANEY              */
/* nicolas.janey@univ-fcomte.fr       */
/* Octobre 2006                       */
/* Animation et matériaux en OpenGL   */

#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <stdlib.h>
#include <stdio.h>

static int tl = 1;
static float rx = 0.0F;
static int anim = 1;
static float shininess = 12.0F;

void init(void) {
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
}

static float r = 0.0F;

void scene(void) {
  GLfloat noir[4] = { 0.0F,0.0F,0.0F,1.0F };
  GLfloat blanc[4] = { 1.0F,1.0F,1.0F,1.0F };
  GLfloat jaune[4] = { 1.0F,1.0F,0.0F,1.0F };
  GLfloat bleu[4] = { 0.0F,0.0F,1.0F,1.0F };
  GLfloat gris[4] = { 0.5F,0.5F,0.5F,1.0F };
  glPushMatrix();
  glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune);
  glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
  glutSolidSphere(5.0,72,72);
  glRotatef(r,0.0F,0.0F,1.0F);
  glTranslatef(10.0F,0.0F,0.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,bleu);
  glMaterialf(GL_FRONT,GL_SHININESS,shininess);
  glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
  glutSolidSphere(1.0,72,72);
  glRotatef(12.0F*r,0.0F,0.0F,1.0F);
  glTranslatef(3.0F,0.0F,0.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,gris);
  glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
  glutSolidSphere(0.5,72,72);
  glPopMatrix();
}

void display(void) {
  glClearColor(0.8F,0.8F,0.8F,1.0F) ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
  glPushMatrix();
  { GLfloat l_pos[4] = { 0.0F,0.0F,0.0F,1.0F };
    glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
    GLfloat blanc[4] = { 1.0F,1.0F,1.0F,1.0F };
    glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
    glLightfv(GL_LIGHT0,GL_SPECULAR,blanc); }
  { GLfloat l_pos[4] = { 0.0F,0.0F,1.0F,0.0F };
    glLightfv(GL_LIGHT1,GL_POSITION,l_pos);
    GLfloat blanc[4] = { 1.0F,1.0F,1.0F,1.0F };
    glLightfv(GL_LIGHT1,GL_DIFFUSE,blanc); }
  glRotatef(rx,1.0F,0.0F,0.0F);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention, erreur OpenGL %d\n",error);
}

void idle(void) {
  r += 0.2F;
  glutPostRedisplay();
}

void reshape(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(35.0,(double) tx/ty,10.0,50.0) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0.0,0.0,30.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case 's'  : shininess -= 1.0F;
                if ( shininess < 0.0F )
                  shininess = 0.0F;
                glutPostRedisplay();
                break;
    case 'S'  : shininess += 1.0F;
                if ( shininess > 128.0F )
                  shininess = 128.0F;
                glutPostRedisplay();
                break;
    case ' '  : anim = !anim;
                glutIdleFunc((anim) ? idle : NULL);
                break;
    case 0x0D : tl = !tl ;
                glutPostRedisplay();
                break;
    case 0x1B : exit(0); }
}

void special(int k,int x,int y) {
  switch (k) {
    case GLUT_KEY_UP   : rx++;
                         glutPostRedisplay();
                         break;
    case GLUT_KEY_DOWN : rx--;
                         glutPostRedisplay();
                         break; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
  glutInitWindowSize(300,300); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Animation et materiaux");
  init(); 
  glutIdleFunc(idle);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutSpecialFunc(special);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR