Le source: BoitesEnglobantes.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Illustration de la notion */
/* de boite englobante */
/* en lancer de rayons */
#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 "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int aff = 0 ;
static GLfloat ctlpoints[4][4][3];
static GLUnurbsObj *theNurb;
static int fenetre;
void init_surface(void) {
int u,v;
for( u = 0 ; u < 4 ; u++ ) {
for( v = 0 ; v < 4 ; v++ ) {
ctlpoints[u][v][0] = 3.95F*(u-1.5F);
ctlpoints[u][v][2] = 3.95F*(v-1.5F);
ctlpoints[u][v][1] = ((rand()%1000)/1000.0F-0.5F)*13.0F; } }
}
void myinit(void) {
GLfloat light_position0[] = { 1.0F,1.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc(0.5F));
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurBlanc());
glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
init_surface();
theNurb = gluNewNurbsRenderer();
gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,10.0);
gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_FILL);
}
void display1(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge()) ;
if ( aff ) {
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,couleurBlanc()) ;
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,100.0F) ;
GLfloat knots[8] = {0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0};
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb,8,knots,8,knots,4*3,3,&ctlpoints[0][0][0],4,4,GL_MAP2_VERTEX_3);
gluEndSurface(theNurb);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,couleurNoir()) ; }
else {
glPushMatrix();
glScalef(14.0F,5.0F,10.0F);
glutSolidCube(1.0);
glPopMatrix(); }
glPopMatrix();
}
void display2(void) {
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBlanc(0.5F)) ;
glPushMatrix();
if ( aff ) {
glPushMatrix();
glScalef(12.0F,12.0F,12.0F);
glutSolidCube(1.0);
glPopMatrix(); }
else
glutSolidSphere(9.0,50,50) ;
glPopMatrix();
}
void display(void) {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix() ;
manipulateurSouris();
manipulateurClavier();
glRotatef(23.0F,1.0F,0.0F,0.0F);
glRotatef(30.0F,0.0F,1.0F,0.0F);
display1() ;
display2() ;
glPopMatrix() ;
glFlush();
glutSwapBuffers() ;
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 0x0D : aff = 1 - aff;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
fenetre = glutCreateWindow("Boites englobantes");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-11.0,11.0,-11.0,11.0,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}