L'exécutable

GestionLumiere01-01.gif (12635 octets) 

 

 

 

 

 

Le source : GestionLumiere01.cpp

/* Auteur: Nicolas JANEY            */
/* nicolas.janey@univ-fcomte.fr     */
/* Octobre 2006                     */
/* Gestion des lumieres en OpenGL   */

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

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

#include "ModuleCylindres.h"
#include "ModuleFont.h"

static int f1;
static int f2;

static int tl = 1;
static int to = 0;
static float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.0F;
static float e = 1.0F;
static float c[4] = { 1.0F,1.0F,1.0F,1.0F };
static GLfloat lp[4] = { 0.0F,0.0F,0.0F,1.0F };
static GLfloat ld[4] = { 0.0F,0.0F,1.0F,0.0F };
static int anim = 1;

void postRedisplay(void) {
  glutPostWindowRedisplay(f1);
  glutPostWindowRedisplay(f2);
}

void init(void) {
  GLfloat c[4] = { 1.0F,1.0F,1.0F,1.0F };
  glMaterialfv(GL_FRONT,GL_DIFFUSE,c);
  glMaterialfv(GL_FRONT,GL_SPECULAR,c);
  glMaterialf(GL_FRONT,GL_SHININESS,15.0F);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glColor4fv(c) ;
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
}

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

void solidElement() {
  glPushMatrix();
  glTranslatef(2.0F,0.0F,2.0F);
  solidCylindre(0.25F,3.0F,72,20);
  for ( int i = 0 ; i < 2 ; i++ ) {
    glTranslatef(0.0F,2.0F,0.0F);
    solidCube(1.0F,1.0F,1.0F);
    glRotatef(90.0F,0.0F,0.0F,1.0F);
    glTranslatef(0.0F,2.0F,0.0F);
    solidCylindre(0.25F,3.0F,72,20); }
  glPopMatrix();
}

void solidScene() {
  glPushMatrix();
  solidElement();
  for ( int i = 1 ; i < 4 ; i++ ) {
    glPushMatrix();
    glRotatef(90.0F*i,1.0F,0.0F,0.0F);
    solidElement();
    glPopMatrix(); }
  glPopMatrix();
}

void scene1(void) {
  glPushMatrix();
  solidScene();
  glPopMatrix();
}

void scene2(void) {
  glPushMatrix();
  for ( int i = 0 ; i < 4 ; i++ )
    for ( int j = 0 ; j < 4 ; j++ )
      for ( int k = 0 ; k < 4 ; k++ ) {
        float x = -2.0F + i*1.3333F;
        float y = -2.0F + j*1.3333F;
        float z = -2.0F + k*1.3333F;
        glPushMatrix();
        glTranslatef(x,y,z);
        glutSolidSphere(0.3,18,18);
        glPopMatrix(); }
  glPopMatrix();
}

void display(void) {
  glClearColor(0.8F,0.8F,0.8F,1.0F) ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
  glPushMatrix();
  glLightfv(GL_LIGHT0,GL_POSITION,(tl) ? lp : ld);
  float nrg[4] = { e*c[0],e*c[1],e*c[2],1.0F };
  glLightfv(GL_LIGHT0,GL_DIFFUSE,nrg);
  glLightfv(GL_LIGHT0,GL_SPECULAR,nrg);
  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 (to) {
    case 0 : scene1();
             break;
    case 1 : scene2();
             break; }
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention, erreur OpenGL %d\n",error);
}

void reshape(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(27.0,(double) tx/ty,15.0,25.0) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

void idle(void) {
  rx += 1.1F;
  ry += 1.4F;
  rz += 1.7F;
  postRedisplay();
}

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case 'r'  : c[0] -= 0.01F;
                if ( c[0] < 0.0F )
                  c[0] = 0.0F;
                postRedisplay();
                break;
    case 'R'  : c[0] += 0.01F;
                if ( c[0] > 1.0F )
                  c[0] = 1.0F;
                postRedisplay();
                break;
    case 'v'  : c[1] -= 0.01F;
                if ( c[1] < 0.0F )
                  c[1] = 0.0F;
                postRedisplay();
                break;
    case 'V'  : c[1] += 0.01F;
                if ( c[1] > 1.0F )
                  c[1] = 1.0F;
                postRedisplay();
                break;
    case 'b'  : c[2] -= 0.01F;
                if ( c[2] < 0.0F )
                  c[2] = 0.0F;
                postRedisplay();
                break;
    case 'B'  : c[2] += 0.01F;
                if ( c[2] > 1.0F )
                  c[2] = 1.0F;
                postRedisplay();
                break;
    case 'x'  : if ( tl == 1 )
                  lp[0] += 0.1F;
                  else
                  ld[0] += 0.1F;
                postRedisplay();
                break;
    case 'X'  : if ( tl == 1 )
                  lp[0] -= 0.1F;
                  else
                  ld[0] -= 0.1F;
                postRedisplay();
                break;
    case 'y'  : if ( tl == 1 )
                  lp[1] += 0.1F;
                  else
                  ld[1] += 0.1F;
                postRedisplay();
                break;
    case 'Y'  : if ( tl == 1 )
                  lp[1] -= 0.1F;
                  else
                  ld[1] -= 0.1F;
                postRedisplay();
                break;
    case 'z'  : if ( tl == 1 )
                  lp[2] += 0.1F;
                  else
                  ld[2] += 0.1F;
                postRedisplay();
                break;
    case 'Z'  : if ( tl == 1 )
                  lp[2] -= 0.1F;
                  else
                  ld[2] -= 0.1F;
                postRedisplay();
                break;
    case 45   : e /= 1.01F;
                postRedisplay();
                break;
    case 43   : e *= 1.01F;
                postRedisplay();
                break;
    case ' '  : anim = !anim;
                glutIdleFunc((anim) ? idle : NULL);
                break;
    case 'o'  : to = !to ;
                postRedisplay();
                break;
    case 0x0D : tl = !tl ;
                postRedisplay();
                break;
    case 0x1B : exit(0); }
}

void reshape2(int w,int h) {
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,w,-h,0,-1.0,1.0); 
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void display2() {
  glClearColor(0.0F,0.0F,0.0F,1.0F);
  glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  float pos = 1.0F;
  if ( tl ) {
    placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
    simpleBitmapOutput(1,REGULAR8x13,"Position  : %6.2f %6.2f %6.2f",lp[0],lp[1],lp[2]) ; }
    else {
    placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
    simpleBitmapOutput(1,REGULAR8x13,"Direction : %6.2f %6.2f %6.2f",ld[0],ld[1],ld[2]) ; }
  pos += 1.0F;
  placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
  simpleBitmapOutput(1,REGULAR8x13,"Energie   : %6.2f %6.2f %6.2f",e*c[0],e*c[1],e*c[2]) ;
  glPopMatrix();
  glutSwapBuffers();
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
  glutInitWindowSize(250,250); 
  glutInitWindowPosition(50,50); 
  f1 = glutCreateWindow("Gestion lumiere");
  init(); 
  glutIdleFunc(idle);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutDisplayFunc(display);
  glutInitWindowSize(330,50);
  glutInitWindowPosition(60,340);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  f2 = glutCreateWindow("Valeurs");
  glutDisplayFunc(display2);
  glutReshapeFunc(reshape2);
  glutKeyboardFunc(key);
  glutMainLoop();
  return(0);
}

RETOUR