Le source : TP-8Cubes-12Cylindres.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Janvier 2002 */
/* 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 "ModuleFont.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int type = 0 ;
static int maille = 0 ;
void myinit(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);
}
void axes() {
glPushMatrix() ;
glColor4fv(couleurJaune()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(1.0F,0.0F,0.0F) ;
glEnd() ;
placeFontCursor(1.05F,0.05F,0.05F);
simpleBitmapOutput("x");
glColor4fv(couleurCyan()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,1.0F,0.0F) ;
glEnd() ;
placeFontCursor(0.05F,1.05F,0.05F);
simpleBitmapOutput("y");
glColor4fv(couleurMagenta()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,0.0F,1.0F) ;
glEnd() ;
placeFontCursor(0.05F,0.05F,1.05F);
simpleBitmapOutput("z");
if ( maille ) {
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 wireCylinder(float r,float h,int n,int m) {
glPushMatrix();
glRotatef(90,1.0F,0.0F,0.0F);
glTranslatef(0.0F,0.0F,-h/2);
GLUquadricObj *qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_LINE);
gluCylinder(qobj,r,r,h,n,m);
gluDeleteQuadric(qobj);
glPopMatrix();
}
void wireCubeCylindre() {
glRotatef(90.0F,0.0F,0.0F,1.0F);
glutWireCube(1.0);
glTranslatef(0.0F,2.0F,0.0F);
wireCylinder(0.2F,4.0F,10,10);
glTranslatef(0.0F,2.0F,0.0F);
}
void wire2Cubes3Cylindres() {
glPushMatrix();
glTranslatef(0.0F,2.0F,0.0F);
wireCylinder(0.2F,4.0F,10,10);
glTranslatef(0.0F,2.0F,0.0F);
wireCubeCylindre();
wireCubeCylindre();
glPopMatrix();
glRotatef(90.0F,1.0F,0.0F,0.0F);
glTranslatef(0.0F,-4.0F,0.0F);
}
void solidCylinder(float r,float h,int n,int m) {
glPushMatrix();
glRotatef(90,1.0F,0.0F,0.0F);
glTranslatef(0.0F,0.0F,-h/2);
GLUquadricObj *qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj,r,r,h,n,m);
gluDeleteQuadric(qobj);
glPopMatrix();
}
void solidCubeCylindre() {
glRotatef(90.0F,0.0F,0.0F,1.0F);
glutSolidCube(1.0);
glTranslatef(0.0F,2.0F,0.0F);
solidCylinder(0.2F,4.0F,10,10);
glTranslatef(0.0F,2.0F,0.0F);
}
void solid2Cubes3Cylindres() {
glPushMatrix();
glTranslatef(0.0F,2.0F,0.0F);
solidCylinder(0.2F,4.0F,10,10);
glTranslatef(0.0F,2.0F,0.0F);
solidCubeCylindre();
solidCubeCylindre();
glPopMatrix();
glRotatef(90.0F,1.0F,0.0F,0.0F);
glTranslatef(0.0F,-4.0F,0.0F);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix() ;
manipulateurSouris();
manipulateurClavier();
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glTranslatef(2.0F,-2.0F,2.0F) ;
glColor4fv(couleurRouge()) ;
wire2Cubes3Cylindres();
glColor4fv(couleurVert()) ;
wire2Cubes3Cylindres();
glColor4fv(couleurBlanc()) ;
wire2Cubes3Cylindres();
glColor4fv(couleurBleu()) ;
wire2Cubes3Cylindres();
glPopMatrix();
axes();
glEnable(GL_LIGHTING);
glEnable(GL_CULL_FACE);
glPushMatrix();
glTranslatef(2.0F,-2.0F,2.0F) ;
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge(0.5F)) ;
solid2Cubes3Cylindres();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert(0.5F)) ;
solid2Cubes3Cylindres();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc(0.5F)) ;
solid2Cubes3Cylindres();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu(0.5F)) ;
solid2Cubes3Cylindres();
glPopMatrix();
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 32 : maille = 1 - maille ;
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");
myinit();
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);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}