/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2019 */ /* Calcul d'une surface parametrique */ /* bicubique avec affichage de ses normales */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleFleche.h" struct coord_3D { GLfloat x = 0.0F; GLfloat y = 0.0F; GLfloat z = 0.0F; GLfloat w = 1.0F; }; typedef struct coord_3D coord_3D; typedef float matrice[4][4]; typedef float vecteur[4]; static GLfloat pts[16][4] = { {-3.0F,-3.0F,-1.0F, 1.0F },{-1.0F,-3.0F, 1.0F, 1.0F }, { 1.0F,-3.0F, 2.0F, 1.0F },{ 3.0F,-3.0F,-1.0F, 1.0F }, {-3.0F,-1.0F, 3.0F, 1.0F },{-1.0F,-1.0F, 2.0F, 1.0F }, { 1.0F,-1.0F,-1.0F, 1.0F },{ 3.0F,-1.0F, 1.0F, 1.0F }, {-3.0F, 1.0F,-1.0F, 1.0F },{-1.0F, 1.0F,-1.0F, 1.0F }, { 1.0F, 1.0F, 1.0F, 1.0F },{ 3.0F, 1.0F,-1.0F, 1.0F }, {-3.0F, 3.0F,-2.0F, 1.0F },{-1.0F, 3.0F, 1.0F, 1.0F }, { 1.0F, 3.0F, 3.0F, 1.0F },{ 3.0F, 3.0F, 1.0F, 1.0F }}; static coord_3D *points =(coord_3D *) pts ; static int aff = 0 ; static int pt = 0 ; static int maille = 10 ; static int tg = 1 ; static int s = 5 ; static int t = 5 ; 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 } ; void point(coord_3D *p,coord_3D *n,coord_3D *t) { glTexCoord2f(t->x,t->y); glNormal3f(n->x,n->y,n->z); glVertex3f(p->x,p->y,p->z); } /* ATTENTION */ /* Les produits matrice-vecteur, */ /* matrice-matrice, la transposition */ /* et le produit vectoriel implantes */ /* ci-dessous necessitent */ /* que les parametres resultats */ /* soient des tableaux et structures */ /* differents des parametres d'entree */ void vectoriel(coord_3D *v1,coord_3D *v2,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(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 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; coord_3D **pts =(coord_3D **) malloc(n*sizeof(coord_3D *)); coord_3D **nms =(coord_3D **) malloc(n*sizeof(coord_3D *)); coord_3D **tex =(coord_3D **) malloc(n*sizeof(coord_3D *)); for ( i = 0 ; i < n ; i++ ) { tex[i] =(coord_3D *) malloc(n*sizeof(coord_3D)); pts[i] =(coord_3D *) malloc(n*sizeof(coord_3D)); nms[i] =(coord_3D *) malloc(n*sizeof(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) ; normalize(&dt); normalize(&ds); vectoriel(&dt,&ds,&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++ ) { point(&pts[i][j],&nms[i][j],&tex[i][j]); point(&pts[i+1][j],&nms[i+1][j],&tex[i+1][j]); point(&pts[i+1][j+1],&nms[i+1][j+1],&tex[i+1][j+1]); point(&pts[i][j+1],&nms[i][j+1],&tex[i][j+1]); } glEnd(); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRougeFonce()); if ( tg ) for ( i = 0 ; i < n ; i++ ) for ( int j = 0 ; j < n ; j++ ) { glPushMatrix(); glTranslatef(pts[i][j].x,pts[i][j].y,pts[i][j].z); flecheEnVolume(nms[i][j].x,nms[i][j].y,nms[i][j].z,0.04F,0.3F,0.01F) ; glPopMatrix(); } else { glPushMatrix(); glTranslatef(pts[s][t].x,pts[s][t].y,pts[s][t].z); flecheEnVolume(nms[s][t].x,nms[t][t].y,nms[s][t].z,0.04F,0.3F,0.01F) ; glPopMatrix(); } glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisClair()); glDisable(GL_LIGHT1); glDisable(GL_LIGHT2); 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); 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_LIGHTING); glDisable(GL_DEPTH_TEST); glPopMatrix(); glFlush(); glutSwapBuffers(); } void init(void) { glClearColor(0.0,0.0,0.0,1.0); glShadeModel(GL_SMOOTH); glDepthFunc(GL_LESS); GLfloat shinines[] = { 50.0 }; GLfloat l_pos0[] = { 1.0,1.0,1.0,0.0 }; GLfloat l_pos1[] = { -1.0,0.0,1.0,0.0 }; GLfloat l_pos2[] = { -1.0,0.0,1.0,0.0 }; glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisClair()); glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc()); glMaterialfv(GL_FRONT,GL_SHININESS,shinines); glLightfv(GL_LIGHT0,GL_POSITION,l_pos0); glLightfv(GL_LIGHT1,GL_POSITION,l_pos1); glLightfv(GL_LIGHT1,GL_POSITION,l_pos2); glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc()); glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBlanc()); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); } void key(unsigned char key,int x,int y) { if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 32 : tg = (tg+1)%2; glutPostRedisplay(); break; case 45 : maille-- ; if ( maille < 5 ) maille = 5 ; glutPostRedisplay(); break; case 43 : maille++ ; glutPostRedisplay(); break; case 's' : s = (s+1)%maille ; glutPostRedisplay(); break; case 'S' : s-- ; if ( s < 0 ) s = 0; glutPostRedisplay(); break; case 't' : t = (t+1)%maille ; glutPostRedisplay(); break; case 'T' : t-- ; if ( t < 0 ) t = 0; glutPostRedisplay(); break; case 0x0D : aff =(aff+1)%8 ; glutPostRedisplay(); break; case 'p' : case 'P' : 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 : tg = 1; glutPostRedisplay(); break; case 2 : tg = 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("Toutes",1); glutAddMenuEntry("Une",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("Normales",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 avec normales"); init(); 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); }