Fichier source : MaterielEtLumieres.cpp
/* Materiel et lumieres */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Fevrier 2018 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Constantes globales */
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float gris[] = { 0.5F,0.5F,0.5F,1.0F };
static const float noir[] = { 0.0F,0.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 };
static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static const float cyan[] = { 0.0F,1.0F,1.0F,1.0F };
static const float magenta[] = { 1.0F,0.0F,1.0F,1.0F };
/* Variables globales */
static int question = 0; // Question resolue
static int cFond = 0; // Numero de la couleur de fond (0: gris, 1: blanc, 2:noir)
static float r1 = 0.0F; // Angle de rotation de la scene sur elle-meme
static float r2 = 0.0F; // Angle de rotation de la scene sur elle-meme
static int anim = 1; // Flag d'action/desactivation de l'animation
static int pMode = 1; // Flag de switch entre modes d'affichage wireframe et fill
static int wTx = 640; // Resolution horizontale de la fenetre
static int wTy = 480; // Resolution verticale de la fenetre
static int wPx = 50; // Position horizontale de la fenetre
static int wPy = 50; // Position verticale de la fenetre
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
static void init(void) {
const GLfloat shininess[] = { 50.0 };
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
}
/* Scene dessinee */
#ifndef M_PI
#define M_PI 3.14159
#endif
static void solidCylindre(double rayon,double hauteur,int ns,int nl,int bases) {
glPushMatrix();
for ( int j = 0 ; j < nl ; j++ ) {
float hi = hauteur/2-j*hauteur/nl;
float hf = hi-hauteur/nl;
glBegin(GL_QUAD_STRIP);
for( int i = 0 ; i <= ns ; i++ ) {
float a = (2*M_PI*i)/ns;
float cs = cos(a);
float sn = -sin(a);
glNormal3f(cs,0.0F,sn);
float x = rayon*cs;
float z = rayon*sn;
glVertex3f(x,hi,z);
glVertex3f(x,hf,z); }
glEnd(); }
if ( bases ) {
glBegin(GL_POLYGON);
glNormal3f(0.0F,1.0F,0.0F);
for( int i = 0 ; i < ns ; i++ ) {
float a = (2*M_PI*i)/ns;
float cs = cos(a);
float sn = -sin(a);
float x = rayon*cs;
float z = rayon*sn;
glVertex3f(x,hauteur/2.0F,z); }
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0F,-1.0F,0.0F);
for( int i = 0 ; i < ns ; i++ ) {
float a = (2*M_PI*i)/ns;
float cs = cos(a);
float sn = sin(a);
float x = rayon*cs;
float z = rayon*sn;
glVertex3f(x,-hauteur/2.0F,z); }
glEnd(); }
glPopMatrix();
}
static void element() {
glPushMatrix();
glTranslatef(-5.0F,0.0F,5.0F);
solidCylindre(0.8F,15.0F,72,50,1);
for ( int i = 0 ; i < 2 ; i++ ) {
glTranslatef(0.0F,5.0F,0.0F);
glutSolidSphere(2.0,36,36);
glRotatef(-90.0F,0.0F,0.0F,1.0F);
glTranslatef(0.0F,5.0F,0.0F);
solidCylindre(0.8F,15.0F,72,50,1); }
glPopMatrix();
}
static void scene() {
glPushMatrix();
for ( int i = 0 ; i < 4 ; i++ ) {
glPushMatrix();
glRotatef(90.0F*i,1.0F,0.0F,0.0F);
element();
glPopMatrix(); }
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
static void reset() {
float diffuse[] = { 0.8F,0.8F,0.8F,0.0F };
float pos[] = { 0.0F,0.0F,1.0F,0.0F };
float spotDir[] = { 0.0F,0.0F,-1.0F };
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHT1);
glDisable(GL_LIGHT2);
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir);
glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
glLightfv(GL_LIGHT0,GL_SPECULAR,blanc);
glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
glMaterialf(GL_FRONT,GL_SHININESS,0.0F);
}
static void lumieresEtMateriel(void) {
reset();
switch (question) {
case 0 : {
}
break;
case 1 : {
glEnable(GL_LIGHTING); }
break;
case 2 : {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0); }
break;
case 3 : {
float pos[] = { -1.0F,0.0F,0.0F,0.0F };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,pos); }
break;
case 4 : {
float pos[] = { 15.0F,15.0F,15.0F,1.0F };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,pos); }
break;
case 5 : {
float pos[] = { -15.0F,15.0F,15.0F,1.0F };
float spotDir[] = { 1.0F,-1.0F,-1.0F };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,10.0);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir); }
break;
case 6 : {
float pos[] = { 15.0F,15.0F,15.0F,1.0F };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge); }
break;
case 7 : {
float pos[] = { 15.0F,15.0F,15.0F,1.0F };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
glLightfv(GL_LIGHT0,GL_SPECULAR,blanc);
glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge);
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialf(GL_FRONT,GL_SHININESS,60.0F); }
break;
case 8 : {
float pos0[] = { -1.0F,0.0F,0.0F,0.0F };
float pos1[] = { 15.0F,15.0F,15.0F,1.0F };
float pos2[] = { -15.0F,15.0F,15.0F,1.0F };
float spotDir[] = { 1.0F,-1.0F,-1.0F };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glLightfv(GL_LIGHT0,GL_POSITION,pos0);
glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
glLightfv(GL_LIGHT0,GL_SPECULAR,blanc);
glLightfv(GL_LIGHT1,GL_POSITION,pos1);
glLightfv(GL_LIGHT1,GL_DIFFUSE,vert);
glLightfv(GL_LIGHT1,GL_SPECULAR,blanc);
glLightfv(GL_LIGHT2,GL_POSITION,pos2);
glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu);
glLightfv(GL_LIGHT2,GL_SPECULAR,blanc);
glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,10.0);
glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,spotDir);
glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialf(GL_FRONT,GL_SHININESS,60.0F); }
break; }
}
static void display(void) {
printf("D %d\n",question);
const float *fond;
switch (cFond) {
case 0 :
fond = gris;
break;
case 1 :
fond = blanc;
break;
case 2 :
fond = noir;
break; }
glClearColor(fond[0],fond[1],fond[2],fond[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK,(pMode == 1) ? GL_FILL : GL_LINE);
glPushMatrix();
lumieresEtMateriel();
glRotatef(r1,1.0F,2.0F,3.0F);
glRotatef(r2,-1.0F,2.0F,-3.0F);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention erreur %d\n",error);
}
/* Fonction executee lorsqu'aucun evenement */
/* n'est en file d'attente */
static void idle(void) {
printf("I\n");
r1 += 1.1355F;
r2 += 0.6755F;
glutPostRedisplay();
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
static void reshape(int wx,int wy) {
printf("R\n");
wTx = wx;
wTy = wy;
glViewport(0,0,wx,wy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( wy < wx )
gluPerspective(70.0,(double) wx/wy,20.0-12.0,20.0+12.0);
else
gluPerspective(70.0*((double) wy/wx),(double) wx/wy,20.0-12.0,20.0+12.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,20.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 */
static void keyboard(unsigned char key,int x,int y) {
printf("K\n");
switch (key) {
case 0x0D :
{ question = (question+1)%9;
glutPostRedisplay(); }
break;
case 'a' :
{ anim = !anim;
glutIdleFunc(( anim ) ? idle : NULL); }
break;
case 'z' :
{ pMode = !pMode;
glutPostRedisplay(); }
break;
case 'f' :
{ cFond = (cFond+1)%3;
glutPostRedisplay(); }
break;
case 'F' :
{ static int full = 0;
static int wFx;
static int wFy;
full = !full;
if ( full ) {
wFx = glutGet(GLUT_WINDOW_WIDTH);
wFy = glutGet(GLUT_WINDOW_HEIGHT);
wPx = glutGet(GLUT_WINDOW_X);
wPy = glutGet(GLUT_WINDOW_Y);
glutFullScreen(); }
else {
glutPositionWindow(wPx,wPy);
glutReshapeWindow(wFx,wFy); } }
break;
case 0x1B :
exit(0);
break; }
}
/* Fonction executee lors de l'appui */
/* d'une touche speciale du clavier : */
/* - touches de curseur */
/* - touches de fonction */
static void special(int specialKey,int x,int y) {
printf("S\n");
switch(specialKey) {
case GLUT_KEY_UP :
r1 += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN :
r1 -= 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT :
r2 += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT :
r2 -= 1.0F;
glutPostRedisplay();
break; }
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(wTx,wTy);
glutInitWindowPosition(wPx,wPy);
glutCreateWindow("Materiel et lumières");
init();
glutSpecialFunc(special);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutIdleFunc((anim) ? idle : NULL);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}