/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
/* Exemple de bump mapping */
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <stdio.h>
#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleManipulateur.h"
int aff = 0;
int anim = 0;
int disci = 100;
int discj = 10;
float ang = 0.8F;
float da = -0.04F;
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) ;
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisClair());
glEnable(GL_LIGHTING);
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 idle(void) {
ang += da ;
if ( ( ang < -0.8F ) || ( ang > 0.8F ) )
da = -da ;
glutPostRedisplay() ;
}
void facetteBumpee(void) {
switch ( aff ) {
case 0 : glNormal3f(0.0F,0.0F,1.0F);
glBegin(GL_QUADS);
glVertex2f(-1.0F,-0.5F);
glVertex2f(1.0F,-0.5F);
glVertex2f(1.0F,0.5F);
glVertex2f(-1.0F,0.5F);
glEnd();
break;
case 1 : { glNormal3f(0.0F,0.0F,1.0F);
glBegin(GL_QUADS);
for ( int i = 0 ; i < disci ; i++ )
for ( int j = 0 ; j < discj ; j++ ) {
glVertex2f(-1.0F+i*2.0F/disci,-0.5F+(float)j/discj);
glVertex2f(-1.0F+(i+1)*2.0F/disci,-0.5F+(float)j/discj);
glVertex2f(-1.0F+(i+1)*2.0F/disci,-0.5F+(float)(j+1)/discj);
glVertex2f(-1.0F+i*2.0F/disci,-0.5F+(float)(j+1)/discj); }
glEnd();
break; }
case 2 : { glBegin(GL_QUADS);
for ( int i = 0 ; i < disci ; i++ )
for ( int j = 0 ; j < discj ; j++ ) {
generationSommetBump(-1.0F+i*2.0F/disci,-0.5F+(float)j/discj);
generationSommetBump(-1.0F+(i+1)*2.0F/disci,-0.5F+(float)j/discj);
generationSommetBump(-1.0F+(i+1)*2.0F/disci,-0.5F+(float)(j+1)/discj);
generationSommetBump(-1.0F+i*2.0F/disci,-0.5F+(float)(j+1)/discj); }
glEnd();
break; } }
}
void toleOndulee(void) {
glBegin(GL_QUADS);
for ( int i = 0 ; i < disci ; i++ )
for ( int j = 0 ; j < discj ; j++ ) {
float xi = -1.0F+i*2.0F/disci;
float xj = -1.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,-0.5F+(float)(j+1)/discj,zi);
glVertex3f(xi,-0.5F+(float)j/discj,zi);
glNormal3f(nxj,0.0F,nzj);
glVertex3f(xj,-0.5F+(float)j/discj,zj);
glVertex3f(xj,-0.5F+(float)(j+1)/discj,zj); }
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
GLfloat l_dir[] = { sin(ang),0.0,cos(ang),0.0 };
glLightfv(GL_LIGHT0,GL_POSITION,l_dir);
if ( aff == 2 ) {
glTranslatef(0.0F,-0.6F,0.0F);
toleOndulee();
glTranslatef(0.0F,1.2F,0.0F); }
facetteBumpee();
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 'f' : fact--;
if ( fact < 1.0F )
fact = 1.0F;
glutPostRedisplay();
break;
case 'F' : fact++;
glutPostRedisplay();
break;
case ' ' : anim = (anim+1)%2;
if ( anim )
glutIdleFunc(idle);
else
glutIdleFunc(NULL);
glutPostRedisplay();
break;
case 43 : disci++;
glutPostRedisplay();
break;
case 45 : disci--;
if ( disci < 2 )
disci = 2;
glutPostRedisplay();
break;
case 0x0D : aff = (aff+1)%3;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Bump mapping");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.5,1.5,-1.5,1.5,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}