Le source : TD-SixCubesEtSphere.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Janvier 2003 */
/* Modelisation OpenGL d'une scene */
/* composee de cubes et d'une sphere */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleFont.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int n = 6;
static int type = 0 ;
static int maille = 0 ;
static float angle = 0.0F;
static float inc = 1.0F;
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 };
glClearColor(0.15F,0.15F,0.15F,1.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);
}
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 souris(int b,int e,int x,int y) {
if ( e == GLUT_UP ) {
inc = -inc;
glutPostRedisplay(); }
}
void scene(int n,float a) {
glPushMatrix();
glColor4fv(couleurRouge()) ;
{ for ( int i = 0 ; i < n ; i++ ) {
glPushMatrix();
glRotatef(i*360.0F/n,0.0F,0.0F,1.0F);
glTranslatef(3.0F,0.0F,0.0F);
glutWireCube(1.0);
glPopMatrix(); }
glColor4fv(couleurVertFonce()) ;
glPushMatrix();
glRotatef(angle,0.0F,0.0F,1.0F);
glTranslatef(3.0F,0.0F,0.0F);
glutWireSphere(0.5,18,18);
glPopMatrix(); }
glPopMatrix();
glEnable(GL_LIGHTING);
glEnable(GL_CULL_FACE);
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge(0.5F)) ;
{ for ( int i = 0 ; i < n ; i++ ) {
glPushMatrix();
glRotatef(i*360.0F/n,0.0F,0.0F,1.0F);
glTranslatef(3.0F,0.0F,0.0F);
glutSolidCube(1.0);
glPopMatrix(); }
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert(0.5F)) ;
glPushMatrix();
glRotatef(angle,0.0F,0.0F,1.0F);
glTranslatef(3.0F,0.0F,0.0F);
glutWireSphere(0.5,18,18);
glPopMatrix(); }
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix() ;
glEnable(GL_DEPTH_TEST);
glRotatef(20.0F,1.0F,0.0F,0.0F);
glRotatef(30.0F,0.0F,1.0F,0.0F);
scene(n,angle);
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
axes();
glDisable(GL_DEPTH_TEST);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void idle1(void) {
angle += inc;
glutPostRedisplay();
}
void idle2(void) {
angle += inc;
if ( ( angle > 360.0F ) || ( angle < 0.0F ) )
inc = -inc;
glutPostRedisplay();
}
void key(unsigned char key,int x,int y) {
static int anim = 1;
switch ( key ) {
case 0x0D : anim = !anim;
glutIdleFunc((anim) ? idle2 : NULL);
break;
case 'a' :
case 'A' : glutIdleFunc(idle2);
angle = angle-((int) angle/360)*360;
glutPostRedisplay();
break;
case 43 : n++;
glutPostRedisplay();
break;
case 45 : n--;
if ( n < 0 )
n = 0;
glutPostRedisplay();
break;
case 32 : maille = 1 - maille ;
glutPostRedisplay();
break;
case 0x1B : exit(0);
break; }
}
void special(int key,int x,int y) {
switch ( key ) {
case GLUT_KEY_UP : inc *= 1.1F;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : inc /= 1.1F;
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Six cubes");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-4.0,4.0,-4.0,4.0,-50.0,50.0);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutMouseFunc(souris);
glutIdleFunc(idle1);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}