Le source : ModelisationHierarchique.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Decembre 2001 */
/* Modelisation hierarchique d'une scene */
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleFleche.h"
#include "ModuleFont.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#define LI 64
#define LH 64
static int disc = 20;
static int aff = 0;
static int mode = 0;
static int axe = 0;
static int coul = 0;
static float rx = 1.0F;
static float ry = 1.0F;
static float rz = 1.0F;
static float ang = 0.0F;
static GLubyte image[LI][LH][3];
void makeImage(void) {
int i,j,c;
for( i = 0 ; i < LI ; i++ ) {
for( j = 0 ; j < LH ; j++ ) {
c = (((i&0x8)==0)^((j&0x8)==0)) ? 255 : 128;
image[i][j][0] =(GLubyte) c;
image[i][j][1] =(GLubyte) c;
image[i][j][2] =(GLubyte) c; } }
}
void axes(void) {
if ( axe ) {
glDisable(GL_LIGHTING);
setFont(GLUT_BITMAP_8_BY_13,CENTER);
glColor4fv(couleurRouge());
flecheEnVolume(1.0F,0.0F,0.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurVert());
flecheEnVolume(0.0F,1.0F,0.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurBleu());
flecheEnVolume(0.0F,0.0F,1.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurRougeFonce());
bitmapStringOutput(1.3F,0.0F,0.0F,"x");
glColor4fv(couleurVertFonce());
bitmapStringOutput(0.0F,1.3F,0.0F,"y");
glColor4fv(couleurBleuFonce());
bitmapStringOutput(0.0F,0.0F,1.3F,"z");
setFont(GLUT_BITMAP_8_BY_13,LEFT);
glEnable(GL_LIGHTING); }
}
void myinit(void) {
makeImage();
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,3,LI,LH,0,GL_RGB,GL_UNSIGNED_BYTE,&image[0][0][0]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
GLfloat shinines[] = { 100.0F };
GLfloat l_pos0[] = { 3.0F,3.0F,3.0F,1.0F };
GLfloat l_pos1[] = { 4.0F,2.5F,-2.0F,1.0F };
GLfloat l_pos2[] = { -2.0F,2.0F,4.0F,1.0F };
glClearColor(0.5,0.5,1.0,1.0) ;
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurGrisTresFonce());
glMaterialfv(GL_FRONT,GL_SHININESS,shinines);
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurGrisMoyen());
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurGrisTresFonce());
glLightfv(GL_LIGHT0,GL_POSITION,l_pos0);
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurGrisMoyen());
glLightfv(GL_LIGHT1,GL_SPECULAR,couleurGrisTresFonce());
glLightfv(GL_LIGHT1,GL_POSITION,l_pos1);
glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurGrisMoyen());
glLightfv(GL_LIGHT2,GL_SPECULAR,couleurGrisTresFonce());
glLightfv(GL_LIGHT2,GL_POSITION,l_pos2);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
}
void solEtMurs(void){
int i,j;
glPushMatrix();
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisClair());
if ( coul )
glEnable(GL_TEXTURE_2D);
if ( !mode )
glBegin(GL_QUADS);
glNormal3f(0.0F,1.0F,0.0F);
for ( i = 0 ; i < disc ; i++ )
for ( j = 0 ; j < disc ; j++ ) {
if ( mode )
glBegin(GL_LINE_LOOP);
float xi = -5.0F+i*10.0F/disc;
float xf = -5.0F+(i+1)*10.0F/disc;
float yi = -5.0F+j*10.0F/disc;
float yf = -5.0F+(j+1)*10.0F/disc;
glTexCoord2f(xi/2.5F,yi/2.5F);
glVertex3f(xi,0.0F,yi);
glTexCoord2f(xi/2.5F,yf/2.5F);
glVertex3f(xi,0.0F,yf);
glTexCoord2f(xf/2.5F,yf/2.5F);
glVertex3f(xf,0.0F,yf);
glTexCoord2f(xf/2.5F,yi/2.5F);
glVertex3f(xf,0.0F,yi);
if ( mode )
glEnd(); }
if ( !mode )
glEnd();
if ( coul )
glDisable(GL_TEXTURE_2D);
if ( !mode )
glBegin(GL_QUADS);
glNormal3f(1.0F,0.0F,0.0F);
for ( i = 0 ; i < disc/2 ; i++ )
for ( j = 0 ; j < disc ; j++ ) {
if ( mode )
glBegin(GL_LINE_LOOP);
glVertex3f(-5.0F,i*4.0F/disc,-5.0F+j*10.0F/disc);
glVertex3f(-5.0F,i*4.0F/disc,-5.0F+(j+1)*10.0F/disc);
glVertex3f(-5.0F,(i+1)*4.0F/disc,-5.0F+(j+1)*10.0F/disc);
glVertex3f(-5.0F,(i+1)*4.0F/disc,-5.0F+j*10.0F/disc);
if ( mode )
glEnd(); }
if ( !mode )
glEnd();
if ( !mode )
glBegin(GL_QUADS);
glNormal3f(0.0F,0.0F,1.0F);
for ( i = 0 ; i < disc/2 ; i++ )
for ( j = 0 ; j < disc ; j++ ) {
if ( mode )
glBegin(GL_LINE_LOOP);
glVertex3f(-5.0F+j*10.0F/disc,i*4.0F/disc,-5.0F);
glVertex3f(-5.0F+(j+1)*10.0F/disc,i*4.0F/disc,-5.0F);
glVertex3f(-5.0F+(j+1)*10.0F/disc,(i+1)*4.0F/disc,-5.0F);
glVertex3f(-5.0F+j*10.0F/disc,(i+1)*4.0F/disc,-5.0F);
if ( mode )
glEnd(); }
if ( !mode )
glEnd();
glPopMatrix();
}
void cylindreVertical(float r,float h) {
glPushMatrix();
GLUquadricObj *qobj = gluNewQuadric();
if ( mode )
gluQuadricDrawStyle(qobj,GLU_LINE);
else
gluQuadricDrawStyle(qobj,GLU_FILL);
gluQuadricNormals(qobj,GLU_SMOOTH);
glRotatef(-90.0F,1.0F,0.0F,0.0F);
gluCylinder(qobj,r,r,h,10,5);
gluDeleteQuadric(qobj);
glPopMatrix();
}
void cube(float c) {
if ( mode )
glutWireCube(c);
else
glutSolidCube(c);
}
void piedChaise(void) {
glPushMatrix();
cylindreVertical(0.05F,1.0F);
glPopMatrix();
}
void assiseChaise(void) {
glPushMatrix();
glScalef(1.1F,0.15F,1.1F);
cube(1.0F);
glPopMatrix();
}
void dossierChaise(void) {
glPushMatrix();
glTranslatef(-0.5F,0.0F,0.0F);
cylindreVertical(0.03F,1.2F);
glTranslatef(1.0F,0.0F,0.0F);
cylindreVertical(0.03F,1.2F);
glTranslatef(-0.5F,0.7F,0.0F);
glScalef(1.0F,0.6F,0.01F);
cube(1.0F);
glPopMatrix();
}
void chaise(void){
glPushMatrix();
axes();
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert());
glTranslatef(-0.5F,0.0F,-0.5F);
piedChaise();
glTranslatef(1.0F,0.0F,0.0F);
piedChaise();
glTranslatef(0.0F,0.0F,1.0F);
piedChaise();
glTranslatef(-1.0F,0.0F,0.0F);
piedChaise();
glPopMatrix();
glPushMatrix();
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge());
glTranslatef(0.0F,1.0F,0.0F);
assiseChaise();
glPopMatrix();
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
glPushMatrix();
glTranslatef(0.0F,1.0F,0.5F);
dossierChaise();
glPopMatrix();
}
void piedTable(void) {
glPushMatrix();
cylindreVertical(0.07F,2.0F);
glPopMatrix();
}
void tablierTable(void) {
glPushMatrix();
glScalef(3.1F,0.15F,2.1F);
cube(1.0F);
glPopMatrix();
}
void table(void){
glPushMatrix();
axes();
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert());
glTranslatef(-1.5F,0.0F,-1.0F);
piedTable();
glTranslatef(3.0F,0.0F,0.0F);
piedTable();
glTranslatef(0.0F,0.0F,2.0F);
piedTable();
glTranslatef(-3.0F,0.0F,0.0F);
piedTable();
glPopMatrix();
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBrun());
glPushMatrix();
glTranslatef(0.0F,2.0F,0.0F);
tablierTable();
glPopMatrix();
}
void clavier(void) {
glPushMatrix();
glTranslatef(0.0F,0.03F,0.0F);
glScalef(1.2F,0.06F,0.4F);
cube(1.0F);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0F,0.07F,0.0F);
for ( int i = 0 ; i < 20; i++ )
for ( int j = 0 ; j < 5; j++ ) {
glPushMatrix();
glTranslatef(-0.55F+0.0575F*i,0.0F,-0.1+0.0575F*j);
cube(0.05F);
glPopMatrix(); }
glPopMatrix();
}
void uniteCentrale(void) {
glPushMatrix();
glTranslatef(0.0F,0.12F,0.0F);
glScalef(1.1F,0.24F,1.1F);
cube(1.0F);
glPopMatrix();
}
void souris(void) {
glPushMatrix();
glTranslatef(0.0F,0.02F,0.0F);
glScalef(0.1F,0.04F,0.16F);
cube(1.0F);
glPopMatrix();
}
void ecran(void) {
glPushMatrix();
glTranslatef(0.0F,0.1F,0.0F);
glPushMatrix();
glScalef(0.8F,0.2F,0.8F);
cube(1.0F);
glPopMatrix();
glTranslatef(0.0F,0.6F,0.4F);
glPushMatrix();
glScalef(1.2F,1.0F,0.3F);
cube(1.0F);
glPopMatrix();
glTranslatef(0.0F,-0.2F,-0.4F);
glPushMatrix();
glScalef(0.8F,0.8F,0.8F);
cube(1.0F);
glPopMatrix();
glPopMatrix();
}
void ordinateur(void){
glPushMatrix();
axes();
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
clavier();
glTranslatef(0.0F,0.0F,-0.8F);
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge());
uniteCentrale();
glTranslatef(0.0F,0.12F,0.0F);
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu());
ecran();
glPopMatrix();
glPushMatrix();
glTranslatef(0.7F,0.0F,-0.0F);
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisMoyen());
souris();
glPopMatrix();
}
void sceneComplete(void){
int axeOld = axe;
axe = 0;
solEtMurs();
for ( int i = -2 ; i <=2 ; i++ )
for ( int j = -1 ; j <=2 ; j++ ) {
glPushMatrix();
glTranslatef(i*2,0,j*2);
glScalef(0.3F,0.3F,0.3F);
chaise();
glPopMatrix();
glPushMatrix();
glTranslatef(i*2,0,j*2-0.5F);
glScalef(0.3F,0.3F,0.3F);
table();
glTranslatef(0.0F,2.1F,0.6F);
ordinateur();
glPopMatrix(); }
axe = axeOld;
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisClair());
glTranslatef(0.0F,-1.5F,0.0F);
switch (aff) {
case 0 : solEtMurs();
break;
case 1 : glRotatef(ang,0.0F,1.0F,0.0F);
glScalef(rx,ry,rz);
chaise();
break;
case 2 : glRotatef(ang,0.0F,1.0F,0.0F);
glScalef(rx,ry,rz);
table();
break;
case 3 : glRotatef(ang,0.0F,1.0F,0.0F);
glScalef(rx,ry,rz);
ordinateur();
break;
case 4 : sceneComplete();
break; }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case '1' : rx -= 0.1F ;
if ( rx < 0.1F )
rx = 0.1F ;
glutPostRedisplay();
break;
case '2' : ry -= 0.1F ;
if ( ry < 0.1F )
ry = 0.1F ;
glutPostRedisplay();
break;
case '3' : rz -= 0.1F ;
if ( rz < 0.1F )
rz = 0.1F ;
glutPostRedisplay();
break;
case '7' : rx += 0.1F ;
glutPostRedisplay();
break;
case '8' : ry += 0.1F ;
glutPostRedisplay();
break;
case '9' : rz += 0.1F ;
glutPostRedisplay();
break;
case 43 : ang -= 3.0F ;
glutPostRedisplay();
break;
case 45 : ang += 3.0F ;
glutPostRedisplay();
break;
case 'a' : axe = (axe+1)%2 ;
glutPostRedisplay();
break;
case 'c' :
case 'C' : coul = (coul+1)%2 ;
glutPostRedisplay();
break;
case ' ' : mode = (mode+1)%2 ;
glutPostRedisplay();
break;
case 0x0D : aff =(aff+1)%5 ;
ang = 0.0F;
rx = ry = rz = 1.0F;
setManipulateurSourisAngle(0.0F,0.0F,0.0F) ;
setManipulateurClavierAngle(25.0F,-30.0F,0.0F) ;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(350,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Modelisation hiérarchique");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-4.5,4.5,-4.5,4.5,-500.0,500.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(25.0F,-30.0F,0.0F) ;
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}