Advertisement
marwanpro

xaxaxaxaxa

Oct 25th, 2017
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.75 KB | None | 0 0
  1. #include <cassert>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <iostream>
  5.  
  6. #include "draw.h" // pour dessiner du point de vue d'une camera
  7. #include "Viewer.h"
  8.  
  9. using namespace std;
  10.  
  11.  
  12. Viewer::Viewer() : App(1024, 768), mb_cullface(true), mb_wireframe(false), b_draw_grid(true), b_draw_axe(true),
  13. b_draw_animation(false)
  14. {
  15. }
  16.  
  17.  
  18. void Viewer::help()
  19. {
  20. printf("HELP:\n");
  21. printf("\th: help\n");
  22. printf("\tc: (des)active GL_CULL_FACE\n");
  23. printf("\tw: (des)active wireframe\n");
  24. printf("\ta: (des)active l'affichage de l'axe\n");
  25. printf("\tg: (des)active l'affichage de la grille\n");
  26. printf("\tz: (des)active l'affichage de la courbe d'animation\n");
  27. printf("\tfleches/pageUp/pageDown: bouge la caméra\n");
  28. printf("\tCtrl+fleche/pageUp/pageDown: bouge la source de lumière\n");
  29. printf("\tSouris+bouton gauche: rotation\n");
  30. printf("\tSouris mouvement vertical+bouton droit: (de)zoom\n");
  31. SDL_Delay(150);
  32. }
  33.  
  34. int Viewer::init()
  35. {
  36. // etat par defaut openGL
  37. glClearColor(0.5f, 0.5f, 0.9f, 1);
  38. glClearDepthf(1);
  39. glDepthFunc(GL_LESS);
  40. glEnable(GL_DEPTH_TEST);
  41. glFrontFace(GL_CCW);
  42. glCullFace(GL_BACK);
  43.  
  44. if (mb_cullface)
  45. glEnable(GL_CULL_FACE);
  46. else
  47. glDisable(GL_CULL_FACE); // good for debug
  48. glEnable(GL_TEXTURE_2D);
  49.  
  50. glEnable(GL_BLEND);
  51. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  52. //glAlphaFunc(GL_GREATER, 0.5);
  53. //glEnable(GL_ALPHA_TEST);
  54.  
  55. m_anim.init("data/animation/anim1.ani");
  56.  
  57. m_camera.lookat(Point(0, 0, 0), 30);
  58. gl.light(Point(0, 20, 20), White());
  59.  
  60. init_axe();
  61. init_grid();
  62. init_cube();
  63. //init_quad();
  64. /*init_cylindre();
  65. init_couvercle();
  66. init_cone();
  67. init_sphere();*/
  68.  
  69. init_cylindre();
  70. init_couvercle();
  71.  
  72.  
  73. return 0;
  74. }
  75.  
  76.  
  77. void Viewer::init_axe()
  78. {
  79. m_axe = Mesh(GL_LINES);
  80. m_axe.color(Color(1, 0, 0));
  81. m_axe.vertex(0, 0, 0);
  82. m_axe.vertex(1, 0, 0);
  83.  
  84. m_axe.color(Color(0, 1, 0));
  85. m_axe.vertex(0, 0, 0);
  86. m_axe.vertex(0, 1, 0);
  87.  
  88. m_axe.color(Color(0, 0, 1));
  89. m_axe.vertex(0, 0, 0);
  90. m_axe.vertex(0, 0, 1);
  91. }
  92.  
  93.  
  94. void Viewer::init_grid()
  95. {
  96. m_grid = Mesh(GL_LINES);
  97.  
  98. m_grid.color(Color(1, 1, 1));
  99. int i, j;
  100. for (i = -5; i <= 5; ++i)
  101. for (j = -5; j <= 5; ++j)
  102. {
  103. m_grid.vertex(-5, 0, j);
  104. m_grid.vertex(5, 0, j);
  105.  
  106. m_grid.vertex(i, 0, -5);
  107. m_grid.vertex(i, 0, 5);
  108. }
  109. }
  110.  
  111.  
  112. void Viewer::init_cube()
  113. {
  114. // 0 1 2 3 4 5 6 7
  115. static float pt[8][3] = {{-1,-1,-1}, {1,-1,-1}, {1,-1,1}, {-1,-1,1}, {-1,1,-1}, {1,1,-1}, {1,1,1}, {-1,1,1}};
  116. static int f[6][4] = {{0,1,2,3}, {5,4,7,6}, {2,1,5,6}, {0,3,7,4}, {3,2,6,7}, {1,0,4,5}};
  117. static float n[6][3] = {{0,-1,0}, {0,1,0}, {1,0,0}, {-1,0,0}, {0,0,1}, {0,0,-1}};
  118. int i, j;
  119.  
  120. m_cube = Mesh(GL_TRIANGLE_STRIP);
  121. m_cube.color(Color(1, 1, 1));
  122.  
  123. m_cube_texture = read_texture(0, "data/debug2x2red.png");
  124.  
  125. for (i = 0; i < 6; i++)
  126. {
  127. m_cube.normal(n[i][0], n[i][1], n[i][2]);
  128.  
  129. m_cube.texcoord(0, 0);
  130. m_cube.vertex(pt[ f[i][0] ][0], pt[ f[i][0] ][1], pt[ f[i][0] ][2]);
  131.  
  132. m_cube.texcoord(1, 0);
  133. m_cube.vertex(pt[ f[i][1] ][0], pt[ f[i][1] ][1], pt[ f[i][1] ][2]);
  134.  
  135. m_cube.texcoord(0, 1);
  136. m_cube.vertex(pt[ f[i][3] ][0], pt[ f[i][3] ][1], pt[ f[i][3] ][2]);
  137.  
  138. m_cube.texcoord(1, 1);
  139. m_cube.vertex(pt[ f[i][2] ][0], pt[ f[i][2] ][1], pt[ f[i][2] ][2]);
  140.  
  141. m_cube.restart_strip();
  142. }
  143. }
  144.  
  145.  
  146. void Viewer::init_quad()
  147. {
  148. m_quad = Mesh(GL_TRIANGLE_STRIP);
  149. m_quad.color(Color(1, 1, 1));
  150.  
  151. m_quad_texture = read_texture(0, "data/papillon.png");
  152.  
  153. m_quad.normal(0, 0, 1);
  154.  
  155. m_quad.texcoord(0, 0);
  156. m_quad.vertex(-1, -1, 0);
  157.  
  158. m_quad.texcoord(1, 0);
  159. m_quad.vertex(1, -1, 0);
  160.  
  161. m_quad.texcoord(0, 1);
  162. m_quad.vertex(-1, 1, 0);
  163.  
  164. m_quad.texcoord(1, 1);
  165. m_quad.vertex(1, 1, 0);
  166. }
  167.  
  168. /*
  169. void Viewer::init_cylindre()
  170. {
  171. const int div = 25;
  172. const float step = 2 * M_PI / div;
  173.  
  174. m_cylindre = Mesh(GL_TRIANGLE_STRIP);
  175. m_cylindre.color(Color(1, 1, 1));
  176. for (int i = 0; i < div + 1; i++)
  177. {
  178. float alpha = i * step;
  179. m_cylindre.normal(Vector(cos(alpha), 0, sin(alpha)));
  180. m_cylindre.vertex(Point(cos(alpha), -1, sin(alpha)));
  181. m_cylindre.normal(Vector(cos(alpha), 0, sin(alpha)));
  182. m_cylindre.vertex(Point(cos(alpha), 1, sin(alpha)));
  183. }
  184. }
  185. /*
  186. void Viewer::init_couvercle()
  187. {
  188. const int div = 25;
  189. float step = 2 * M_PI / div;
  190. m_couvercle = Mesh(GL_TRIANGLE_FAN);
  191. m_couvercle.normal(Vector(0, 1, 0));
  192. m_couvercle.vertex(Point(0, 1, 0));
  193. m_cylindre.color(Color(1, 1, 1));
  194. for (int i = 0; i < div + 1; i++)
  195. {
  196. float alpha = i * step;
  197. m_couvercle.vertex(Point(cos(alpha), 1, sin(alpha)));
  198. }
  199. }
  200.  
  201. void Viewer::init_cone()
  202. {
  203. const int div = 25;
  204. float step = 2 * M_PI / div;
  205. m_cone = Mesh(GL_TRIANGLE_STRIP);
  206. for (int i = 0; i < div + 1; i++)
  207. {
  208. float alpha = i * step;
  209. m_cone.normal(Vector(cos(alpha) / sqrt(2), 1 / sqrt(2), sin(alpha) / sqrt(2)));
  210. m_cone.vertex(Point(cos(alpha), 0, sin(alpha)));
  211. m_cone.vertex(Point(0, 1, 0));
  212. }
  213. }
  214.  
  215. void Viewer::init_sphere()
  216. {
  217. const int divbeta = 25;
  218. const int divalpha = divbeta / 2;
  219. float alpha, alpha2, beta;
  220.  
  221. m_sphere = Mesh(GL_TRIANGLE_STRIP);
  222. for (int i = 0; i < divalpha + 1; i++)
  223. {
  224. alpha = -0.5 * M_PI + float(i) * M_PI / divalpha;
  225. alpha2 = -0.5 * M_PI + float(i + 1) * M_PI / divalpha;
  226. for (float j = 0; j < divbeta + 1; j++)
  227. {
  228. beta = float(j) * 2 * M_PI / divbeta;
  229. m_sphere.normal(Vector(cos(alpha) * cos(beta), sin(alpha), cos(alpha) * sin(beta)));
  230. m_sphere.vertex(Point(cos(alpha) * cos(beta), sin(alpha), cos(alpha) * sin(beta)));
  231. m_sphere.normal(Vector(cos(alpha2) * cos(beta), sin(alpha2), cos(alpha2) * sin(beta)));
  232. m_sphere.vertex(Point(cos(alpha2) * cos(beta), sin(alpha2), cos(alpha2) * sin(beta)));
  233. }
  234. }
  235. }*/
  236.  
  237.  
  238. void Viewer::init_cylindre()
  239. {
  240. m_cylindre = Mesh(GL_TRIANGLE_STRIP);
  241. m_cylindre.color(1, 1, 1);
  242. const int div = 25;
  243. const float rota = M_PI * 2 / div;
  244. for (int i = 0; i <= div; i++)
  245. {
  246. const float alpha = rota * i;
  247. m_cylindre.normal(Vector(cos(alpha), 0, sin(alpha)));
  248. m_cylindre.vertex(Point(cos(alpha), -1, sin(alpha)));
  249. m_cylindre.normal(Vector(cos(alpha), 0, sin(alpha)));
  250. m_cylindre.vertex(Point(cos(alpha), 1, sin(alpha)));
  251. }
  252. }
  253.  
  254. void Viewer::init_couvercle()
  255. {
  256. m_couvercle = Mesh(GL_TRIANGLE_FAN);
  257. m_couvercle.color(1, 1, 1);
  258. const int div = 25;
  259. const float rota = M_PI * 2 / div;
  260. m_couvercle.vertex(Point(0, 0, 0));
  261. m_couvercle.normal(Vector(0, 1, 0));
  262. for (int i = 0; i <= div; i++)
  263. {
  264. const float alpha = rota * i;
  265. m_couvercle.vertex(Point(cos(alpha), 0, sin(alpha)));
  266. }
  267. }
  268.  
  269.  
  270. int Viewer::render()
  271. {
  272. // Efface l'ecran
  273. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  274.  
  275. // Deplace la camera, lumiere, etc.
  276. manageCameraLight();
  277.  
  278. // donne notre camera au shader
  279. gl.camera(m_camera);
  280.  
  281. /* gl.texture(m_quad_texture);
  282. gl.model( Tquad );
  283. gl.draw(m_quad);
  284.  
  285. gl.texture(m_cube_texture);
  286. gl.model(Translation( -3, 5, 0 ));
  287. gl.draw(m_cube); */
  288.  
  289. /*
  290. //CYLINDRE:
  291. gl.model(Translation(3, 0, 0));
  292. gl.draw(m_cylindre);
  293. gl.model(Translation(3, -2, 0));
  294. gl.draw(m_couvercle);
  295. gl.model(-1 * Translation(-3, 0, 0));
  296. gl.draw(m_couvercle);
  297.  
  298. //CONE;
  299. gl.model(Translation(0, -1, 0));
  300. gl.draw(m_couvercle);
  301. gl.model(Translation(0, 0, 0));
  302. gl.draw(m_cone);
  303.  
  304. //CUBE:
  305. Transform Tcube2 = Translation(-3, 0, 0) * Rotation(Vector(0, 0, 1), 0);
  306. gl.model(Tcube2);
  307. gl.draw(m_cube);
  308.  
  309. //SPHERE:
  310. gl.model(Translation(0, 2, 0));
  311. gl.draw(m_sphere);
  312. */
  313.  
  314. gl.model(Translation(3, 0, 0));
  315. gl.draw(m_cylindre);
  316.  
  317.  
  318. gl.model(-1 * Translation(0, 0, 2));
  319. gl.draw(m_couvercle);
  320.  
  321. return 1;
  322. }
  323.  
  324.  
  325. int Viewer::update(const float time, const float delta)
  326. {
  327. Tquad = Translation(3, 5, 0) * Rotation(Vector(0, 0, 1), 0.1f * time);
  328. return 1;
  329. }
  330.  
  331.  
  332. void Viewer::manageCameraLight()
  333. {
  334. // recupere les mouvements de la souris pour deplacer la camera, cf tutos/tuto6.cpp
  335. int mx, my;
  336. unsigned int mb = SDL_GetRelativeMouseState(&mx, &my);
  337. // deplace la camera
  338. if ((mb & SDL_BUTTON(1)) && (mb & SDL_BUTTON(3))) // le bouton du milieu est enfonce
  339. m_camera.translation((float)mx / (float)window_width(), (float)my / (float)window_height());
  340. // deplace le point de rotation
  341. else if (mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
  342. m_camera.rotation(mx, my); // tourne autour de l'objet
  343. else if (mb & SDL_BUTTON(3)) // le bouton droit est enfonce
  344. m_camera.move(my); // approche / eloigne l'objet
  345. if (key_state(SDLK_PAGEUP) && (!key_state(SDLK_LCTRL))) { m_camera.translation(0, 0.01); }
  346. if (key_state(SDLK_PAGEDOWN) && (!key_state(SDLK_LCTRL))) { m_camera.translation(0, -0.01); }
  347. if (key_state(SDLK_LEFT) && (!key_state(SDLK_LCTRL))) { m_camera.translation(0.01, 0); }
  348. if (key_state(SDLK_RIGHT) && (!key_state(SDLK_LCTRL))) { m_camera.translation(-0.01, 0); }
  349. if (key_state(SDLK_UP) && (!key_state(SDLK_LCTRL))) { m_camera.move(1); }
  350. if (key_state(SDLK_DOWN) && (!key_state(SDLK_LCTRL))) { m_camera.move(-1); }
  351.  
  352.  
  353. // Deplace la lumiere
  354. const float step = 0.1f;
  355. if (key_state(SDLK_RIGHT) && key_state(SDLK_LCTRL)) { gl.light(gl.light() + Vector(step, 0, 0)); }
  356. if (key_state(SDLK_LEFT) && key_state(SDLK_LCTRL)) { gl.light(gl.light() + Vector(-step, 0, 0)); }
  357. if (key_state(SDLK_UP) && key_state(SDLK_LCTRL)) { gl.light(gl.light() + Vector(0, 0, -step)); }
  358. if (key_state(SDLK_DOWN) && key_state(SDLK_LCTRL)) { gl.light(gl.light() + Vector(0, 0, step)); }
  359. if (key_state(SDLK_PAGEUP) && key_state(SDLK_LCTRL)) { gl.light(gl.light() + Vector(0, step, 0)); }
  360. if (key_state(SDLK_PAGEDOWN) && key_state(SDLK_LCTRL)) { gl.light(gl.light() + Vector(0, -step, 0)); }
  361.  
  362.  
  363. // (De)Active la grille / les axes
  364. if (key_state('h')) help();
  365. if (key_state('c'))
  366. {
  367. clear_key_state('c');
  368. mb_cullface = !mb_cullface;
  369. if (mb_cullface) glEnable(GL_CULL_FACE);
  370. else glDisable(GL_CULL_FACE);
  371. }
  372. if (key_state('w'))
  373. {
  374. clear_key_state('w');
  375. mb_wireframe = !mb_wireframe;
  376. if (mb_wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  377. else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  378. }
  379. if (key_state('g'))
  380. {
  381. b_draw_grid = !b_draw_grid;
  382. clear_key_state('g');
  383. }
  384. if (key_state('a'))
  385. {
  386. b_draw_axe = !b_draw_axe;
  387. clear_key_state('a');
  388. }
  389. if (key_state('z'))
  390. {
  391. b_draw_animation = !b_draw_animation;
  392. clear_key_state('z');
  393. }
  394.  
  395. gl.camera(m_camera);
  396. //draw(cube, Translation( Vector( gl.light()))*Scale(0.3, 0.3, 0.3), camera);
  397. //draw_param.texture(quad_texture).camera(camera).model(Translation( 3, 5, 0 )).draw(quad);
  398.  
  399. // AXE et GRILLE
  400. gl.model(Identity());
  401. if (b_draw_grid) gl.draw(m_grid);
  402. if (b_draw_axe) gl.draw(m_axe);
  403. if (b_draw_animation) m_anim.draw(m_camera);
  404.  
  405. // LIGHT
  406. gl.texture(0);
  407. gl.model(Translation(Vector(gl.light())) * Scale(0.3, 0.3, 0.3));
  408. gl.draw(m_cube);
  409. }
  410.  
  411. int Viewer::quit()
  412. {
  413. return 0;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement