/* Auteur: Nicolas JANEY */ /* Avril 2001 */ /* Utilisation d'une courbe de Bezier */ /* pour generer un objet texture */ /* (un vase) par revolution */ #include #include #include #include #include #include #define iw 256 #define ih 256 #define tf "VentPrairie.raw" static GLubyte im[3*iw*ih]; static GLfloat view_rotx=50.0F ; static GLfloat view_roty=20.0F ; static GLfloat view_rotz=10.0F ; static int cyl = 0 ; static int pt = 0 ; static int ff = 0 ; static int np1 = 20 ; static int np2 = 20 ; float p = 0.55F ; float pts1[10][2] = { {0.0F,-5.0F}, {5.0F,-5.0}, {4.5F,-4.0F}, {2.0F,-2.0F}, {1.5F,0.0F}, {0.2F,1.0F}, {0.2F,2.0F}, {1.5F,4.0F}, {1.5F,5.0F}, {0.5F,5.0F} } ; GLfloat texpts[2][2][2] = { {{0.0,0.0},{0.0,1.0}}, {{1.0,0.0},{1.0,1.0}}}; void lectureTexture(char *fichier,int dx,int dy) { FILE *f = fopen(fichier,"rb") ; if ( f ) { for ( int i = 0 ; i < dx ; i++ ) for ( int j = 0 ; j < dy ; j++ ) fread(&im[(j*dy+i)*3],1,3,f) ; fclose(f) ; } } void points(float *pts) { glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING) ; glPointSize(5.0); glColor3f(1.0,1.0,1.0); glBegin(GL_POINTS); for ( int i = 0 ; i < 40 ; i++ ) glVertex3fv(&pts[i*3]); glEnd(); glEnable(GL_LIGHTING) ; glEnable(GL_TEXTURE_2D); } void traceQuartVaseBezierGL(void) { glPushMatrix() ; float pts[10][4][3] ; for ( int i = 0 ; i < 10 ; i++ ) { pts[i][0][0] = pts1[i][0] ; pts[i][0][1] = pts1[i][1] ; pts[i][0][2] = 0.0F ; pts[i][1][0] = pts1[i][0] ; pts[i][1][1] = pts1[i][1] ; pts[i][1][2] = p*pts1[i][0] ; pts[i][2][0] = p*pts1[i][0] ; pts[i][2][1] = pts1[i][1] ; pts[i][2][2] = pts1[i][0] ; pts[i][3][0] = 0.0F ; pts[i][3][1] = pts1[i][1] ; pts[i][3][2] = pts1[i][0] ; } glMap2f(GL_MAP2_VERTEX_3, 0.0F,1.0F, 3,4, 0.0F,1.0F, 12,10, (float *) pts); glEnable(GL_MAP2_VERTEX_3); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glMapGrid2f(np1,0.0,1.0,np2,0.0,1.0); if ( ff ) glEvalMesh2(GL_LINE,0,np1,0,np2); else glEvalMesh2(GL_FILL,0,np1,0,np2); if ( pt ) points((float *) pts) ; glPopMatrix() ; } void traceVaseBezierGL(void) { glPushMatrix() ; traceQuartVaseBezierGL() ; glRotatef(90.0F,0.0F,1.0F,0.0F) ; traceQuartVaseBezierGL() ; glRotatef(90.0F,0.0F,1.0F,0.0F) ; traceQuartVaseBezierGL() ; glRotatef(90.0F,0.0F,1.0F,0.0F) ; traceQuartVaseBezierGL() ; glPopMatrix() ; } void CALLBACK display(void) { glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); glPushMatrix(); glEnable(GL_DEPTH_TEST); 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); traceVaseBezierGL() ; glPopMatrix(); glFlush(); auxSwapBuffers(); } void CALLBACK myReshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if( w <= h ) { float y = 7.F*(float)h/(float)w ; glOrtho(-7.,7.,-y,y,-17.,17.); } else { float x = 7.F*(float)w/(float)h ; glOrtho(-x,x,-7.,7.,-17.,17.); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void initlights(void) { GLfloat ambient[] = { 0.2F,0.2F,0.2F,1.0F }; GLfloat diffuse[] = { 2.2F,2.2F,2.2F,1.0F }; GLfloat pos[] = { 0.0F,0.0F,-1.0F,0.0F }; GLfloat spec[] = { 1.0F,1.0F,1.0F,1.0F }; GLfloat shininess[] = { 50.0F }; glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0,GL_AMBIENT,ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse); glLightfv(GL_LIGHT0,GL_POSITION,pos); glMaterialfv(GL_FRONT,GL_SPECULAR,spec); glMaterialfv(GL_FRONT,GL_SHININESS,shininess); } void myinit(void) { glClearColor(0.2F,0.2F,0.2F,1.0F); lectureTexture(tf,iw,ih) ; glMap2f(GL_MAP2_TEXTURE_COORD_2, 0.0F,1.0F,2,2, 0.0F,1.0F,4,2, &texpts[0][0][0]); glEnable(GL_MAP2_TEXTURE_COORD_2); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0,3,iw,ih,0, GL_RGB, GL_UNSIGNED_BYTE, im); glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); initlights(); } void CALLBACK keyp(void) { pt = 1 - pt ; } void CALLBACK keyx(void) { view_rotx += 2 ; } void CALLBACK keyX(void) { view_rotx -= 2 ; } void CALLBACK keyy(void) { view_roty += 2 ; } void CALLBACK keyY(void) { view_roty -= 2 ; } void CALLBACK keyz(void) { view_rotz += 2 ; } void CALLBACK keyZ(void) { view_rotz -= 2 ; } void CALLBACK keya(void) { ff = 1 - ff ; } void CALLBACK keyq(void) { np1++ ; } void CALLBACK keyQ(void) { np1-- ; if ( np1 < 3 ) np1 = 3 ; } void CALLBACK keys(void) { np2++ ; } void CALLBACK keyS(void) { np2-- ; if ( np2 < 3 ) np2 = 3 ; } void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH); auxInitPosition(0,0,300,300); auxInitWindow("Vase par revolution avec texture"); myinit(); auxKeyFunc(AUX_q,keyq) ; auxKeyFunc(AUX_Q,keyQ) ; auxKeyFunc(AUX_s,keys) ; auxKeyFunc(AUX_S,keyS) ; auxKeyFunc(AUX_a,keya) ; auxKeyFunc(AUX_p,keyp) ; 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); }