Le source : CubeDe8CubesEt12Cylindres.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Septembre 2005 */
/* Modelisation OpenGL d'une scene */
/* composee de cubes et de cylindres */
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleCylindres.h"
#include "ModuleAxes.h"
#include "ModuleFont.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int solutionTD = 0 ;
static int aff = 0 ;
void init(void) {
GLfloat light_position0[] = { 0.0F,0.0F,1.0F,0.0F };
GLfloat light_position1[] = { 0.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir());
glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
glLightfv(GL_LIGHT1,GL_POSITION,light_position1);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
setManipulateurClavierAngle(35.0F,20.0F,0.0F);
glEnable(GL_DEPTH_TEST);
}
void planxOy(int aff) {
glPushMatrix() ;
if ( aff ) {
glColor4fv(couleurGrisMoyen()) ;
for ( int i = -100 ; i < 100 ; i++ ) {
glBegin(GL_LINES) ;
glVertex3f((float) i,200.0F,0.0F) ;
glVertex3f((float) i,-200.0F,0.0F) ;
glEnd() ;
glBegin(GL_LINES) ;
glVertex3f(200.0F,(float) i,0.0F) ;
glVertex3f(-200.0F,(float) i,0.0F) ;
glEnd() ; } }
glPopMatrix() ;
}
void solidCube(float tx,float ty,float tz) {
glPushMatrix();
glScalef(tx,ty,tz);
glutSolidCube(1.0);
glPopMatrix();
}
void wireCube(float tx,float ty,float tz) {
glPushMatrix();
glScalef(tx,ty,tz);
glutWireCube(1.0);
glPopMatrix();
}
void wireElement() {
glPushMatrix();
glTranslatef(2.0F,0.0F,2.0F);
wireCylindre(0.25F,3.0F,10,5);
for ( int i = 0 ; i < 2 ; i++ ) {
glTranslatef(0.0F,2.0F,0.0F);
wireCube(1.0F,1.0F,1.0F);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glTranslatef(0.0F,2.0F,0.0F);
wireCylindre(0.25F,3.0F,10,5); }
glPopMatrix();
}
void solidElement() {
glPushMatrix();
glTranslatef(2.0F,0.0F,2.0F);
solidCylindre(0.25F,3.0F,10,5);
for ( int i = 0 ; i < 2 ; i++ ) {
glTranslatef(0.0F,2.0F,0.0F);
solidCube(1.0F,1.0F,1.0F);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glTranslatef(0.0F,2.0F,0.0F);
solidCylindre(0.25F,3.0F,10,5); }
glPopMatrix();
}
void wireScene() {
glPushMatrix();
glColor4fv(couleurRouge()) ;
wireElement();
for ( int i = 1 ; i < 4 ; i++ ) {
glPushMatrix();
glRotatef(90.0F*i,1.0F,0.0F,0.0F);
switch (i) {
case 1 : glColor4fv(couleurVert()) ;
break;
case 2 : glColor4fv(couleurBlanc()) ;
break;
case 3 : glColor4fv(couleurBleu()) ;
break; }
wireElement();
glPopMatrix(); }
glPopMatrix();
}
void solidScene() {
glEnable(GL_LIGHTING);
glEnable(GL_CULL_FACE);
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge(0.5F)) ;
solidElement();
for ( int i = 1 ; i < 4 ; i++ ) {
glPushMatrix();
glRotatef(90.0F*i,1.0F,0.0F,0.0F);
switch (i) {
case 1 : glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert(0.5F)) ;
break;
case 2 : glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc(0.5F)) ;
break;
case 3 : glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu(0.5F)) ;
break; }
solidElement();
glPopMatrix(); }
glPopMatrix();
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
}
void scene(void) {
glPushMatrix();
planxOy(aff);
axes();
wireScene();
solidScene();
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix() ;
manipulateurSouris();
manipulateurClavier();
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void sceneTD() {
glPushMatrix() ;
wireElement();
for ( int i = 1 ; i < 4 ; i++ ) {
glPushMatrix();
glRotatef(90.0F*i,1.0F,0.0F,0.0F);
wireElement();
glPopMatrix(); }
glPopMatrix() ;
}
void displayTD(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix() ;
manipulateurSouris();
manipulateurClavier();
glColor4fv(couleurBlanc()) ;
axes();
planxOy(aff);
sceneTD();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 0x0D : solutionTD = !solutionTD;
glutDisplayFunc((solutionTD) ? displayTD : display);
glutPostRedisplay();
break;
case 32 : aff = !aff ;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("8 cubes et 12 cylindres");
init();
creationMenuBasique();
setParametresOrthoBasique(-4.5,4.5,-4.5,4.5,-50.0,50.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutIdleFunc(idleBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}