Le source : LumiereSpeculaire.cpp
#include "windows.h" #include <GL/gl.h> #include <GL/glu.h> #include <GL/glaux.h> #include <stdio.h> #include <math.h> #include "modulefont.h" static GLfloat view_rotx=25.0F ; static GLfloat view_roty=0.0F ; static GLfloat view_rotz=0.0F ; static GLfloat px=0.0F ; static GLfloat pz=0.0F ; static float ymax ; void myinit(void) { makeRasterFont(); GLfloat light_ambient[] = { 0.0F,0.0F,0.0F,1.0F }; GLfloat light_diffuse[] = { 1.0F,1.0F,1.0F,1.0F }; GLfloat light_specular[] = { 0.0F,0.0F,0.0F,1.0F }; GLfloat light_position[] = { 0.0F,0.0F,1.0F,0.0F }; glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); glLightfv(GL_LIGHT0,GL_POSITION,light_position); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glDepthFunc(GL_LESS); } float norme(float x,float y,float z) { return((float) sqrt(x*x+y*y+z*z)) ; } void CALLBACK display(void) { float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; float vert[] = { 0.0F,1.0F,0.0F,1.0F }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glPushMatrix(); float rx = px+9.0F ; float ry = 11.0F ; float rz = pz ; float nr = norme(rx,ry,rz) ; rx /= nr ; ry /= nr ; rz /= nr ; float ex = 8.0F-px ; float ey = 11.0F ; float ez = -pz ; float ne = norme(ex,ey,ez) ; ex /= ne ; ey /= ne ; ez /= ne ; float scal = ex*rx+ey*ry+ez*rz; glRotatef(view_rotx,1.0,0.0,0.0); glRotatef(view_roty,0.0,1.0,0.0); glRotatef(view_rotz,0.0,0.0,1.0); glEnable(GL_LIGHTING); glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune) ; glTranslatef(-9.0F,5.0F,0.0F) ; auxSolidSphere(0.5); glPopMatrix(); glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,vert) ; glTranslatef(8.0F,5.0F,0.0F) ; auxSolidSphere(0.5); glPopMatrix(); glPushMatrix(); glDisable(GL_LIGHTING); glColor4fv(jaune) ; glBegin(GL_LINES); glVertex3f(-9.0F,5.0F,0.0F); glVertex3f(px,-6.0F,pz); glEnd(); glColor4fv(rouge) ; glBegin(GL_LINES); glVertex3f(2*px+9.0F,5.0F,2*pz); glVertex3f(px,-6.0F,pz); glEnd(); glColor4fv(bleu) ; glBegin(GL_LINES); glVertex3f(px,8.0F,pz); glVertex3f(px,-6.0F,pz); glEnd(); glColor4fv(vert) ; glBegin(GL_LINES); glVertex3f(8.0F,5.0F,0.0F); glVertex3f(px,-6.0F,pz); glEnd(); glEnable(GL_LIGHTING); glPopMatrix(); glPushMatrix(); glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc) ; glTranslatef(0.0F,-6.25F,0.0F) ; auxSolidBox(16.0,0.5,14.0) ; glPopMatrix(); glPopMatrix(); glDisable(GL_LIGHTING); GLfloat white[4] = { 1.0,1.0,1.0,1.0 }; glDisable(GL_DEPTH_TEST); glColor4fv(white); glRasterPos3f(-10,ymax-1,0); char st[50] ; sprintf(st,"%f -> %lf degres",scal,acos(scal)*180/3.14159); printString(st); glFlush(); auxSwapBuffers(); } void CALLBACK myReshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); ymax = h*11.0F/w ; glOrtho(-11,11,-h*11.0/w,ymax,-15.0,15.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void CALLBACK keyLeft(void) { px -= 0.1F ; if ( px < -6.0F ) px = -6.0F ; } void CALLBACK keyRight(void) { px += 0.1F ; if ( px > 6.0F ) px = 6.0F ; } void CALLBACK keyUp(void) { pz -= 0.1F ; if ( pz < -6.0F ) pz = -6.0F ; } void CALLBACK keyDown(void) { pz += 0.1F ; if ( pz > 6.0F ) pz = 6.0F ; } void CALLBACK keyx(void) { view_rotx++ ; } void CALLBACK keyX(void) { view_rotx-- ; } void CALLBACK keyy(void) { view_roty++ ; } void CALLBACK keyY(void) { view_roty-- ; } void CALLBACK keyz(void) { view_rotz++ ; } void CALLBACK keyZ(void) { view_rotz-- ; } void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGB|AUX_DEPTH); auxInitPosition(0,0,300,300); auxInitWindow("Réflexion spéculaire"); myinit(); auxKeyFunc(AUX_LEFT,keyLeft) ; auxKeyFunc(AUX_RIGHT,keyRight) ; auxKeyFunc(AUX_UP,keyUp) ; auxKeyFunc(AUX_DOWN,keyDown) ; auxKeyFunc(AUX_x,keyx) ; auxKeyFunc(AUX_X,keyX) ; auxKeyFunc(AUX_y,keyy) ; auxKeyFunc(AUX_Y,keyY) ; auxKeyFunc(AUX_z,keyz) ; auxKeyFunc(AUX_Z,keyZ) ; auxReshapeFunc(myReshape); auxMainLoop(display); }
RETOUR