opengl.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "opengl.h"
  2. #include <a2ri/epaississement.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. vf_model *original;
  6. vf_model *todisplay;
  7. int zoom=0;
  8. float rinc;
  9. float tinc;
  10. int initx,inity,rotation,deplacement;
  11. double angle,angle2,longdiag;
  12. int affiche_normale;
  13. double epaisseur;
  14. void
  15. init (int argc, char **argv)
  16. {
  17. int i, j;
  18. glutInit (&argc, argv);
  19. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  20. glutInitWindowSize (WIDTH, HEIGHT);
  21. glutInitWindowPosition (100, 25);
  22. glutCreateWindow ("Epaississement de surface");
  23. glClearColor (0.0, 0.0, 0.0, 0.0);
  24. glEnable(GL_DEPTH_TEST);
  25. glViewport (0, 0, (GLsizei)WIDTH, (GLsizei)HEIGHT);
  26. rx = ry = rz = tx = ty = tz = 0;
  27. for (i=0; i < NB_VAL; i++)
  28. for (j=0; j < 6; j++)
  29. hist [i][j] = 0;
  30. }
  31. void
  32. display_triangles()
  33. {
  34. for(int i=0;i<todisplay->nbface;i++)
  35. {
  36. int ve1=todisplay->fa[i].ve1;
  37. int ve2=todisplay->fa[i].ve2;
  38. int ve3=todisplay->fa[i].ve3;
  39. glBegin(GL_TRIANGLES);
  40. glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
  41. vector3d AB,AC,norm;
  42. vector3d_init(&AB,
  43. todisplay->ve[ve2].x-todisplay->ve[ve1].x,
  44. todisplay->ve[ve2].y-todisplay->ve[ve1].y,
  45. todisplay->ve[ve2].z-todisplay->ve[ve1].z);
  46. vector3d_init(&AC,
  47. todisplay->ve[ve3].x-todisplay->ve[ve1].x,
  48. todisplay->ve[ve3].y-todisplay->ve[ve1].y,
  49. todisplay->ve[ve3].z-todisplay->ve[ve1].z);
  50. norm=vector3d_vectorialproduct(&AB,&AC);
  51. vector3d_normalize(&norm);
  52. glNormal3f (norm.dx, norm.dy, norm.dz);
  53. glVertex3f(todisplay->ve[ve1].x,
  54. todisplay->ve[ve1].y,
  55. todisplay->ve[ve1].z);
  56. glVertex3f(todisplay->ve[ve2].x,
  57. todisplay->ve[ve2].y,
  58. todisplay->ve[ve2].z);
  59. glVertex3f(todisplay->ve[ve3].x,
  60. todisplay->ve[ve3].y,
  61. todisplay->ve[ve3].z);
  62. glEnd();
  63. if(affiche_normale)
  64. {
  65. glColor4f(0.0f,0.0f,1.0f,1.0f);
  66. glLineWidth(2);
  67. glBegin(GL_LINES);
  68. glVertex3f((todisplay->ve[ve1].x+todisplay->ve[ve2].x+todisplay->ve[ve3].x)/3.0,
  69. (todisplay->ve[ve1].y+todisplay->ve[ve2].y+todisplay->ve[ve3].y)/3.0,
  70. (todisplay->ve[ve1].z+todisplay->ve[ve2].z+todisplay->ve[ve3].z)/3.0);
  71. glVertex3f((todisplay->ve[ve1].x+todisplay->ve[ve2].x+todisplay->ve[ve3].x)/3.0+(norm.dx*longdiag/20.0),
  72. (todisplay->ve[ve1].y+todisplay->ve[ve2].y+todisplay->ve[ve3].y)/3.0+(norm.dy*longdiag/20.0),
  73. (todisplay->ve[ve1].z+todisplay->ve[ve2].z+todisplay->ve[ve3].z)/3.0+(norm.dz*longdiag/20.0));
  74. glEnd();
  75. }
  76. glColor4f(0.0f,0.0f,0.0f,1.0f);
  77. glLineWidth(2);
  78. glBegin(GL_LINES);
  79. glVertex3f(todisplay->ve[ve1].x,
  80. todisplay->ve[ve1].y,
  81. todisplay->ve[ve1].z);
  82. glVertex3f(todisplay->ve[ve2].x,
  83. todisplay->ve[ve2].y,
  84. todisplay->ve[ve2].z);
  85. glVertex3f(todisplay->ve[ve2].x,
  86. todisplay->ve[ve2].y,
  87. todisplay->ve[ve2].z);
  88. glVertex3f(todisplay->ve[ve3].x,
  89. todisplay->ve[ve3].y,
  90. todisplay->ve[ve3].z);
  91. glVertex3f(todisplay->ve[ve3].x,
  92. todisplay->ve[ve3].y,
  93. todisplay->ve[ve3].z);
  94. glVertex3f(todisplay->ve[ve1].x,
  95. todisplay->ve[ve1].y,
  96. todisplay->ve[ve1].z);
  97. glEnd();
  98. }
  99. glFlush();
  100. }
  101. void
  102. transform (int i)
  103. {
  104. glTranslatef((todisplay->xmin+todisplay->xmax)/2.0,
  105. (todisplay->ymin+todisplay->ymax)/2.0,
  106. (todisplay->zmin+todisplay->zmax)/2.0);
  107. glRotatef (hist[i][0], 1.0, 0.0, 0.0);
  108. glRotatef (hist[i][1], 0.0, 1.0, 0.0);
  109. glRotatef (hist[i][2], 0.0, 0.0, 1.0);
  110. glTranslatef(-(todisplay->xmin+todisplay->xmax)/2.0,
  111. -(todisplay->ymin+todisplay->ymax)/2.0,
  112. -(todisplay->zmin+todisplay->zmax)/2.0);
  113. }
  114. void
  115. display ()
  116. {
  117. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  118. glMatrixMode (GL_PROJECTION);
  119. glLoadIdentity ();
  120. gluPerspective (45, WIDTH/((float)HEIGHT), 0.1, 900);
  121. glMatrixMode (GL_MODELVIEW);
  122. glLoadIdentity ();
  123. if(todisplay->zmax==0)
  124. gluLookAt (((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  125. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  126. 70+tz,
  127. ((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  128. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  129. ((todisplay->zmin+todisplay->zmax)/2.0)+tz,
  130. 0, 1, 0);
  131. else
  132. gluLookAt (((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  133. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  134. todisplay->zmax*1.5+tz,
  135. ((todisplay->xmin+todisplay->xmax)/2.0)+tx,
  136. ((todisplay->ymin+todisplay->ymax)/2.0)+ty,
  137. ((todisplay->zmin+todisplay->zmax)/2.0)+tz,
  138. 0, 1, 0);
  139. transform (0);
  140. glTranslatef((todisplay->xmin+todisplay->xmax)/2.0,
  141. (todisplay->ymin+todisplay->ymax)/2.0,
  142. (todisplay->zmin+todisplay->zmax)/2.0);
  143. glRotatef(angle,0.0,1.0,0.0);
  144. glRotatef(angle2,1.0,0.0,0.0);
  145. glTranslatef(-(todisplay->xmin+todisplay->xmax)/2.0,
  146. -(todisplay->ymin+todisplay->ymax)/2.0,
  147. -(todisplay->zmin+todisplay->zmax)/2.0);
  148. display_triangles();
  149. glutSwapBuffers ();
  150. }
  151. void
  152. exitFunc ()
  153. {
  154. a2ri_vf_free(original);
  155. exit (EXIT_SUCCESS);
  156. }
  157. void mouse(int button, int state, int x, int y)
  158. {
  159. if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  160. {
  161. rotation=1;
  162. initx=x;
  163. inity=y;
  164. }
  165. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  166. rotation=0;
  167. if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
  168. {
  169. deplacement=1;
  170. initx=x;
  171. inity=y;
  172. }
  173. if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  174. deplacement=0;
  175. if(button==GLUT_WHEEL_MOUSE_UP)
  176. tz+=longdiag/50;
  177. if(button==GLUT_WHEEL_MOUSE_DOWN)
  178. tz-=longdiag/50;
  179. }
  180. void motion(int x, int y)
  181. {
  182. if(rotation)
  183. {
  184. angle+=(x-initx)*0.5;
  185. angle2+=(y-inity)*0.5;
  186. initx=x;
  187. inity=y;
  188. glutPostRedisplay();
  189. }
  190. if(deplacement)
  191. {
  192. tx-=(x-initx)*0.001*longdiag;
  193. ty+=(y-inity)*0.001*longdiag;
  194. initx=x;
  195. inity=y;
  196. glutPostRedisplay();
  197. }
  198. }
  199. void
  200. keyboard (unsigned char key, int x, int y)
  201. {
  202. int i, j;
  203. char file[100];
  204. //evite les warning
  205. x=y;
  206. y=x;
  207. switch (key)
  208. {
  209. case 'd':
  210. case 'D':
  211. a2ri_vf_display(todisplay);
  212. break;
  213. case 'f':
  214. case 'F':
  215. a2ri_vf_display_detail(todisplay);
  216. break;
  217. case 'o':
  218. case 'O':
  219. todisplay=original;
  220. break;
  221. case 'e':
  222. case 'E':
  223. a2ri_vf_epaissi_surface(original,epaisseur);
  224. break;
  225. case 'n':
  226. case 'N':
  227. affiche_normale=(affiche_normale+1)%2;
  228. break;
  229. case 's':
  230. case 'S':
  231. printf("entrer le nom du fichier :\n");
  232. a2ri_erreur_critique_si(!scanf("%s",file),"erreur\n");
  233. a2ri_vf_save_file(file,todisplay);
  234. break;
  235. case 'Q':
  236. case 'q':
  237. case 27:
  238. exitFunc ();
  239. break;
  240. }
  241. for (i=NB_VAL-1; i > 0; i--)
  242. for (j=0; j < 6; j++)
  243. hist [i][j] = hist [i-1][j];
  244. hist [0][0] = rx;
  245. hist [0][1] = ry;
  246. hist [0][2] = rz;
  247. hist [0][3] = tx;
  248. hist [0][4] = ty;
  249. hist [0][5] = tz;
  250. }
  251. void
  252. idleFunc ()
  253. {
  254. int i, j;
  255. for (i=NB_VAL-1; i > 0; i--)
  256. for (j=0; j < 6; j++)
  257. hist [i][j] = hist [i-1][j];
  258. glutPostRedisplay ();
  259. }
  260. int
  261. go (int argc, char **argv, vf_model *m, double epais)
  262. {
  263. point3d ptmin,ptmax;
  264. epaisseur=epais;
  265. original=m;
  266. todisplay=original;
  267. affiche_normale=0;
  268. rinc=R_INC;
  269. tinc=T_INC;
  270. if(todisplay->zmax-todisplay->zmin!=0)
  271. tinc=(todisplay->zmax-todisplay->zmin)/10;
  272. ptmin.x=todisplay->xmin;
  273. ptmin.y=todisplay->ymin;
  274. ptmin.z=todisplay->zmin;
  275. ptmax.x=todisplay->xmax;
  276. ptmax.y=todisplay->ymax;
  277. ptmax.z=todisplay->zmax;
  278. longdiag=point3d_length(&ptmin,&ptmax);
  279. init (argc, argv);
  280. glutDisplayFunc (display);
  281. glutMouseFunc(mouse);
  282. glutMotionFunc(motion);
  283. glutKeyboardFunc (keyboard);
  284. glutIdleFunc (idleFunc);
  285. glutMainLoop ();
  286. return EXIT_SUCCESS;
  287. }