Le source : TD-FrontBackFace.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2003 */
/* Materiel FRONT et BACK sur une facette */
#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 disc = 50;
typedef struct Position {
float x ;
float y ;
float z ; } Position ;
void dessineFacette(Position *p1,Position *p2,Position *p4,int disc) {
float dx1 = (p2->x-p1->x)/disc;
float dy1 = (p2->y-p1->y)/disc;
float dz1 = (p2->z-p1->z)/disc;
float dx2 = (p4->x-p1->x)/disc;
float dy2 = (p4->y-p1->y)/disc;
float dz2 = (p4->z-p1->z)/disc;
glNormal3f(0.0F,0.0F,1.0F);
glBegin(GL_QUADS);
for ( int i = 0 ; i < disc ; i++ )
for ( int j = 0 ; j < disc ; j++ ) {
Position pp1 = { p1->x+i*dx1+j*dx2,
p1->y+i*dy1+j*dy2,
p1->z+i*dz1+j*dz2};
Position pp2 = { pp1.x+dx1,pp1.y+dy1,pp1.z+dz1 };
Position pp3 = { pp1.x+dx1+dx2,pp1.y+dy1+dy2,pp1.z+dz1+dz2 };
Position pp4 = { pp1.x+dx2,pp1.y+dy2,pp1.z+dz2 };
glVertex3fv((float *) &pp1);
glVertex3fv((float *) &pp2);
glVertex3fv((float *) &pp3);
glVertex3fv((float *) &pp4); }
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
Position p1 = { -8.0F,-6.0F,0.0F } ;
Position p2 = { 4.0F,-5.0F,0.0F } ;
Position p3 = { -3.0F,7.0F,0.0F } ;
dessineFacette(&p1,&p2,&p3,disc);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit (void) {
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
GLfloat shininesFront[] = { 100.0 };
glClearColor(0.5,0.5,1.0,1.0) ;
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc());
glMaterialfv(GL_FRONT,GL_SHININESS,shininesFront);
GLfloat shininesBack[] = { 50.0 };
glMaterialfv(GL_BACK,GL_SPECULAR,couleurBleu());
glMaterialfv(GL_BACK,GL_SHININESS,shininesBack);
glMaterialfv(GL_BACK,GL_DIFFUSE,couleurRouge());
GLfloat l_pos0[] = { 1.0,1.0,2.2,1.0 };
glLightfv(GL_LIGHT0,GL_POSITION,l_pos0);
GLfloat l_pos1[] = { -1.0,1.0,2.5,1.0 };
glLightfv(GL_LIGHT1,GL_POSITION,l_pos1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 43 : disc++;
glutPostRedisplay();
break;
case 45 : disc--;
if ( disc < 3 )
disc = 3;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Materiaux faces avant-arriere");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-10.0,10.0,-10.0,10.0,-50.0,50.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutIdleFunc(idleBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}