Le source: NormalesMoyennes.cpp
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glaux.h> #include <math.h> static GLfloat view_rotx=-90.0 ; static GLfloat view_roty=0.0 ; static GLfloat view_rotz=0.0; static int s = 0 ; void myinit(void) { GLfloat specular[] = { 0.0,0.0,0.0,0.0 }; GLfloat shinines[] = { 0.0 }; GLfloat l_pos[] = { 1.0,1.0,1.0,0.0 }; glClearColor(1.0,1.0,1.0,1.0) ; glMaterialfv(GL_FRONT,GL_SPECULAR,specular); glMaterialfv(GL_FRONT,GL_SHININESS,shinines); glLightfv(GL_LIGHT0,GL_POSITION,l_pos); glEnable(GL_LIGHT0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); } void normal(float px,float py,float pz,float dx,float dy,float dz) { float vx = dy ; float vy = -dx ; float vz = 0.0F ; float d =(float) sqrt(dy*dy+dx*dx) ; vx /= d ; vy /= d ; float vx1 = (2.0F*dx+vx)/3.0F ; float vy1 = (2.0F*dy+vy)/3.0F ; float vz1 = (2.0F*dz+vz)/3.0F ; float vx2 = (2.0F*dx-vx)/3.0F ; float vy2 = (2.0F*dy-vy)/3.0F ; float vz2 = (2.0F*dz-vz)/3.0F ; glPushMatrix() ; glTranslatef(px,py,pz) ; glBegin(GL_LINES) ; glVertex3f(0.0F,0.0F,0.0F) ; glVertex3f(dx,dy,dz) ; glVertex3f(dx,dy,dz) ; glVertex3f(vx1,vy1,vz1) ; glVertex3f(dx,dy,dz) ; glVertex3f(vx2,vy2,vz2) ; glEnd() ; glPopMatrix() ; } void scene1(void) { GLfloat noir[] = { 0.0F,0.0F,0.0F,1.0F }; GLfloat rouge[] = { 1.0F,0.0F,0.0F,1.0F }; GLfloat vert[] = { 0.0F,1.0F,0.0F,1.0F }; GLfloat gris[] = { 0.7F,0.7F,0.7F,1.0F }; glMaterialfv(GL_FRONT,GL_DIFFUSE,gris); glEnable(GL_LIGHTING); glPushMatrix(); float p[] = { 0.0F,0.0F,0.0F } ; float p1[] = { -5.0F,-4.0F,-3.0F } ; float p2[] = { 1.0F,-5.0F,-4.0F } ; float p3[] = { 4.0F,1.0F,-5.0F } ; float p4[] = { 2.0F,5.0F,-4.0F } ; float p5[] = { -2.0F,4.0F,-4.0F } ; 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); glBegin(GL_TRIANGLES) ; glNormal3f(0.94F,0.26F,2.84F) ; glVertex3fv(p) ; glNormal3f(-1.19F,-0.53F,2.7F) ; glVertex3fv(p1) ; glNormal3f(1.33F,-1.52F,2.22F) ; glVertex3fv(p2) ; glNormal3f(0.94F,0.26F,2.84F) ; glVertex3fv(p) ; glNormal3f(1.33F,-1.52F,2.22F) ; glVertex3fv(p2) ; glNormal3f(2.35F,-0.12F,1.86F) ; glVertex3fv(p3) ; glNormal3f(0.94F,0.26F,2.84F) ; glVertex3fv(p) ; glNormal3f(2.35F,-0.12F,1.86F) ; glVertex3fv(p3) ; glNormal3f(1.46F,1.4F,2.21F) ; glVertex3fv(p4) ; glNormal3f(0.94F,0.26F,2.84F) ; glVertex3fv(p) ; glNormal3f(1.46F,1.4F,2.21F) ; glVertex3fv(p4) ; glNormal3f(-0.84F,1.66F,2.35F) ; glVertex3fv(p5) ; glNormal3f(0.94F,0.26F,2.84F) ; glVertex3fv(p) ; glNormal3f(-0.84F,1.66F,2.35F) ; glVertex3fv(p5) ; glNormal3f(-1.19F,-0.53F,2.7F) ; glVertex3fv(p1) ; glEnd() ; glMaterialfv(GL_FRONT,GL_DIFFUSE,noir); glLineWidth(2.0) ; glDisable(GL_LIGHTING); glColor3f(0.0F,0.0F,1.0F); normal(-1.33F,-3.0F,-2.33F,0.081F,-1.86F,2.34F) ; normal(1.66F,-1.33F,-3.0F,2.32F,-0.88F,1.68F) ; normal(2.0F,2.0F,-3.0F,2.23F,0.64F,1.91F) ; normal(0.0F,3.0F,-2.66F,0.49F,1.97F,2.21F) ; normal(-2.33F,0.0F,-2.33F,-2.0F,1.0F,2.0F) ; glColor3f(1.0F,0.0F,0.0F); normal(0.0F,0.0F,0.0F,0.94F,0.26F,2.84F) ; glColor3f(0.0F,1.0F,0.0F); normal(p1[0],p1[1],p1[2],-1.19F,-0.53F,2.7F) ; normal(p2[0],p2[1],p2[2],1.33F,-1.52F,2.22F) ; normal(p3[0],p3[1],p3[2],2.35F,-0.12F,1.86F) ; normal(p4[0],p4[1],p4[2],1.46F,1.4F,2.21F) ; normal(p5[0],p5[1],p5[2],-0.84F,1.66F,2.35F) ; glPopMatrix(); } void scene2(void) { GLfloat noir[] = { 0.0F,0.0F,0.0F,1.0F }; GLfloat gris[] = { 0.7F,0.7F,0.7F,1.0F }; glMaterialfv(GL_FRONT,GL_DIFFUSE,gris); glEnable(GL_LIGHTING); glPushMatrix(); float p[] = { 0.0F,0.0F,0.0F } ; float p1[] = { -5.0F,-4.0F,-3.0F } ; float p2[] = { 1.0F,-5.0F,-4.0F } ; float p3[] = { 4.0F,1.0F,-5.0F } ; float p4[] = { 2.0F,5.0F,-4.0F } ; float p5[] = { -2.0F,4.0F,-4.0F } ; 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); glBegin(GL_TRIANGLES) ; glNormal3f(0.081F,-1.86F,2.34F) ; glVertex3fv(p) ; glVertex3fv(p1) ; glVertex3fv(p2) ; glNormal3f(2.32F,-0.88F,1.68F) ; glVertex3fv(p) ; glVertex3fv(p2) ; glVertex3fv(p3) ; glNormal3f(2.23F,0.64F,1.91F) ; glVertex3fv(p) ; glVertex3fv(p3) ; glVertex3fv(p4) ; glNormal3f(0.49F,1.97F,2.21F) ; glVertex3fv(p) ; glVertex3fv(p4) ; glVertex3fv(p5) ; glNormal3f(-2.0F,1.0F,2.0F) ; glVertex3fv(p) ; glVertex3fv(p5) ; glVertex3fv(p1) ; glEnd() ; glMaterialfv(GL_FRONT,GL_DIFFUSE,noir); glLineWidth(2.0) ; glDisable(GL_LIGHTING); glColor3f(0.0F,0.0F,1.0F); normal(-1.33F,-3.0F,-2.33F,0.081F,-1.86F,2.34F) ; normal(1.66F,-1.33F,-3.0F,2.32F,-0.88F,1.68F) ; normal(2.0F,2.0F,-3.0F,2.23F,0.64F,1.91F) ; normal(0.0F,3.0F,-2.66F,0.49F,1.97F,2.21F) ; normal(-2.33F,0.0F,-2.33F,-2.0F,1.0F,2.0F) ; glColor3f(1.0F,0.0F,0.0F); normal(0.0F,0.0F,0.0F,0.94F,0.26F,2.84F) ; glColor3f(0.0F,1.0F,0.0F); normal(p1[0],p1[1],p1[2],-1.19F,-0.53F,2.7F) ; normal(p2[0],p2[1],p2[2],1.33F,-1.52F,2.22F) ; normal(p3[0],p3[1],p3[2],2.35F,-0.12F,1.86F) ; normal(p4[0],p4[1],p4[2],1.46F,1.4F,2.21F) ; normal(p5[0],p5[1],p5[2],-0.84F,1.66F,2.35F) ; glPopMatrix(); } void CALLBACK display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); if ( s ) scene1() ; else scene2() ; glFlush(); auxSwapBuffers(); } void CALLBACK myReshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-8.5,8.5,-8.5*(float)h/(float)w,8.5*(float)h/(float)w,-8,8); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0F,1.0F,0.0F); } void CALLBACK up(void) { view_rotx++ ; } void CALLBACK down(void) { view_rotx-- ; } void CALLBACK left(void) { view_roty++ ; } void CALLBACK right(void) { view_roty-- ; } void CALLBACK keyz(void) { view_rotz++ ; } void CALLBACK keyZ(void) { view_rotz-- ; } void CALLBACK keySpace() { s = 1 - s ; } void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGB|AUX_DEPTH); auxInitPosition(0,0,350,250); auxInitWindow("Calcul des normales moyennes"); myinit(); auxKeyFunc(AUX_UP,up) ; auxKeyFunc(AUX_DOWN,down) ; auxKeyFunc(AUX_LEFT,left) ; auxKeyFunc(AUX_RIGHT,right) ; auxKeyFunc(AUX_z,keyz) ; auxKeyFunc(AUX_Z,keyZ) ; auxKeyFunc(AUX_SPACE,keySpace) ; auxReshapeFunc(myReshape); auxMainLoop(display); }