Le source : BrasRobotAnimeOpenGL.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Septembre 2007 */
/* Un programme OpenGL */
/* de dessin d'un bras robot */
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCylindres.h"
/* Variables globales */
static int l = 1;
static int ff = 0;
static int sc = 0;
static float rx = 0.0F;
static float ry = 0.0F;
static float r1 = 0.0F;
static float r2 = 0.0F;
static float r3 = 0.0F;
static float d = 0.0F;
static int clic = 0;
static int mx;
static int my;
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
const GLfloat l_pos[] = { 1.0F,1.0F,1.0F,0.0F };
const GLfloat c[4] = { 0.5F,0.2F,0.6F,1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,c);
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
glEnable(GL_LIGHT0);
glColor4fv(c) ;
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
/* Dessin d'une "boite" parallelipipedique */
/* en fil de fer */
void myWireCube(double w,double h,double d) {
glPushMatrix();
glScalef((float) w,(float) h,(float) d);
glutWireCube(1.0);
glPopMatrix();
}
/* Dessin d'une "boite" parallelipipedique */
void mySolidCube(double w,double h,double d) {
glPushMatrix();
glScalef((float) w,(float) h,(float) d);
glutSolidCube(1.0);
glPopMatrix();
}
/* Dessin d'une "boite" parallelipipedique */
/* en fil de fer ou non suivant la valeur */
/* de la variable globale ff */
void myCube(double w,double h,double d) {
glPushMatrix();
if ( ff )
myWireCube(w,h,d);
else
mySolidCube(w,h,d);
glPopMatrix();
}
/* Dessin d'une sphere en fil de fer ou non */
/* suivant la valeur de la variable globale ff */
void mySphere(double r,int n1,int n2) {
glPushMatrix();
if ( ff )
glutWireSphere(r,n1,n2);
else
glutSolidSphere(r,n1,n2);
glPopMatrix();
}
/* Dessin d'un cylindre en fil de fer ou non */
/* suivant la valeur de la variable globale ff */
void myCylindre(double h,double d) {
glPushMatrix();
glRotatef(90.0F,0.0F,0.0F,1.0F);
if ( ff )
wireCylindre(d/2.0,h,18,10);
else
solidCylindre(d/2.0,h,18,10);
glPopMatrix();
}
/* Une mandibule */
void mandibule(float d) {
glPushMatrix();
glTranslatef(0.0F,0.0F,-d-0.2F) ;
myCube(1.0,1.2,0.4) ;
glPopMatrix();
}
/* Le bras robot a base de cubes */
void brasRobot01(float r1,float r2,float r3,float d) {
glPushMatrix();
glRotatef(r1,0.0F,1.0F,0.0F);
glTranslatef(1.5F,0.0F,0.0F);
myCube(3.0,1.0,1.0);
glTranslatef(1.5F,0.0F,0.0F);
glRotatef(r2,0.0F,1.0F,0.0F);
glTranslatef(1.5F,0.0F,0.0F);
myCube(3.0,0.8,0.8);
glTranslatef(1.8F,0.0F,0.0F) ;
glRotatef(r3,1.0F,0.0F,0.0F) ;
myCube(0.6,1.2,1.8) ;
glTranslatef(0.8F,0.0F,0.0F) ;
mandibule(d);
glRotatef(180.0F,1.0F,0.0F,0.0F);
mandibule(d);
glPopMatrix();
}
/* Le bras robot a base de cylindres */
void brasRobot02(float r1,float r2,float r3,float d) {
glPushMatrix();
glRotatef(r1,0.0F,1.0F,0.0F);
glTranslatef(1.5F,0.0F,0.0F);
myCylindre(3.0,1.0);
glTranslatef(1.5F,0.0F,0.0F);
mySphere(0.7,18,18);
glRotatef(r2,0.0F,1.0F,0.0F);
glTranslatef(1.5F,0.0F,0.0F);
myCylindre(3.0,0.8);
glTranslatef(1.8F,0.0F,0.0F) ;
glRotatef(r3,1.0F,0.0F,0.0F) ;
myCube(0.6,1.2,1.8) ;
glTranslatef(0.8F,0.0F,0.0F) ;
mandibule(d);
glRotatef(180.0F,1.0F,0.0F,0.0F);
mandibule(d);
glPopMatrix();
}
/* Scene dessinee */
void scene(void) {
glPushMatrix();
switch (sc) {
case 0 : brasRobot01(r1,r2,r3,d);
break;
case 1 : brasRobot02(r1,r2,r3,d);
break; }
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClearColor(0.8F,0.8F,0.8F,1.0F) ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
if ( l )
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
glPushMatrix();
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(ry,0.0F,1.0F,0.0F);
glScalef(0.3F,0.3F,0.3F);
glTranslatef(-4.0F,0.0F,0.0F);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
/* Fonction executee lors de la frappe */
/* d'une touche alphanumerique du clavier */
void keyboard(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 < -120.0F )
r2 = -120.0F;
else
glutPostRedisplay();
break;
case '5' : r2 += 1.0F;
if ( r2 > 120.0F )
r2 = 120.0F;
else
glutPostRedisplay();
break;
case '3' : r3 -= 1.0F;
glutPostRedisplay();
break;
case '6' : r3 += 1.0F;
glutPostRedisplay();
break;
case 43 : d += 0.01F;
if ( d > 0.5F )
d = 0.5F;
else
glutPostRedisplay();
break;
case 45 : d -= 0.01F;
if ( d < 0.0F )
d = 0.0F;
else
glutPostRedisplay();
break;
case 's' :
case 'S' : sc = (sc+1)%2 ;
glutPostRedisplay();
break;
case 0x0D : l = !l ;
glutPostRedisplay();
break;
case 0x20 : ff = !ff ;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
/* Fonction executee lors de la frappe */
/* d'une touche non alphanumerique du clavier */
void special(int key,int x,int y) {
switch ( key ) {
case GLUT_KEY_UP : rx -= 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : rx += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT : ry += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : ry -= 1.0F;
glutPostRedisplay();
break; }
}
/* Fonction executee lors d'un clic de souris */
/* dans la fenetre */
void mouse(int bouton,int etat,int x,int y) {
if ( bouton == GLUT_LEFT_BUTTON ) {
if ( etat == GLUT_DOWN ) {
clic = 1;
mx = x;
my = y; }
if ( etat == GLUT_UP ) {
clic = 0; } }
}
/* Fonction executee lors d'un deplacement */
/* de la souris sur la fenetre */
/* avec un bouton appuye */
void motion(int x,int y) {
if ( clic ) {
ry += (x-mx);
rx += (y-my);
mx = x;
my = y;
glutPostRedisplay(); }
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Configuration d'une camera de visualisation */
/* en projection orthographique */
void reshape(int tx,int ty) {
glViewport(0,0,tx,ty);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho((double) -tx/ty,(double) tx/ty,-1.0,1.0,-4.0,4.0) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(500,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Bras robot anime");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}