/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Avril 2001 */ /* Calcul d'une surface parametrique */ /* bicubique */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" struct coord_3D { GLfloat x,y,z ; } ; typedef float matrice[4][4] ; typedef float vecteur[4] ; static GLfloat pts[16][3] = { {-3.0,-3.0,-1.0},{-1.0,-3.0, 1.0},{1.0,-3.0,2.0},{3.0,-3.0,-1.0}, {-3.0,-1.0, 3.0},{-1.0,-1.0, 2.0},{1.0,-1.0,-1.0},{3.0,-1.0, 1.0}, {-3.0, 1.0,-1.0},{-1.0, 1.0,-1.0},{1.0, 1.0, 1.0},{3.0, 1.0,-1.0}, {-3.0, 3.0,-2.0},{-1.0, 3.0, 1.0},{1.0, 3.0, 3.0},{3.0, 3.0, 1.0}} ; static coord_3D *points =(coord_3D *) pts ; static int aff = 0 ; static int pt = 0 ; static int maille = 30 ; static int texture = 1 ; static matrice nrubs = { -0.1666666F, 0.5F, -0.5F, 0.1666666F, 0.5F ,-1.0F, 0.5F, 0.0F, -0.5F , 0.0F, 0.5F, 0.0F, 0.1666666F, 0.6666666F, 0.1666666F,0.0F } ; static matrice cr = { -0.5F, 1.5F,-1.5F, 0.5F, 1.0F,-2.5F, 2.0F,-0.5F, -0.5F, 0.0F, 0.5F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F } ; static matrice bezier = { -1.0F, 3.0F,-3.0F, 1.0F, 3.0F,-6.0F, 3.0F, 0.0F, -3.0F, 3.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F } ; #define LI 64 #define LH 64 GLubyte image[LI][LH][3]; void makeImage(void) { int i,j,c; for( i = 0 ; i < LI ; i++ ) { for( j = 0 ; j < LH ; j++ ) { c =(((i&0x8)==0)^((j&0x8)==0))*255; image[i][j][0] =(GLubyte) 255; image[i][j][1] =(GLubyte) c; image[i][j][2] =(GLubyte) c; } } } void pixel(float x,float y,float z) { glVertex3f(x,y,z); } void vectoriel(struct coord_3D *v1,struct coord_3D *v2,struct coord_3D *v) { v->x = v1->y*v2->z - v1->z*v2->y; v->y = v1->z*v2->x - v1->x*v2->z; v->z = v1->x*v2->y - v1->y*v2->x; } void normalize(struct coord_3D *n) { float d =(float) sqrt(n->x*n->x+n->y*n->y+n->z*n->z); if ( d != 0.0F) { n->x /= d; n->y /= d; n->z /= d;} } void pixel(struct coord_3D *p,struct coord_3D *n,struct coord_3D *t) { glTexCoord2f(t->x,t->y); glNormal3f(n->x,n->y,n->z); glVertex3f(p->x,p->y,p->z); } void transposition(matrice m,matrice t) { for ( int i = 0 ; i < 4 ; i++ ) for ( int j = 0 ; j < 4 ; j++ ) t[i][j] = m[j][i]; } void produitMatriceMatrice(matrice m1,matrice m2,matrice m) { for ( int i = 0 ; i < 4 ; i++ ) for ( int j = 0 ; j < 4 ; j++ ) { m[i][j] = 0 ; for ( int k = 0 ; k < 4 ; k++ ) m[i][j] += m1[i][k]*m2[k][j]; } } void produitMatriceVecteur(matrice m,vecteur v,vecteur r) { for ( int i = 0 ; i < 4 ; i++ ) { r[i] = 0 ; for ( int j = 0 ; j < 4 ; j++ ) r[i] += m[i][j]*v[j]; } } void produitVecteurMatrice(vecteur v,matrice m,vecteur r) { for ( int i = 0 ; i < 4 ; i++ ) { r[i] = 0 ; for ( int j = 0 ; j < 4 ; j++ ) r[i] += v[j]*m[j][i]; } } float produitVecteurVecteur(vecteur v1,vecteur v2) { float r = 0 ; for ( int i = 0 ; i < 4 ; i++ ) r += v1[i]*v2[i]; return(r) ; } void bicubiquePatch(int n,matrice m,matrice mprime,coord_3D *p) { int i,j; struct coord_3D **pts =(struct coord_3D **) malloc(n*sizeof(struct coord_3D *)); struct coord_3D **nms =(struct coord_3D **) malloc(n*sizeof(struct coord_3D *)); struct coord_3D **tex =(struct coord_3D **) malloc(n*sizeof(struct coord_3D *)); for ( i = 0 ; i < n ; i++ ) { tex[i] =(struct coord_3D *) malloc(n*sizeof(struct coord_3D)); pts[i] =(struct coord_3D *) malloc(n*sizeof(struct coord_3D)); nms[i] =(struct coord_3D *) malloc(n*sizeof(struct coord_3D)); } matrice tx,a,aa ; matrice ty,b,bb ; matrice tz,c,cc ; for ( i = 0 ; i < 4 ; i++ ) for ( j = 0 ; j < 4 ; j++ ) { a[i][j] = p[i*4+j].x ; b[i][j] = p[i*4+j].y ; c[i][j] = p[i*4+j].z ; } matrice trans ; transposition(mprime,trans) ; produitMatriceMatrice(m,a,aa) ; produitMatriceMatrice(m,b,bb) ; produitMatriceMatrice(m,c,cc) ; produitMatriceMatrice(aa,trans,tx) ; produitMatriceMatrice(bb,trans,ty) ; produitMatriceMatrice(cc,trans,tz) ; for ( i = 0 ; i < n ; i++ ) { for ( int j = 0 ; j < n ; j++ ) { float s =(float) i/(float) (n-1) ; float t =(float) j/(float) (n-1) ; vecteur S = { s*s*s,s*s,s,1.0F } ; vecteur T = { t*t*t,t*t,t,1.0F } ; vecteur dS = { 3*s*s,2*s,1.0F,0.0F } ; vecteur dT = { 3*t*t,2*t,1.0F,0.0F } ; vecteur d ; coord_3D ds,dt; produitVecteurMatrice(S,tx,d) ; float x = produitVecteurVecteur(d,T) ; produitVecteurMatrice(dS,tx,d) ; ds.x = produitVecteurVecteur(d,T) ; produitVecteurMatrice(S,tx,d) ; dt.x = produitVecteurVecteur(d,dT) ; produitVecteurMatrice(S,ty,d) ; float y = produitVecteurVecteur(d,T) ; produitVecteurMatrice(dS,ty,d) ; ds.y = produitVecteurVecteur(d,T) ; produitVecteurMatrice(S,ty,d) ; dt.y = produitVecteurVecteur(d,dT) ; produitVecteurMatrice(S,tz,d) ; float z = produitVecteurVecteur(d,T) ; produitVecteurMatrice(dS,tz,d) ; ds.z = produitVecteurVecteur(d,T) ; produitVecteurMatrice(S,tz,d) ; dt.z = produitVecteurVecteur(d,dT) ; vectoriel(&dt,&ds,&nms[i][j]); normalize(&nms[i][j]); tex[i][j].x = s; tex[i][j].y = t; pts[i][j].x = x; pts[i][j].y = y; pts[i][j].z = z; } } glBegin(GL_QUADS); for ( i = 0 ; i < n-1 ; i++ ) for ( int j = 0 ; j < n-1 ; j++ ) { pixel(&pts[i][j],&nms[i][j],&tex[i][j]); pixel(&pts[i+1][j],&nms[i+1][j],&tex[i+1][j]); pixel(&pts[i+1][j+1],&nms[i+1][j+1],&tex[i+1][j+1]); pixel(&pts[i][j+1],&nms[i][j+1],&tex[i][j+1]); } glEnd(); for ( i = 0 ; i < n ; i++ ) { free(tex[i]); free(pts[i]); free(nms[i]); } free(tex); free(pts); free(nms); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); glEnable(GL_DEPTH_TEST); glPointSize(5.0); glBegin(GL_POINTS); for ( int i = 0 ; i < 16 ; i++ ) { if ( i != pt ) glColor4fv(couleurJaune()); else glColor4fv(couleurVert()); glVertex3fv(&pts[i][0]); } glEnd(); glColor4fv(couleurCyan()); glEnable(GL_LIGHTING); glPointSize(1.0); if ( texture ) glEnable(GL_TEXTURE_2D); switch ( aff ) { case 0 : bicubiquePatch(maille,bezier,bezier,points); break ; case 1 : bicubiquePatch(maille,bezier,cr,points); break ; case 2 : bicubiquePatch(maille,cr,bezier,points); break ; case 3 : bicubiquePatch(maille,cr,cr,points); break ; case 4 : bicubiquePatch(maille,nrubs,nrubs,points); break ; case 5 : bicubiquePatch(maille,nrubs,cr,points); break ; case 6 : bicubiquePatch(maille,cr,nrubs,points); break ; } glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glPopMatrix(); glFlush(); glutSwapBuffers(); } void myinit(void) { makeImage(); glTexImage2D(GL_TEXTURE_2D,0,3,LI,LH,0,GL_RGB,GL_UNSIGNED_BYTE,&image[0][0][0]); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glClearColor(0.0,0.0,0.0,1.0); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); GLfloat shinines[] = { 50.0 }; GLfloat l_pos[] = { 1.0,1.0,1.0,0.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc()); glMaterialfv(GL_FRONT,GL_SHININESS,shinines); glLightfv(GL_LIGHT0,GL_POSITION,l_pos); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 't' : case 'T' : texture = (texture+1)%2; glutPostRedisplay(); break; case 45 : maille-- ; if ( maille < 5 ) maille = 5 ; glutPostRedisplay(); break; case 43 : maille++ ; glutPostRedisplay(); break; case 0x0D : aff =(aff+1)%8 ; glutPostRedisplay(); break; case 32 : pt = (pt+1)%16 ; glutPostRedisplay(); break; } } void special(int k, int x, int y) { switch (k) { case GLUT_KEY_LEFT : points[pt].x -= 0.1F; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : points[pt].x += 0.1F; glutPostRedisplay(); break; case GLUT_KEY_UP : points[pt].y += 0.1F; glutPostRedisplay(); break; case GLUT_KEY_DOWN : points[pt].y -= 0.1F; glutPostRedisplay(); break; case GLUT_KEY_PAGE_UP : points[pt].z += 0.1F ; glutPostRedisplay(); break; case GLUT_KEY_PAGE_DOWN : points[pt].z += 0.1F ; glutPostRedisplay(); break; } } void select1(int selection) { switch (selection) { case 1 : texture = 1; glutPostRedisplay(); break; case 2 : texture = 0; glutPostRedisplay(); break; } } void select2(int selection) { switch (selection) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : aff = selection ; glutPostRedisplay(); break; } } void select3(int selection) { switch (selection) { case 10 : maille = 5; glutPostRedisplay(); break; case 11 : maille = 10; glutPostRedisplay(); break; case 12 : maille = 25; glutPostRedisplay(); break; case 13 : maille = 50; glutPostRedisplay(); break; case 14 : maille = 100; glutPostRedisplay(); break; case 6 : maille++; glutPostRedisplay(); break; case 7 : maille--; if ( maille < 3 ) maille = 3; glutPostRedisplay(); break; } } void select(int selection) { switch (selection) { case 1 : pt = (pt+1)%4; glutPostRedisplay(); break; case 0 : exit(0); } } void creationMenu(void) { int menu1 = glutCreateMenu(select1); glutAddMenuEntry("Oui",1); glutAddMenuEntry("Non",2); int menu2 = glutCreateMenu(select2); glutAddMenuEntry("Bezier x Bezier",0); glutAddMenuEntry("Bezier x Catmull Rom",1); glutAddMenuEntry("Catmull Rom x Bezier",2); glutAddMenuEntry("Catmull Rom x Catmull Rom",3); glutAddMenuEntry("NRUBS x NRUBS",4); glutAddMenuEntry("NRUBS x Catmull Rom",5); glutAddMenuEntry("Catmull Rom x NRUBS",6); int menu3 = glutCreateMenu(select3); glutAddMenuEntry("5",10); glutAddMenuEntry("10",11); glutAddMenuEntry("25",12); glutAddMenuEntry("50",13); glutAddMenuEntry("100",14); glutAddMenuEntry("Augmenter",6); glutAddMenuEntry("Reduire",7); glutCreateMenu(select); glutAddSubMenu("Affichage",menu2); glutAddSubMenu("Discretisation",menu3); glutAddSubMenu("Texture",menu1); glutAddMenuEntry("Changer point",1); glutAddMenuEntry("Quitter",0); glutAttachMenu(GLUT_RIGHT_BUTTON); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Surface bicubique"); myinit(); creationMenu(); setParametresOrthoBasique(-5.0,5.0,-5.0,5.0,-500.0,500.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(special); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutMainLoop(); return(0); }