L'exécutable

Lumieres1OpenGL.jpg (23416 octets)

Lumieres1aOpenGL.jpg (18620 octets)

Lumieres1aOpenGL.jpg (18620 octets)

Lumieres1aOpenGL.jpg (18620 octets)

Le source : TD-Lumieres.cpp

#include <windows.h>

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

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

static float anglex = 0.0F ;
static float angley = 0.0F ;
static int scene = 0 ;

static float noir[] = { 0.0F,0.0F,0.0F,1.0F };
static float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static float vert[] = { 0.0F,1.0F,0.0F,1.0F };
static float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
static float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static float magenta[] = { 1.0F,0.0F,1.0F,1.0F };
static float cyan[] = { 0.0F,1.0F,1.0F,1.0F };
static float blanc[] = { 1.0F,1.0F,1.0F,1.0F };

void CALLBACK display(void) {
  glClearColor(0.0F,0.0F,0.0F,0.0F);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  switch ( scene ) {
    case 0 : glEnable(GL_LIGHT0);
             glEnable(GL_LIGHT1);
             glEnable(GL_LIGHT2);
             break;
    case 1 : glEnable(GL_LIGHT0);
             glDisable(GL_LIGHT1);
             glDisable(GL_LIGHT2);
             break;
    case 2 : glDisable(GL_LIGHT0);
             glEnable(GL_LIGHT1);
             glDisable(GL_LIGHT2);
             break;
    case 3 : glDisable(GL_LIGHT0);
             glDisable(GL_LIGHT1);
             glEnable(GL_LIGHT2);
             break; }
  glPushMatrix();
  glRotatef(angley,0.0F,1.0F,0.0F) ;
  glRotatef(anglex,1.0F,0.0F,0.0F) ;
  glPushMatrix();
  glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,noir) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,cyan) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,rouge) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,noir) ;
  glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,100) ;
  glTranslatef(-2.0F,0.0F,0.0F) ;
  glutSolidSphere(1.0,50,50) ;
  glPopMatrix();
  glPushMatrix();
  glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,noir) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,magenta) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,noir) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,noir) ;
  glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0) ;
  glTranslatef(2.0F,0.0F,0.0F) ;
  glRotatef(-90.0f,1.0f,0.0f,0.0f);
  glTranslatef(0.0f,0.0f,-1.0f);
  glutSolidCone(1.0,2.0,50,50) ;
  glPopMatrix();
  glPopMatrix();
  glFlush();
  auxSwapBuffers();
}

void myinit (void) {
  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHTING);
  float dir0[] = { -1.0f,0.0f,1.0f,0.0f } ;
  glLightfv(GL_LIGHT0,GL_DIFFUSE,cyan) ;
  glLightfv(GL_LIGHT0,GL_SPECULAR,cyan) ;
  glLightfv(GL_LIGHT0,GL_AMBIENT,noir) ;
  glLightfv(GL_LIGHT0,GL_POSITION,dir0) ;
  float pos1[] = { 4.0f,0.0f,0.0f,1.0f } ;
  glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune) ;
  glLightfv(GL_LIGHT1,GL_SPECULAR,jaune) ;
  glLightfv(GL_LIGHT1,GL_AMBIENT,noir) ;
  glLightfv(GL_LIGHT1,GL_POSITION,pos1) ;
  float pos2[] = { -2.0f,0.0f,5.0f,1.0f } ;
  float dir2[] = { 0.0f,0.0f,-1.0f,0.0f } ;
  glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu) ;
  glLightfv(GL_LIGHT2,GL_SPECULAR,bleu) ;
  glLightfv(GL_LIGHT2,GL_AMBIENT,noir) ;
  glLightfv(GL_LIGHT2,GL_POSITION,pos2) ;
  glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,dir2) ;
  glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,0.15*180.0/3.14159) ;
}

void CALLBACK myReshape(int w, int h) {
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-3.0,3.0,-3.0/w*h,3.0/w*h,-3.0,3.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void CALLBACK up(void) {
  anglex++ ;
}

void CALLBACK down(void) {
  anglex-- ;
}

void CALLBACK left(void) {
  angley++ ;
}

void CALLBACK right(void) {
  angley-- ;
}

void CALLBACK enter(void) {
  scene = (scene+1)%4 ;
}

void main(void) {
  auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH);
  auxInitPosition (0,0,300,150);
  auxInitWindow("Lumieres et materiaux") ;
  myinit();
  auxKeyFunc(AUX_UP,up) ;
  auxKeyFunc(AUX_DOWN,down) ;
  auxKeyFunc(AUX_LEFT,left) ;
  auxKeyFunc(AUX_RIGHT,right) ;
  auxKeyFunc(AUX_RETURN,enter) ;
  auxReshapeFunc(myReshape);
  auxMainLoop(display);
}
WB01624_.gif (281 octets) RETOUR