Le source : TD-RobotAnimable.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Janvier 2004 */
/* Un bras robot animable au clavier */
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
static float r1 = 10.0F;
static float r2 = 20.0F;
static float r3 = 30.0F;
static float d = 0.2F;
static int lumiere = 0;
static int mode = 0;
static float size = 5.0F;
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 };
static GLfloat couleurJaune[] = { 1.0F,1.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);
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 mandibule(float d) {
glPushMatrix();
glTranslatef(0.0F,0.0F,-d-0.2F) ;
glColor3fv(couleurJaune) ;
glPushMatrix();
glScalef(1.0F,1.2F,0.4F) ;
glutSolidCube(1.0) ;
glPopMatrix();
glPopMatrix();
}
void robot(float r1,float r2,float r3,float d) {
glPushMatrix();
glRotatef(r1,0.0F,1.0F,0.0F) ;
glTranslatef(1.5F,0.0F,0.0F) ;
glColor3fv(couleurRouge) ;
glPushMatrix();
glScalef(3.0F,1.0F,1.0F) ;
glutSolidCube(1.0) ;
glPopMatrix();
glTranslatef(1.5F,0.0F,0.0F) ;
glRotatef(r2,0.0F,1.0F,0.0F) ;
glTranslatef(1.5F,0.0F,0.0F) ;
glColor3fv(couleurVert) ;
glPushMatrix();
glScalef(3.0F,0.8F,0.8F) ;
glutSolidCube(1.0) ;
glPopMatrix();
glTranslatef(1.8F,0.0F,0.0F) ;
glRotatef(r3,1.0F,0.0F,0.0F) ;
glColor3fv(couleurBleu) ;
glPushMatrix();
glScalef(0.6F,1.2F,1.8F) ;
glutSolidCube(1.0) ;
glPopMatrix();
glTranslatef(0.8F,0.0F,0.0F) ;
mandibule(d);
glRotatef(180.0F,1.0F,0.0F,0.0F);
mandibule(d);
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if ( lumiere )
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
glPolygonMode(GL_FRONT_AND_BACK,(mode) ? GL_LINE : GL_FILL);
glPushMatrix();
glRotatef(45.0F,1.0F,1.0F,1.0F);
robot(r1,r2,r3,d);
glPopMatrix();
glFlush();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("\nAttention erreur %d\n",error);
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case '1' : r1 += 1.0F;
glutPostRedisplay();
break;
case '4' : r1 -= 1.0F;
glutPostRedisplay();
break;
case '2' : r2 += 1.0F;
if ( r2 > 90.0F )
r2 = 90.0F;
glutPostRedisplay();
break;
case '5' : r2 -= 1.0F;
if ( r2 < -90.0F )
r2 = -90.0F;
glutPostRedisplay();
break;
case '3' : r3 += 1.0F;
glutPostRedisplay();
break;
case '6' : r3 -= 1.0F;
glutPostRedisplay();
break;
case '7' : d += 0.02F;
if ( d > 0.4F )
d = 0.4F;
glutPostRedisplay();
break;
case '8' : d -= 0.02F;
if ( d < 0.0F )
d = 0.0F;
glutPostRedisplay();
break;
case 32 : lumiere = !lumiere;
glutPostRedisplay();
break;
case 0x0D : mode = !mode;
glutPostRedisplay();
break;
case 27 : exit(0);
break; }
}
void reshape(int w,int l) {
glViewport(0,0,w,l);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( w > l )
glOrtho((float) -w/l*size,(float) w/l*size,
-size,size,
-2*size,2*size);
else
glOrtho(-size,size,
(float) -l/w*size,(float) l/w*size,
-2*size,2*size);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
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);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}