L'exécutable

Le source : Box.cpp

/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Septembre 2008                               */
/* Simulation d'un accelerateur de particules   */
/* avec cubes modelises par facettes            */

#include <stdio.h>
#include <math.h>

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

/* Variables et constantes globales             */

static const float blanc[] = { 1.0F,1.0F,1.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 int n = 10;

/* 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,rouge);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,vert);
  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);
  glEnable(GL_CULL_FACE);
}

/* Scene dessinee                               */

void box2(float tx,float ty,float tz) {
  glPushMatrix();
  glScalef(tx,ty,tz);
  glutSolidCube(1.0);
  glPopMatrix();
}

void box(float tx,float ty,float tz) {
  tx /= 2.0F;
  ty /= 2.0F;
  tz /= 2.0F;
  glPushMatrix();
  glBegin(GL_QUADS);
  glNormal3f(1.0F,0.0F,0.0F);
  glVertex3f(tx,-ty,-tz);
  glVertex3f(tx,ty,-tz);
  glVertex3f(tx,ty,tz);
  glVertex3f(tx,-ty,tz);
  glNormal3f(-1.0F,0.0F,0.0F);
  glVertex3f(-tx,-ty,tz);
  glVertex3f(-tx,ty,tz);
  glVertex3f(-tx,ty,-tz);
  glVertex3f(-tx,-ty,-tz);
  glNormal3f(0.0F,1.0F,0.0F);
  glVertex3f(tx,ty,tz);
  glVertex3f(tx,ty,-tz);
  glVertex3f(-tx,ty,-tz);
  glVertex3f(-tx,ty,tz);
  glNormal3f(0.0F,-1.0F,0.0F);
  glVertex3f(-tx,-ty,tz);
  glVertex3f(-tx,-ty,-tz);
  glVertex3f(tx,-ty,-tz);
  glVertex3f(tx,-ty,tz);
  glNormal3f(0.0F,0.0F,1.0F);
  glVertex3f(-tx,ty,tz);
  glVertex3f(-tx,-ty,tz);
  glVertex3f(tx,-ty,tz);
  glVertex3f(tx,ty,tz);
  glNormal3f(0.0F,0.0F,-1.0F);
  glVertex3f(tx,-ty,-tz);
  glVertex3f(-tx,-ty,-tz);
  glVertex3f(-tx,ty,-tz);
  glVertex3f(tx,ty,-tz);
  glEnd();
  glPopMatrix();
}

void scene(int n) {
  double a = 2*3.14159/n;
  double cs = 1.0-cos(a);
  double sn = sin(a);
  double d = 10*sqrt(cs*cs+sn*sn);
  glPushMatrix();
  forint i=0 ; i<n ; i++ ) {
    glPushMatrix();
    glRotatef((360.0f/n)*i,0.0F,0.0F,1.0F);
    glTranslatef(10.0F,0.0F,0.0F);
    box(1.0F,1.0F,1.0F);
    glRotatef(180.0F/n,0.0F,0.0F,1.0F);
    glTranslatef(0.0F,d/2,0.0F);
    box(0.5F,d,0.5F);
    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(-50.0F,1.0F,0.0F,0.0F);
  scene(n);
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

/* 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(40.0F,(float) x/y,1.0,50.0) ;
  glMatrixMode(GL_MODELVIEW) ;
  glLoadIdentity() ;
  gluLookAt(0.0,-10.0,25.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

/* 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("Un \"accelerateur de particules\" avec box par facettes"); 
  init();
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR