Le source : TD-FrontBackFaceSurSphere.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2003 */
/* Materiel FRONT et BACK transparents */
/* sur une facette situee devant 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 "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int disc = 50;
static int ordre = 1;
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 facette(void) {
glPushMatrix();
Position p1 = { -8.0F,-6.0F,0.0F } ;
Position p2 = { 4.0F,-5.0F,0.0F } ;
Position p3 = { -3.0F,7.0F,0.0F } ;
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurVert(1.0F));
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu(1.0F));
glMaterialfv(GL_BACK,GL_SPECULAR,couleurBlanc(0.5F));
glMaterialfv(GL_BACK,GL_DIFFUSE,couleurRouge(0.5F));
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,64.0F);
dessineFacette(&p1,&p2,&p3,disc);
glPopMatrix();
}
void sphere(void) {
glPushMatrix();
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE);
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurNoir(1.0F));
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc(1.0F));
glTranslatef(0.0F,0.0F,-10.0F);
glutSolidSphere(2.0,72,72);
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
if ( ordre ) {
sphere();
manipulateurSouris();
manipulateurClavier();
facette(); }
else {
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
facette();
glPopMatrix();
sphere(); }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit (void) {
glClearColor(0.5,0.5,0.5,1.0) ;
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_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
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 0x0D : ordre = !ordre;
glutPostRedisplay();
break;
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);
}