opengl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include "opengl.h"
  2. #include <a2ri/face.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <a2ri/subdivision.h>
  6. #include <a2ri/quality.h>
  7. vf_model *modeltodisplay;
  8. double vpx,vpy,vpz,cx,cy,cz;
  9. double rinc,tinc,tx,ty,tz;
  10. GLfloat matShininess = 16.0f;
  11. int initx,inity,rotation,deplacement;
  12. double angle,angle2,longdiag;
  13. int affiche_normale;
  14. double *ar,min_ar,max_ar;
  15. int size_ar;
  16. int *ar_int;
  17. int *liste_vert=NULL;
  18. int *liste_rouge=NULL;
  19. int size_liste_couleur=0;
  20. double seuil;
  21. void
  22. init (int argc, char **argv)
  23. {
  24. GLfloat matR = 1.0f;
  25. GLfloat matG = 1.0f;
  26. GLfloat matB = 1.0f;
  27. GLfloat matShininess = 10.0f;
  28. GLfloat matGray[]= {0.1f, 0.1f, 0.1f, 1.0f};
  29. GLfloat mat[] = {matR, matG, matB, 1.0f};
  30. glutInit (&argc, argv);
  31. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  32. glutInitWindowSize (WIDTH, HEIGHT);
  33. glutInitWindowPosition (100, 25);
  34. glutCreateWindow ("Visualisation de l aspect ratio des triangles");
  35. glClearColor (255.0, 255.0, 255.0, 0.0);
  36. glEnable(GL_DEPTH_TEST);
  37. glCullFace(GL_BACK);
  38. glEnable(GL_CULL_FACE);
  39. glEnable(GL_LIGHT0);
  40. glShadeModel(GL_SMOOTH);
  41. GLfloat ambientlight[]={1.0f,1.0f,1.0f,1.0f};
  42. GLfloat diffuselight[]={1.0f,1.0f,1.0f,1.0f};
  43. GLfloat specularlight[]={1.0f,1.0f,1.0f,1.0f};
  44. GLfloat position[]={vpx,vpy,vpz+tz,1.0f};
  45. if(modeltodisplay->zmax<0)
  46. position[2]*=-1.0;
  47. glLightfv(GL_LIGHT0, GL_AMBIENT, ambientlight);
  48. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuselight);
  49. glLightfv(GL_LIGHT0, GL_SPECULAR, specularlight);
  50. glLightfv(GL_LIGHT0, GL_POSITION, position);
  51. glMaterialfv (GL_FRONT, GL_AMBIENT, matGray);
  52. glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
  53. glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
  54. glMaterialf (GL_FRONT, GL_SHININESS, matShininess);
  55. glViewport (0, 0, (GLsizei)WIDTH, (GLsizei)HEIGHT);
  56. }
  57. void
  58. display_triangles()
  59. {
  60. GLfloat matBlue[] = {0.7f, 0.5f, 0.1f, 1.0f};
  61. glMaterialf (GL_FRONT, GL_SHININESS, matShininess);
  62. for(int i=0;i<modeltodisplay->nbface;i++)
  63. {
  64. int ve1=modeltodisplay->fa[i].ve1;
  65. int ve2=modeltodisplay->fa[i].ve2;
  66. int ve3=modeltodisplay->fa[i].ve3;
  67. if(ar[i]<seuil)
  68. {
  69. glBegin(GL_TRIANGLES);
  70. //glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
  71. //glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
  72. glColor4f((liste_rouge[i]*1.0)/255.0, (liste_vert[i]*1.0)/255.0, 0.0f, 1.0f);
  73. vector3d AB,AC,norm;
  74. vector3d_init(&AB,
  75. modeltodisplay->ve[ve2].x-modeltodisplay->ve[ve1].x,
  76. modeltodisplay->ve[ve2].y-modeltodisplay->ve[ve1].y,
  77. modeltodisplay->ve[ve2].z-modeltodisplay->ve[ve1].z);
  78. vector3d_init(&AC,
  79. modeltodisplay->ve[ve3].x-modeltodisplay->ve[ve1].x,
  80. modeltodisplay->ve[ve3].y-modeltodisplay->ve[ve1].y,
  81. modeltodisplay->ve[ve3].z-modeltodisplay->ve[ve1].z);
  82. norm=vector3d_vectorialproduct(&AB,&AC);
  83. vector3d_normalize(&norm);
  84. glMaterialfv (GL_FRONT, GL_DIFFUSE, matBlue);
  85. glNormal3f (norm.dx, norm.dy, norm.dz);
  86. glVertex3f(modeltodisplay->ve[ve1].x,
  87. modeltodisplay->ve[ve1].y,
  88. modeltodisplay->ve[ve1].z);
  89. glVertex3f(modeltodisplay->ve[ve2].x,
  90. modeltodisplay->ve[ve2].y,
  91. modeltodisplay->ve[ve2].z);
  92. glVertex3f(modeltodisplay->ve[ve3].x,
  93. modeltodisplay->ve[ve3].y,
  94. modeltodisplay->ve[ve3].z);
  95. glEnd();
  96. if(affiche_normale)
  97. {
  98. glColor4f(0.0f,0.0f,1.0f,1.0f);
  99. glLineWidth(2);
  100. glBegin(GL_LINES);
  101. glVertex3f((modeltodisplay->ve[ve1].x+modeltodisplay->ve[ve2].x+modeltodisplay->ve[ve3].x)/3.0,
  102. (modeltodisplay->ve[ve1].y+modeltodisplay->ve[ve2].y+modeltodisplay->ve[ve3].y)/3.0,
  103. (modeltodisplay->ve[ve1].z+modeltodisplay->ve[ve2].z+modeltodisplay->ve[ve3].z)/3.0);
  104. glVertex3f((modeltodisplay->ve[ve1].x+modeltodisplay->ve[ve2].x+modeltodisplay->ve[ve3].x)/3.0+(norm.dx*longdiag/20.0),
  105. (modeltodisplay->ve[ve1].y+modeltodisplay->ve[ve2].y+modeltodisplay->ve[ve3].y)/3.0+(norm.dy*longdiag/20.0),
  106. (modeltodisplay->ve[ve1].z+modeltodisplay->ve[ve2].z+modeltodisplay->ve[ve3].z)/3.0+(norm.dz*longdiag/20.0));
  107. glEnd();
  108. }
  109. glColor4f(0.0f,0.0f,0.0f,1.0f);
  110. glLineWidth(2);
  111. glBegin(GL_LINES);
  112. glVertex3f(modeltodisplay->ve[ve1].x,
  113. modeltodisplay->ve[ve1].y,
  114. modeltodisplay->ve[ve1].z);
  115. glVertex3f(modeltodisplay->ve[ve2].x,
  116. modeltodisplay->ve[ve2].y,
  117. modeltodisplay->ve[ve2].z);
  118. glVertex3f(modeltodisplay->ve[ve2].x,
  119. modeltodisplay->ve[ve2].y,
  120. modeltodisplay->ve[ve2].z);
  121. glVertex3f(modeltodisplay->ve[ve3].x,
  122. modeltodisplay->ve[ve3].y,
  123. modeltodisplay->ve[ve3].z);
  124. glVertex3f(modeltodisplay->ve[ve3].x,
  125. modeltodisplay->ve[ve3].y,
  126. modeltodisplay->ve[ve3].z);
  127. glVertex3f(modeltodisplay->ve[ve1].x,
  128. modeltodisplay->ve[ve1].y,
  129. modeltodisplay->ve[ve1].z);
  130. glEnd();
  131. }
  132. }
  133. glFlush();
  134. }
  135. void
  136. display ()
  137. {
  138. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  139. glMatrixMode (GL_PROJECTION);
  140. glLoadIdentity ();
  141. gluPerspective (45, WIDTH/((float)HEIGHT), 0.1, 5000);
  142. glMatrixMode (GL_MODELVIEW);
  143. glLoadIdentity ();
  144. gluLookAt (vpx+tx,vpy+ty,vpz+tz,cx+tx,cy+ty,cz+tz,0,1,0);
  145. glTranslatef((modeltodisplay->xmin+modeltodisplay->xmax)/2.0,
  146. (modeltodisplay->ymin+modeltodisplay->ymax)/2.0,
  147. (modeltodisplay->zmin+modeltodisplay->zmax)/2.0);
  148. glRotatef(angle,0.0,1.0,0.0);
  149. glRotatef(angle2,1.0,0.0,0.0);
  150. glTranslatef(-(modeltodisplay->xmin+modeltodisplay->xmax)/2.0,
  151. -(modeltodisplay->ymin+modeltodisplay->ymax)/2.0,
  152. -(modeltodisplay->zmin+modeltodisplay->zmax)/2.0);
  153. display_triangles();
  154. glutSwapBuffers ();
  155. }
  156. void
  157. compute_aspect_ratio()
  158. {
  159. free(ar);
  160. ar=NULL;
  161. free(liste_vert);
  162. free(liste_rouge);
  163. liste_vert=NULL;
  164. liste_rouge=NULL;
  165. size_liste_couleur=0;
  166. size_ar=0;
  167. free(ar_int);
  168. ar_int=0;
  169. for(int i=0;i<modeltodisplay->nbface;i++)
  170. {
  171. double val=a2ri_vf_triangle_aspect_ratio(modeltodisplay,i);
  172. //val=sqrt(val);
  173. if(i==0)
  174. {
  175. min_ar=val;
  176. max_ar=val;
  177. }
  178. else
  179. {
  180. if(val>max_ar)
  181. max_ar=val;
  182. if(val<min_ar)
  183. min_ar=val;
  184. }
  185. list_double_add(&ar,&size_ar,val,WITH_REDUNDANCE);
  186. }
  187. printf("min : %lf - max : %lf\n",min_ar,max_ar);
  188. seuil=max_ar+0.25;
  189. min_ar=1.1547;
  190. if(max_ar>10)
  191. max_ar=10;
  192. size_ar=0;
  193. for(int i=0;i<modeltodisplay->nbface;i++)
  194. {
  195. int val=(int)(((ar[i]-min_ar)/(max_ar-min_ar))*65535.0);
  196. if(val>65535)
  197. val=65535;
  198. list_int_add(&ar_int,&size_ar,(int)(((ar[i]-min_ar)/(max_ar-min_ar))*65535.0),WITH_REDUNDANCE);
  199. }
  200. liste_vert=NULL;
  201. liste_rouge=NULL;
  202. size_liste_couleur=0;
  203. for(int i=0;i<modeltodisplay->nbface;i++)
  204. {
  205. if(ar_int[i]<32767)
  206. {
  207. list_int_add(&liste_rouge,&size_liste_couleur,ar_int[i]/128,WITH_REDUNDANCE);
  208. size_liste_couleur--;
  209. list_int_add(&liste_vert,&size_liste_couleur,255,WITH_REDUNDANCE);
  210. }
  211. else
  212. {
  213. list_int_add(&liste_rouge,&size_liste_couleur,255,WITH_REDUNDANCE);
  214. size_liste_couleur--;
  215. list_int_add(&liste_vert,&size_liste_couleur,255-((ar_int[i]-32767)/128),WITH_REDUNDANCE);
  216. }
  217. }
  218. }
  219. void
  220. exitFunc ()
  221. {
  222. a2ri_vf_free(modeltodisplay);
  223. exit (EXIT_SUCCESS);
  224. }
  225. void mouse(int button, int state, int x, int y)
  226. {
  227. if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  228. {
  229. rotation=1;
  230. initx=x;
  231. inity=y;
  232. }
  233. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  234. rotation=0;
  235. if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
  236. {
  237. deplacement=1;
  238. initx=x;
  239. inity=y;
  240. }
  241. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  242. deplacement=0;
  243. if(button==GLUT_WHEEL_MOUSE_UP)
  244. tz+=longdiag/50;
  245. if(button==GLUT_WHEEL_MOUSE_DOWN)
  246. tz-=longdiag/50;
  247. }
  248. void motion(int x, int y)
  249. {
  250. if(rotation)
  251. {
  252. angle+=(x-initx)*0.5;
  253. angle2+=(y-inity)*0.5;
  254. initx=x;
  255. inity=y;
  256. glutPostRedisplay();
  257. }
  258. if(deplacement)
  259. {
  260. tx-=(x-initx)*0.001*longdiag;
  261. ty+=(y-inity)*0.001*longdiag;
  262. initx=x;
  263. inity=y;
  264. glutPostRedisplay();
  265. }
  266. }
  267. void
  268. keyboard (unsigned char key, int x, int y)
  269. {
  270. switch (key)
  271. {
  272. case 'l':
  273. case 'L':
  274. a2ri_vf_loop(modeltodisplay,1);
  275. compute_aspect_ratio();
  276. break;
  277. case 's':
  278. case 'S':
  279. printf("entrez le nouveau seuil :");
  280. a2ri_erreur_critique_si(!scanf("%lf",&seuil),"erreur seuil\n");
  281. break;
  282. case 't':
  283. case 'T':
  284. if(glIsEnabled(GL_LIGHTING))
  285. glDisable(GL_LIGHTING);
  286. else
  287. glEnable(GL_LIGHTING);
  288. break;
  289. case 'n':
  290. case 'N':
  291. affiche_normale=(affiche_normale+1)%2;
  292. break;
  293. case '+':
  294. if(glIsEnabled(GL_LIGHTING))
  295. {
  296. matShininess/=2.0;
  297. if(matShininess<1.0)
  298. matShininess=1.0;
  299. }
  300. else
  301. {
  302. seuil+=0.25;
  303. printf("seuil : %lf\n",seuil);
  304. }
  305. break;
  306. case ':':
  307. a2ri_vf_display(modeltodisplay);
  308. break;
  309. case '-':
  310. if(glIsEnabled(GL_LIGHTING))
  311. {
  312. matShininess*=2.0;
  313. if(matShininess>128.0)
  314. matShininess=128.0;
  315. }
  316. else
  317. {
  318. seuil-=0.25;
  319. if(seuil<1.1547)
  320. seuil+=0.25;
  321. printf("seuil : %lf\n",seuil);
  322. }
  323. break;
  324. case 'q':
  325. case 'Q':
  326. case 27:
  327. exitFunc ();
  328. break;
  329. }
  330. }
  331. void
  332. idleFunc ()
  333. {
  334. glutPostRedisplay ();
  335. }
  336. int
  337. go (int argc, char **argv, vf_model *m)
  338. {
  339. point3d ptmin,ptmax;
  340. modeltodisplay=m;
  341. ar=NULL;
  342. ar_int=NULL;
  343. size_ar=0;
  344. liste_vert=NULL;
  345. liste_rouge=NULL;
  346. size_liste_couleur=0;
  347. compute_aspect_ratio();
  348. rinc=R_INC;
  349. tinc=T_INC;
  350. if(modeltodisplay->zmax-modeltodisplay->zmin!=0)
  351. tinc=(modeltodisplay->zmax-modeltodisplay->zmin)/10;
  352. if(modeltodisplay->zmax!=0)
  353. vpz=modeltodisplay->zmax*1.5+tz;
  354. else
  355. vpz=70;
  356. ptmin.x=modeltodisplay->xmin;
  357. ptmin.y=modeltodisplay->ymin;
  358. ptmin.z=modeltodisplay->zmin;
  359. ptmax.x=modeltodisplay->xmax;
  360. ptmax.y=modeltodisplay->ymax;
  361. ptmax.z=modeltodisplay->zmax;
  362. longdiag=point3d_length(&ptmin,&ptmax);
  363. vpx=(modeltodisplay->xmin+modeltodisplay->xmax)/2.0;
  364. vpy=(modeltodisplay->ymin+modeltodisplay->ymax)/2.0;
  365. cx=(modeltodisplay->xmin+modeltodisplay->xmax)/2.0;
  366. cy=(modeltodisplay->ymin+modeltodisplay->ymax)/2.0;
  367. cz=(modeltodisplay->zmin+modeltodisplay->zmax)/2.0;
  368. angle=0;
  369. angle2=0;
  370. tx = ty = tz = 0;
  371. affiche_normale=0;
  372. init (argc, argv);
  373. glutDisplayFunc (display);
  374. glutMouseFunc(mouse);
  375. glutMotionFunc(motion);
  376. glutKeyboardFunc (keyboard);
  377. glutIdleFunc (idleFunc);
  378. glutMainLoop ();
  379. return EXIT_SUCCESS;
  380. }