L'exécutable

Rapprochement de la caméra en direction de la scène.

 

Le source : Benzene5.cpp

/* Une molecule de benzene : C6H6               */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Septembre 2009                               */

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

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

/* Variables et constantes globales             */
/* pour les angles et les couleurs utilises     */

static float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.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 };

/* Fonction d'initialisation des parametres     */
/* OpenGL ne changeant pas au cours de la vie   */
/* du programme                                 */

void init(void) {
  const GLfloat shininess[] = { 50.0 };
  glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
  glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune);
  glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu);
  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                               */

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 scene() {
  glPushMatrix();
  for ( int i = 0 ; i < 6 ; i++ ) {
    float angle = i*60.0F;
    glPushMatrix();
    glRotatef(angle,0.0F,0.0F,1.0F);
    glTranslatef(2.0F,0.0F,0.0F);
    glutSolidSphere(0.5,36,36);
    if ( i%2 == 0 ) {
      glPushMatrix();
      glRotatef(30.0,0.0F,0.0F,1.0F);
      glTranslatef(0.0F,1.0F,0.0F);
      cylindre(2.0,0.05,20,20);
      glPopMatrix(); }
      else {
      glPushMatrix();
      glRotatef(30.0,0.0F,0.0F,1.0F);
      glPushMatrix();
      glTranslatef(0.1F,1.0F,0.0F);
      cylindre(2.0,0.05,20,20);
      glPopMatrix();
      glPushMatrix();
      glTranslatef(-0.1F,1.0F,0.0F);
      cylindre(2.0,0.05,20,20);
      glPopMatrix();
      glPopMatrix(); }
    glTranslatef(0.6F,0.0F,0.0F);
    glPushMatrix();
    glRotatef(90.0,0.0F,0.0F,1.0F);
    cylindre(1.2,0.05,20,20);
    glPopMatrix();
    glTranslatef(0.6F,0.0F,0.0F);
    glutSolidSphere(0.25,36,36);
    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();
  glRotatef(rx,1.0F,0.0F,0.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  glRotatef(rz,0.0F,0.0F,1.0F);
  scene();
  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 idle(void) {
  rx += 0.4256F + (rand()%1000)/1000.0F ;
  ry += 0.6117F ;
  rz += 0.4174F ;
  glutPostRedisplay() ;
}

/* 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(80.0F,(float) x/y,1.0,40.0) ;
  glMatrixMode(GL_MODELVIEW) ;
  glLoadIdentity() ;
  gluLookAt(0.0,0.0,6.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 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("Une molecule de benzene en rotation"); 
  init();
  glutKeyboardFunc(keyboard);
  glutSpecialFunc(special);
  glutReshapeFunc(reshape);
  glutIdleFunc(idle);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR