L'exécution d'une fonction événement entraine
l'affichage dans la console d'une lettre différente suivant la fonction.
La fonction reshape assure un affichage en projection orthographique.
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Janvier 2004 */
/* Un programme d'illustration */
/* des fonctionnalites principales de GLUT */
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
static float r1 = 0.0F;
static float r2 = 0.0F;
static float r3 = 0.0F;
static float r4 = 0.0F;
static float size = 12.0F;
static float disc = 1.0F;
static float larg = 1.0F;
static int anim = 1;
static int lumiere = 0;
static int culling = 0;
static int depthCueing = 0;
static GLfloat couleurBlanc[] = { 1.0F,1.0F,1.0F,1.0F };
static GLfloat couleurRouge[] = { 1.0F,0.0F,0.0F,1.0F };
static GLfloat couleurVert[] = { 0.0F,1.0F,0.0F,1.0F };
static GLfloat couleurBleu[] = { 0.0F,0.0F,1.0F,1.0F };
static GLfloat couleurNoir[] = { 0.0F,0.0F,0.0F,1.0F };
void init(void) {
GLfloat mat_shininess[] = { 50.0F };
GLfloat light0_position[] = { 1.0F,1.0F,1.0F,0.0F };
GLfloat light1_position[] = { -1.0F,1.0F,1.0F,0.0F };
GLfloat light2_position[] = { 1.0F,-1.0F,1.0F,0.0F };
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurRouge);
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurVert);
glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBleu);
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
glFogi(GL_FOG_MODE,GL_LINEAR);
glHint(GL_FOG_HINT,GL_NICEST);
glFogf(GL_FOG_START,2.0F);
glFogf(GL_FOG_END,23.0F);
glFogfv(GL_FOG_COLOR,couleurNoir);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glClearColor(0.0F,0.0F,0.0F,1.0F);
}
void scene(void) {
int fac =(int) (72*disc);
glPushMatrix();
glScalef(larg,1.0F,1.0F);
glRotatef(r1,0.0F,1.0F,0.0F);
glColor4fv(couleurBlanc);
glutSolidTorus(0.7,10.0,fac,fac);
glRotatef(r2,1.0F,0.0F,0.0F);
glColor4fv(couleurRouge);
glutSolidTorus(0.7,8.6F,fac,fac);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glRotatef(r3,1.0F,0.0F,0.0F);
glColor4fv(couleurVert);
glutSolidTorus(0.7,7.2F,fac,fac);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glRotatef(r4,1.0F,0.0F,0.0F);
glColor4fv(couleurBleu);
glutSolidTorus(0.7,5.8F,fac,fac);
glPopMatrix();
}
void display(void) {
printf("D");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if ( lumiere )
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
if ( culling )
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
if ( depthCueing )
glEnable(GL_FOG);
else
glDisable(GL_FOG);
glPushMatrix();
glTranslatef(0.0F,0.0F,-15.0F);
scene();
glPopMatrix();
glFlush();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("\nAttention erreur %d\n",error);
glutSwapBuffers();
}
void idle(void) {
printf("I");
r1 += 2.1137F ;
r2 += 3.174F ;
r3 += 4.715F ;
r4 += 6.433F ;
glutPostRedisplay() ;
}
void special(int k,int x,int y) {
printf("S\n");
switch (k) {
case GLUT_KEY_F1 : glEnable(GL_DIFFUSE);
break;
case GLUT_KEY_F2 : depthCueing = !depthCueing;
glutPostRedisplay();
break;
case GLUT_KEY_UP : disc *= 1.01F;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : disc /= 1.01F;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT : larg *= 1.01F;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : larg /= 1.01F;
glutPostRedisplay();
break; }
}
void key(unsigned char key,int x,int y) {
printf("K\n");
switch ( key ) {
case 32 : lumiere = !lumiere;
glutPostRedisplay();
break;
case 0x0D : culling = !culling;
glutPostRedisplay();
break;
case 27 : exit(0);
break; }
}
void souris(int bouton,int etat,int x,int y) {
printf("S\n");
if ( ( bouton == GLUT_LEFT_BUTTON ) && ( etat == GLUT_UP ) )
anim = !anim;
glutIdleFunc((anim) ? idle : NULL);
}
void motion(int x,int y) {
printf("M\n");
}
void passive(int x,int y) {
printf("P\n");
}
void reshape(int w,int l) {
printf("R\n");
glViewport(0,0,w,l);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( w > l )
glOrtho((float) -w/l*size,(float) w/l*size,
-size,size,
-size+15.0F,size+15.0F);
else
glOrtho(-size,size,
(float) -l/w*size,(float) l/w*size,
-size+15.0F,size+15.0F);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void select(int selection) {
switch (selection) {
case 0 : exit(0); }
glutPostRedisplay();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Interface utilisateur en GLUt");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutMotionFunc(motion);
glutPassiveMotionFunc(passive);
glutMouseFunc(souris);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutCreateMenu(select);
glutAddMenuEntry("Quitter",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return(0);
}