L'exécutable

Fichier source : DisqueAccretion.cpp

/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Octobre 2010                                 */
/*                                              */
/* Un programme OpenGL d'animation automatique  */
/* d'un ensemble de particules ponctuelles      */
/* selon des orbites circulaires de periodes    */
/* en pow(r,1.5) (orbite keplerienne).          */
/* r est le rayon de l'orbite et decroit        */
/* au cours du temps                            */

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

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

/* Variables globales                           */

static float *r;
static float *v;
static float *a;
static int image = 0;
static int nouvellesEtoilesParImage = 20;
static int imagesParCycle = 1100;
static float rayon = 40.0F;
static float rayonMin = 0.1F;
static int nombreEtoiles = nouvellesEtoilesParImage * imagesParCycle;
static float vitesse = 0.5F;

/* Variables globales de gestion                */
/* de l'interactivite clavier et souris         */

static int clic = 0;
static int mx;
static int my;
static float rx = 0.0F;
static float ry = 0.0F;
static float zoom = 1.0F;

/* Fonction d'affichage d'une eventuelle        */
/* erreur OpenGL                                */

static void checkError(int line) {
  GLenum err = glGetError();
  if (err) {
    printf("GL Error %s (0x%x) at line %d\n",
           gluErrorString(err), (int) err, line); }
}

/* Fonction de calcul d'un nombre reel          */
/* aleatoire compris entre 0.0 et 1.0           */

static float random(void) {
  float v = ((float) rand()/RAND_MAX);
  return(v);
}

/* Fonction de creation d'une particule         */

void creationParticule(int i) {
  r[i] = rayon+random();
  v[i] = 1.0F/pow((double) r[i],1.5);
  a[i] = random()*2*3.14159;
}

/* Fonction de dessin du systeme de particules  */

static void drawDisqueAccretion(void) {
  glPointSize(1.0);
  glBegin(GL_POINTS);
  for ( int i = 0 ; i < nombreEtoiles ; i++ ) {
    if ( i < nouvellesEtoilesParImage*image ) {
      float x = r[i]*cos(a[i]);
      float y = r[i]*sin(a[i]);
      float c = (rayon-r[i])/rayon+0.2F;
      glColor3f(c,c,c);
      glVertex3f(x,y,0.0F); } }
  glEnd();
}

/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin                      */

static void display(void) {
  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);
  glScalef(zoom,zoom,zoom);
  drawDisqueAccretion();
  glPopMatrix();
  glFlush();
  checkError(__LINE__);
  glutSwapBuffers();
}

/* Fonction executee lorsqu'aucun evenement     */
/* n'est en attente de gestion                  */

static void idle(void) {
  int i;
  int ne = (image%imagesParCycle)*nouvellesEtoilesParImage;
  for ( i = ne ; i < ne+nouvellesEtoilesParImage ; i++ )
    creationParticule(i);
  for ( i = 0 ; i < nombreEtoiles ; i++ ) {
    if ( i < nouvellesEtoilesParImage*image ) {
      a[i] += v[i]*vitesse;
      r[i] -= (rayon-rayonMin)/imagesParCycle;
      v[i] = 1.0F/pow((double) r[i],1.5); } }
  image++;
  glutPostRedisplay();
}

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */
/* Configuration d'une camera de visualisation  */
/* en projection en perspective                 */

void reshape(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60,(double) tx/ty,0.1,50.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0F,0.0F,-6.0F);
}

/* Fonction executee lors de la frappe          */
/* d'une touche alphanumerique du clavier       */

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case ' '  : { static int anim = 0;
                  anim = !anim;
                  glutIdleFunc(anim ? idle : NULL); }
                break;
    case 0x1B : free(r);
                free(v);
                free(a);
                exit(0);
                break; }
}

/* Fonction executee lors de la frappe          */
/* d'une touche non alphanumerique du clavier   */

void special(int key,int x,int y) {
  switch ( key ) {
    case GLUT_KEY_UP      : zoom *= 0.99F;
                            glutPostRedisplay();
                            break;
    case GLUT_KEY_DOWN    : zoom /= 0.99F;
                            glutPostRedisplay();
                            break;
    case GLUT_KEY_RIGHT   : vitesse += 0.001F;
                            glutPostRedisplay();
                            break;
    case GLUT_KEY_LEFT    : vitesse -= 0.001F;
                            glutPostRedisplay();
                            break; }
}

/* Fonction d'initialisation de certains        */
/* parametres de l'environnement OpenGL         */
/* et allocation des tableaux de stockage       */
/* des informations relatives aux particules    */

static void init(void) {
  r =(float *) calloc(nombreEtoiles,sizeof(float));
  v =(float *) calloc(nombreEtoiles,sizeof(float));
  a =(float *) calloc(nombreEtoiles,sizeof(float));
  glClearColor(0.0F,0.0F,0.0F,0.0F);
  glEnable(GL_DEPTH_TEST);
}

/* Fonction executee lors d'un clic de souris   */
/* dans la fenetre                              */

void mouse(int bouton,int etat,int x,int y) {
  if ( bouton == GLUT_LEFT_BUTTON ) {
    if ( etat == GLUT_DOWN ) {
      clic = 1;
      mx = x;
      my = y; }
    if ( etat == GLUT_UP ) {
      clic = 0; } }
}

/* Fonction executee lors d'un deplacement      */
/* de la souris sur la fenetre                  */
/* avec un bouton appuye                        */

void motion(int x,int y) {
  if ( clic ) {
    ry += (x-mx);
    rx += (y-my);
    mx = x;
    my = y;
    glutPostRedisplay(); }
}

/* Fonction principale                          */

int main(int argc,char *argv[]) {
  glutInit(&argc,argv);
  glutInitWindowPosition(10,10);
  glutInitWindowSize(480,240);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
  glutCreateWindow("Disque d'accrétion");
  init();
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutSpecialFunc(special);
  glutIdleFunc(idle);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR