Le source : SchemaBumpMapping.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
/* Exemple de bump mapping */
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleFleche.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleManipulateur.h"
int aff = 0;
int fleches = 1;
int disci = 100;
int discj = 6;
float pos = 1.0F;
float fact = 15.0F;
float amp = 10.F;
void normalize(float *x,float*z) {
float d =(float) sqrt(*x * *x + *z * *z);
*x /= d;
*z /= d;
}
void myinit(void) {
glClearColor(0.5F,0.5F,1.0F,1.0F) ;
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void generationSommetBump(float x,float y) {
float nxi = fact*sin(x*fact);
float nzi = amp;
normalize(&nxi,&nzi);
glNormal3f(nxi,0.0F,nzi);
glVertex2f(x,y);
}
void traceFleche(float x,float y,float z) {
glPushMatrix();
glTranslatef(x,y,z);
flecheEnVolume(0.0F,0.0F,0.4F,0.03F,0.1F,0.005F) ;
glPopMatrix();
}
void traceFlecheBump(float x,float y,float z) {
float nxi = fact*sin(x*fact);
float nzi = amp;
normalize(&nxi,&nzi);
glPushMatrix();
glTranslatef(x,y,z);
flecheEnVolume(0.4F*nxi,0.0F,0.4F*nzi,0.03F,0.1F,0.005F) ;
glPopMatrix();
}
void toleOndulee(void) {
glBegin(GL_QUADS);
for ( int i = 0 ; i < 2*disci ; i++ )
for ( int j = 0 ; j < discj ; j++ ) {
float xi = -2.0F+i*2.0F/disci;
float xj = -2.0F+(i+1)*2.0F/disci;
float zi = cos(xi*fact)/amp;
float zj = cos(xj*fact)/amp;
float nxi = fact*sin(xi*fact);
float nxj = fact*sin(xj*fact);
float nzi = amp;
float nzj = amp;
normalize(&nxi,&nzi);
normalize(&nxj,&nzj);
glNormal3f(nxi,0.0F,nzi);
glVertex3f(xi,-1.0F+2.0F*(float)(j+1)/discj,zi);
glVertex3f(xi,-1.0F+2.0F*(float)j/discj,zi);
glNormal3f(nxj,0.0F,nzj);
glVertex3f(xj,-1.0F+2.0F*(float)j/discj,zj);
glVertex3f(xj,-1.0F+2.0F*(float)(j+1)/discj,zj); }
glEnd();
if ( fleches ) {
glDisable(GL_LIGHTING);
glColor4fv(couleurRouge());
int nf = disci/10;
for ( int i = 0 ; i < 2*nf+1 ; i++ )
for ( int j = 0 ; j < discj+1 ; j++ ) {
float xi = -2.0F+i*2.0F/nf;
float zi = cos(xi*fact)/amp;
float nxi = fact*sin(xi*fact);
float nzi = amp;
normalize(&nxi,&nzi);
glPushMatrix();
glTranslatef(xi,-1.0F+2.0F*(float)j/discj,zi);
flecheEnVolume(0.4F*nxi,0.0F,0.4F*nzi,0.03F,0.1F,0.005F);
glPopMatrix(); }
glEnable(GL_LIGHTING); }
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0F,-0.5F,0.0F);
manipulateurSouris();
manipulateurClavier();
glEnable(GL_LIGHTING);
GLfloat l_dir[] = { -1.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_POSITION,l_dir);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
glPushMatrix();
glTranslatef(pos,0.0F,1.0F);
glutSolidSphere(0.05,30,30) ;
glPopMatrix();
GLfloat l_pos[] = { pos,0.0F,1.0F,1.0F };
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisClair());
switch ( aff ) {
case 0 : if ( fleches ) {
glDisable(GL_LIGHTING);
glColor4fv(couleurRouge());
traceFleche(-2.0F,-1.0F,0.0F);
traceFleche(2.0F,-1.0F,0.0F);
traceFleche(2.0F,1.0F,0.0F);
traceFleche(-2.0F,1.0F,0.0F);
glEnable(GL_LIGHTING); }
glNormal3f(0.0F,0.0F,1.0F);
glBegin(GL_QUADS);
glVertex2f(-2.0F,-1.0F);
glVertex2f(2.0F,-1.0F);
glVertex2f(2.0F,1.0F);
glVertex2f(-2.0F,1.0F);
glEnd();
break;
case 1 : { int i;
if ( fleches ) {
glDisable(GL_LIGHTING);
glColor4fv(couleurRouge());
int nf = disci/10;
for ( i = 0 ; i < 2*nf+1 ; i++ )
for ( int j = 0 ; j < discj+1 ; j++ ) {
traceFleche(-2.0F+i*2.0F/nf,-1.0F+j*2.0F/discj,0.0F); }
glEnable(GL_LIGHTING); }
glNormal3f(0.0F,0.0F,1.0F);
glBegin(GL_QUADS);
for ( i = 0 ; i < 2*disci ; i++ )
for ( int j = 0 ; j < discj ; j++ ) {
glVertex2f(-2.0F+i*2.0F/disci,-1.0F+j*2.0F/discj);
glVertex2f(-2.0F+(i+1)*2.0F/disci,-1.0F+j*2.0F/discj);
glVertex2f(-2.0F+(i+1)*2.0F/disci,-1.0F+(j+1)*2.0F/discj);
glVertex2f(-2.0F+i*2.0F/disci,-1.0F+(j+1)*2.0F/discj); }
glEnd();
break; }
case 2 : { int i;
if ( fleches ) {
glDisable(GL_LIGHTING);
glColor4fv(couleurRouge());
int nf = disci/10;
for ( i = 0 ; i < 2*(disci/10)+1 ; i++ )
for ( int j = 0 ; j < discj+1 ; j++ ) {
traceFlecheBump(-2.0F+i*2.0F/nf,-1.0F+j*2.0F/discj,0.0F); }
glEnable(GL_LIGHTING); }
glBegin(GL_QUADS);
for ( i = 0 ; i < 2*disci ; i++ )
for ( int j = 0 ; j < discj ; j++ ) {
generationSommetBump(-2.0F+i*2.0F/disci,-1.0F+j*2.0F/discj);
generationSommetBump(-2.0F+(i+1)*2.0F/disci,-1.0F+j*2.0F/discj);
generationSommetBump(-2.0F+(i+1)*2.0F/disci,-1.0F+(j+1)*2.0F/discj);
generationSommetBump(-2.0F+i*2.0F/disci,-1.0F+(j+1)*2.0F/discj); }
glEnd();
break; }
case 3 : { toleOndulee();
break; } }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 'a' : amp *= 1.1F;
glutPostRedisplay();
break;
case 'A' : amp /= 1.1F;
glutPostRedisplay();
break;
case 'l' : pos += 0.1F;
glutPostRedisplay();
break;
case 'L' : pos -= 0.1F;
glutPostRedisplay();
break;
case 'f' : fact--;
if ( fact < 1.0F )
fact = 1.0F;
glutPostRedisplay();
break;
case 'F' : fact++;
glutPostRedisplay();
break;
case 43 : disci++;
glutPostRedisplay();
break;
case 45 : disci--;
if ( disci < 2 )
disci = 2;
glutPostRedisplay();
break;
case ' ' : fleches = (fleches+1)%2;
glutPostRedisplay();
break;
case 0x0D : aff = (aff+1)%4;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(400,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Schema bump mapping");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.5,1.5,-1.5,1.5,-500.0,500.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(-65.0F,0.0F,10.0F) ;
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}