Le source : BrasRobotOpenGL.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 pour l'activation */
/* de la gestion des ombrages, */
/* du dessin en fil de fer, */
/* du dessin avec des cylindres */
static int l = 1;
static int ff = 0;
static int sc = 0;
/* 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(30,-60,50,0.2);
break;
case 1 : brasRobot02(30,-60,50,0.2);
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();
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 du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
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 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,-1.0,1.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");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}