Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.17 KB | None | 0 0
  1. #define GLEW_STATIC
  2. #include <GL/glew.h>
  3.  
  4. //#include "../common/Angel_util.h"
  5. #include "LetraT.h"
  6.  
  7.  
  8. LetraT::LetraT() { }
  9.  
  10. LetraT::~LetraT() { }
  11.  
  12. void LetraT::desenhar()
  13. {
  14. glBindVertexArray( vao );
  15. int inicioPrisma;
  16. glm::mat4 M, espelhoX;
  17. espelhoX = glm::scale(espelhoX, glm::vec3(-1.0, 1.0, 1.0));
  18. for (int i=0; i<nPrismas; i++) {
  19. M = mModel[i];
  20. inicioPrisma = i*24;
  21. glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
  22. glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
  23. M = espelhoX * M;
  24. glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
  25. glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
  26. }
  27. }
  28.  
  29. void LetraT::desenharCompleto()
  30. {
  31. glBindVertexArray( vao );
  32. int inicioPrisma;
  33. glm::mat4 M, espelho;
  34. espelho = glm::rotate(espelho,glm::radians(180.0f),glm::vec3(-1.0,0.0,0.0));
  35. espelho = glm::scale(espelho, glm::vec3(-1.0,1.0, 1.0));
  36. espelho = glm::scale(espelho, glm::vec3(1.0,1.0,-1.0));
  37.  
  38.  
  39.  
  40.  
  41. for (int i=0; i<nPrismas; i++) {
  42. M = mModel[i];
  43. inicioPrisma = i*24;
  44. glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
  45. glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
  46. M = espelho * M;
  47. M = glm::rotate(M,glm::radians(180.0f),glm::vec3(1.0,0.0,0.0));
  48. M = glm::scale(M, glm::vec3(1.0,1.0,-1.0));
  49. glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
  50. glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
  51. }
  52. }
  53.  
  54. void LetraT::setmModel_loc(GLuint Model)
  55. {
  56. mModel_loc = Model;
  57. }
  58.  
  59. void LetraT::criaLetraT(GLuint program, const GLchar *position,
  60. const GLchar *normal)
  61. {
  62. // Create and initialize a buffer object
  63. glGenVertexArrays( 1, &vao );
  64. glBindVertexArray( vao );
  65. GLuint vertexbuf;
  66. glGenBuffers( 1, &vertexbuf);
  67. glBindBuffer( GL_ARRAY_BUFFER, vertexbuf);
  68. criaPrismas();
  69. glBufferData( GL_ARRAY_BUFFER,
  70. sizeof(points) + sizeof(normals) + sizeof(tex_coord),
  71. NULL, GL_STATIC_DRAW );
  72. glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), &points );
  73. glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(normals), &normals );
  74. glBufferSubData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals),
  75. sizeof(tex_coord), &tex_coord );
  76.  
  77. // set up vertex arrays
  78. GLuint vPosition = glGetAttribLocation( program, position );
  79. glEnableVertexAttribArray( vPosition );
  80. glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
  81. BUFFER_OFFSET(0) );
  82.  
  83. GLuint vNormal = glGetAttribLocation( program, normal );
  84. glEnableVertexAttribArray( vNormal );
  85. glVertexAttribPointer( vNormal, 4, GL_FLOAT, GL_FALSE, 0,
  86. BUFFER_OFFSET(sizeof(points)) );
  87. }
  88.  
  89. void LetraT::criaLetraT(GLuint program, const GLchar *position,
  90. const GLchar *normal, const GLchar *tCoords)
  91. {
  92. criaLetraT(program, position, normal);
  93. GLuint vTCoords = glGetAttribLocation( program, tCoords );
  94. glEnableVertexAttribArray( vTCoords );
  95. glVertexAttribPointer( vTCoords, 2, GL_FLOAT, GL_FALSE, 0,
  96. BUFFER_OFFSET(sizeof(points)+sizeof(normals)) );
  97. }
  98.  
  99. glm::vec4 LetraT::normalDaFace(int a, int b, int c)
  100. {
  101. glm::vec3 u; // = vertices[b].xyz - vertices[a].xyz;
  102. u.x = vertices[b].x - vertices[a].x;
  103. u.y = vertices[b].y - vertices[a].y;
  104. u.z = vertices[b].z - vertices[a].z;
  105. glm::vec3 v; // = vertices[c].xyz - vertices[b].xyz;
  106. v.x = vertices[c].x - vertices[b].x;
  107. v.y = vertices[c].y - vertices[b].y;
  108. v.z = vertices[c].z - vertices[b].z;
  109.  
  110. glm::vec3 normal = glm::normalize( glm::cross(u, v) );
  111. glm::vec4 normal4 = glm::vec4(normal,0.0);
  112. return normal4;
  113. }
  114.  
  115. void LetraT::triang( int a, int b, int c)
  116. {
  117. glm::vec4 normal4 = normalDaFace(a,b,c);
  118. points[indexVert] = vertices[a];
  119. normals[indexVert] = normal4;
  120. tex_coord[indexVert] = glm::vec2((maxX+vertices[a].x)/maxX2, vertices[a].y/maxY);
  121. indexVert++;
  122. points[indexVert] = vertices[b];
  123. normals[indexVert] = normal4;
  124. tex_coord[indexVert] = glm::vec2((maxX+vertices[b].x)/maxX2, vertices[b].y/maxY);
  125. indexVert++;
  126. points[indexVert] = vertices[c];
  127. normals[indexVert] = normal4;
  128. tex_coord[indexVert] = glm::vec2((maxX+vertices[c].x)/maxX2, vertices[c].y/maxY);
  129. indexVert++;
  130. }
  131.  
  132. void LetraT::quad( int a, int b, int c, int d )
  133. {
  134. glm::vec4 normal4 = normalDaFace(a,b,c);
  135. // triangulo 1
  136. points[indexVert] = vertices[a];
  137. normals[indexVert] = normal4;
  138. tex_coord[indexVert] = glm::vec2(0.0f,0.0f);
  139. indexVert++;
  140. points[indexVert] = vertices[b];
  141. normals[indexVert] = normal4;
  142. tex_coord[indexVert] = glm::vec2(2.0f,0.0f);
  143. indexVert++;
  144. points[indexVert] = vertices[c];
  145. normals[indexVert] = normal4;
  146. tex_coord[indexVert] = glm::vec2(2.0f,1.0f);
  147. indexVert++;
  148. // triangulo 2
  149. points[indexVert] = vertices[a];
  150. normals[indexVert] = normal4;
  151. tex_coord[indexVert] = glm::vec2(0.0f,0.0f);
  152. indexVert++;
  153. points[indexVert] = vertices[c];
  154. normals[indexVert] = normal4;
  155. tex_coord[indexVert] = glm::vec2(2.0f,1.0f);
  156. indexVert++;
  157. points[indexVert] = vertices[d];
  158. normals[indexVert] = normal4;
  159. tex_coord[indexVert] = glm::vec2(0.0f,1.0f);
  160. indexVert++;
  161. }
  162.  
  163. // gerar 8 triângulos: 24 vertices
  164. void LetraT::prisma(int aF, int bF, int cF, int aT, int bT, int cT)
  165. {
  166. // prisma de bases triangulares e laterais retangulares
  167. triang(aF, bF, cF); // bases
  168. triang(aT, cT, bT);
  169. quad(aF,aT,bT,bF); // laterais
  170. quad(bF,bT,cT,cF);
  171. quad(cF,cT,aT,aF);
  172. }
  173.  
  174. void LetraT::criaPrismas()
  175. {
  176. int aF, bF, cF, aT, bT, cT, k;
  177. indexVert = 0;
  178. // centralizar modelo (letra T) na origem
  179. for (int i=0; i<nVertices; i++)
  180. vertices[i].y -= maxY/2.0;
  181. for (int i=0; i<nPrismas; i++) {
  182. k = i*6;
  183. aF = prismas[k]; bF = prismas[k+1]; cF = prismas[k+2];
  184. aT = prismas[k+3]; bT = prismas[k+4]; cT = prismas[k+5];
  185. prisma(aF, bF, cF, aT, bT, cT);
  186. centroPrisma[i].x = (vertices[aF].x + vertices[bF].x + vertices[cF].x +
  187. vertices[aF].x + vertices[bF].x + vertices[cF].x )/6.0;
  188. centroPrisma[i].y = (vertices[aF].y + vertices[bF].y + vertices[cF].y +
  189. vertices[aF].y + vertices[bF].y + vertices[cF].y )/6.0;
  190. centroPrisma[i].z = (vertices[aF].z + vertices[bF].z + vertices[cF].z +
  191. vertices[aF].z + vertices[bF].z + vertices[cF].z )/6.0;
  192. }
  193. }
  194.  
  195. //----------------------------------------------------------------------------
  196.  
  197. void LetraT::explosao()
  198. {
  199. glm::vec3 vetExpl[nPrismas], //vetor de deslocamento do prisma
  200. eixoRotExpl[nPrismas], //vetor direção do eixo de rotação do prisma
  201. direcao[nPrismas];
  202.  
  203. int k;
  204.  
  205. // definir dados da explosão para cada prisma
  206. // definir aleatoriamente vetor de translação para cada prisma
  207. for (int i=0; i<nPrismas; i++) {
  208. vetExpl[i].x = -rand(); // x sempre negativo, porque o meioT setá espelhado
  209. // para a porção positiva do eixo x
  210. k = rand();
  211. vetExpl[i].y = (k%2==0)? k : -k;
  212. k = rand();
  213. vetExpl[i].z = (k%2==0)? k : -k;
  214. // diminuir módulo do vetor
  215. vetExpl[i] *= 1.0e-8;
  216. }
  217.  
  218. // definir aleatoriamente um vetor de direção para o eixo
  219. // de rotação de cada prisma. Será considerado que esse eixo
  220. // passa pelo centro do prisma
  221. for (int i=0; i<nPrismas; i++) {
  222. k = rand();
  223. eixoRotExpl[i].x = (k%2==0)? k : -k;
  224. k = rand();
  225. eixoRotExpl[i].y = (k%2==0)? k : -k;
  226. k = rand();
  227. eixoRotExpl[i].z = (k%2==0)? k : -k;
  228. }
  229.  
  230. //gira em torno de si
  231. for (int j = 0; j < 120.0; ++j)
  232. {
  233. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  234. for (int i = 0; i < nPrismas; ++i)
  235. mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(-centroPrisma[i].x,-centroPrisma[i].y,-centroPrisma[i].z));
  236. // mModel[i] = glm::translate(mModel[i],glm::vec3(0.0f,0.0f,direcao[i].z));
  237.  
  238. desenhar();
  239. glutSwapBuffers();
  240. //espere um pouco
  241. for (int k=0; k<999990; k++);
  242. }
  243.  
  244. //translada ida
  245. for (int j = 0; j < 180.0; ++j)
  246. {
  247. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  248. for (int i = 0; i < nPrismas; ++i){
  249. mModel[i] = glm::translate(mModel[i],glm::vec3(vetExpl[i].x,vetExpl[i].y,vetExpl[i].z));
  250. mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(eixoRotExpl[i].x,eixoRotExpl[i].y,eixoRotExpl[i].z));
  251. }
  252.  
  253. desenhar();
  254. glutSwapBuffers();
  255.  
  256. }
  257. for (int j = 0; j < 180.0; ++j)
  258. {
  259. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  260. for (int i = 0; i < nPrismas; ++i)
  261. mModel[i] = glm::translate(mModel[i],glm::vec3(0.0025*centroPrisma[i].x,0.0025*centroPrisma[i].y,0.0025*centroPrisma[i].z));
  262.  
  263. desenhar();
  264. glutSwapBuffers();
  265. //espere um pouco
  266. for (int k=0; k<999991; k++);
  267. }
  268.  
  269.  
  270. //volta para a origem
  271. for (int j = 0; j < 180.0; ++j)
  272. {
  273. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  274. for (int i = 0; i < nPrismas; ++i)
  275. mModel[i] = glm::translate(mModel[i],glm::vec3(-0.0025*centroPrisma[i].x,-0.0025*centroPrisma[i].y,-0.0025*centroPrisma[i].z));
  276.  
  277. desenhar();
  278. glutSwapBuffers();
  279. //espere um pouco
  280. for (int k=0; k<999991; k++);
  281.  
  282. }
  283. for (int j = 0; j < 180.0; ++j)
  284. {
  285. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  286. for (int i = 0; i < nPrismas; ++i){
  287. mModel[i] = glm::translate(mModel[i],glm::vec3(-vetExpl[i].x,-vetExpl[i].y,-vetExpl[i].z));
  288. mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(-eixoRotExpl[i].x,-eixoRotExpl[i].y,-eixoRotExpl[i].z));
  289. }
  290.  
  291. desenhar();
  292. glutSwapBuffers();
  293.  
  294. }
  295. //rotaciona de volta
  296. for (int j = 0; j < 120.0; ++j)
  297. {
  298. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  299. for (int i = 0; i < nPrismas; ++i)
  300. mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(centroPrisma[i].x,centroPrisma[i].y,centroPrisma[i].z));
  301.  
  302. desenhar();
  303. glutSwapBuffers();
  304. //espere um pouco
  305. for (int k=0; k<99999; k++);
  306. }
  307. }
  308.  
  309. void LetraT::explosaoInvertida(){
  310. glm::vec3 vetExpl[nPrismas], //vetor de deslocamento do prisma
  311. eixoRotExpl[nPrismas], //vetor direção do eixo de rotação do prisma
  312. direcao[nPrismas];
  313.  
  314. int k;
  315.  
  316. // definir dados da explosão para cada prisma
  317. // definir aleatoriamente vetor de translação para cada prisma
  318. for (int i=0; i<nPrismas; i++) {
  319. vetExpl[i].x = -rand(); // x sempre negativo, porque o meioT setá espelhado
  320. // para a porção positiva do eixo x
  321. k = rand();
  322. vetExpl[i].y = (k%2==0)? k : -k;
  323. k = rand();
  324. vetExpl[i].z = (k%2==0)? k : -k;
  325. // diminuir módulo do vetor
  326. vetExpl[i] *= 1.0e-8;
  327. }
  328.  
  329. // definir aleatoriamente um vetor de direção para o eixo
  330. // de rotação de cada prisma. Será considerado que esse eixo
  331. // passa pelo centro do prisma
  332. for (int i=0; i<nPrismas; i++) {
  333. k = rand();
  334. eixoRotExpl[i].x = (k%2==0)? k : -k;
  335. k = rand();
  336. eixoRotExpl[i].y = (k%2==0)? k : -k;
  337. k = rand();
  338. eixoRotExpl[i].z = (k%2==0)? k : -k;
  339. }
  340.  
  341. //gira em torno de si
  342. for (int j = 0; j < 120.0; ++j)
  343. {
  344. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  345. for (int i = 0; i < nPrismas; ++i)
  346. mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(-centroPrisma[i].x,-centroPrisma[i].y,-centroPrisma[i].z));
  347. // mModel[i] = glm::translate(mModel[i],glm::vec3(0.0f,0.0f,direcao[i].z));
  348.  
  349. desenhar();
  350. glutSwapBuffers();
  351. //espere um pouco
  352. for (int k=0; k<999990; k++);
  353. }
  354.  
  355. //translada ida
  356. for (int j = 0; j < 180.0; ++j)
  357. {
  358. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  359. for (int i = 0; i < nPrismas; ++i){
  360. mModel[i] = glm::translate(mModel[i],glm::vec3(vetExpl[i].x,vetExpl[i].y,vetExpl[i].z));
  361. mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(eixoRotExpl[i].x,eixoRotExpl[i].y,eixoRotExpl[i].z));
  362. }
  363.  
  364. desenhar();
  365. glutSwapBuffers();
  366.  
  367. }
  368. for (int j = 0; j < 180.0; ++j)
  369. {
  370. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  371. for (int i = 0; i < nPrismas; ++i)
  372. mModel[i] = glm::translate(mModel[i],glm::vec3(0.0025*centroPrisma[i].x,0.0025*centroPrisma[i].y,0.0025*centroPrisma[i].z));
  373.  
  374. desenhar();
  375. glutSwapBuffers();
  376. //espere um pouco
  377. for (int k=0; k<999991; k++);
  378. }
  379.  
  380. glm::mat4 M;
  381. M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
  382. for (int i=0; i<180.0; i++) {
  383. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  384. for (int j=0; j<nPrismas; j++)
  385. mModel[j] = M*mModel[j];
  386. desenhar();
  387. glutSwapBuffers();
  388. //espere um pouco
  389. for (int k=0; k<999999; k++);
  390. }
  391.  
  392. //volta para a origem
  393. for (int j = 0; j < 180.0; ++j)
  394. {
  395. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  396. for (int i = 0; i < nPrismas; ++i)
  397. mModel[i] = glm::translate(mModel[i],glm::vec3(-0.0025*centroPrisma[i].x,-0.0025*centroPrisma[i].y,-0.0025*centroPrisma[i].z));
  398.  
  399. desenhar();
  400. glutSwapBuffers();
  401. //espere um pouco
  402. for (int k=0; k<999991; k++);
  403.  
  404. }
  405. for (int j = 0; j < 180.0; ++j)
  406. {
  407. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  408. for (int i = 0; i < nPrismas; ++i){
  409. mModel[i] = glm::translate(mModel[i],glm::vec3(-vetExpl[i].x,-vetExpl[i].y,-vetExpl[i].z));
  410. mModel[i] = glm::rotate(mModel[i], glm::radians(1.0f),glm::vec3(-eixoRotExpl[i].x,-eixoRotExpl[i].y,-eixoRotExpl[i].z));
  411. }
  412.  
  413. desenhar();
  414. glutSwapBuffers();
  415.  
  416. }
  417. //rotaciona de volta
  418. for (int j = 0; j < 120.0; ++j)
  419. {
  420. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  421. for (int i = 0; i < nPrismas; ++i)
  422. mModel[i] = glm::rotate(mModel[i],glm::radians(1.0f),glm::vec3(centroPrisma[i].x,centroPrisma[i].y,centroPrisma[i].z));
  423.  
  424. desenhar();
  425. glutSwapBuffers();
  426. //espere um pouco
  427. for (int k=0; k<99999; k++);
  428. }
  429. }
  430.  
  431. //----------------------------------------------------------------------------
  432.  
  433. void LetraT::girar()
  434. {
  435. glm::mat4 M;
  436. M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
  437. for (int i=0; i<180.0; i++) {
  438. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  439. for (int j=0; j<nPrismas; j++)
  440. mModel[j] = M*mModel[j];
  441. desenhar();
  442. glutSwapBuffers();
  443. //espere um pouco
  444. for (int k=0; k<999999; k++);
  445. }
  446. }
  447.  
  448. void LetraT::girarCompleto(){
  449.  
  450. glm::mat4 M;
  451. M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
  452. for (int i=0; i<180.0; i++) {
  453. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  454. for (int j=0; j<nPrismas; j++)
  455. mModel[j] = M*mModel[j];
  456. desenharCompleto();
  457. glutSwapBuffers();
  458. //espere um pouco
  459. for (int k=0; k<999999; k++);
  460. }
  461. }
  462.  
  463. void LetraT::curva(){
  464.  
  465. glm::mat4 M;
  466. M = glm::rotate(M, glm::radians(1.0f), glm::vec3(0.0,0.0,1.0));
  467. //M = glm::translate(M, glm::vec3(0.01,0.0,0.0));
  468. for (int i=0; i<180.0; i++) {
  469. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  470. for (int j=0; j<nPrismas; j++)
  471. mModel[j] = M*mModel[j];
  472. curvaParametrica();
  473. glutSwapBuffers();
  474. //espere um pouco
  475. for (int k=0; k<999999; k++);
  476. }
  477. }
  478.  
  479. void LetraT::curvaParametrica(){
  480. glBindVertexArray( vao );
  481. int inicioPrisma;
  482. static float x = 0.0;
  483. glm::mat4 M, espelho;
  484. espelho = glm::rotate(espelho,glm::radians(180.0f),glm::vec3(-1.0,0.0,0.0));
  485. espelho = glm::scale(espelho, glm::vec3(-1.0,1.0, 1.0));
  486. espelho = glm::scale(espelho, glm::vec3(1.0,1.0,-1.0));
  487. //espelho = glm::translate(M, glm::vec3(-0.0001,0.0,0.0));
  488.  
  489. for (int i=0; i<nPrismas; i++) {
  490. x += sqrt(0.01);
  491. M = mModel[i];
  492. M = glm::translate(M, glm::vec3(-1.0,0.0,0.0));
  493. inicioPrisma = i*24;
  494. glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
  495. glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
  496. M = espelho * M;
  497. M = glm::translate(M, glm::vec3(-2.0,0.0,0.0));
  498. M = glm::rotate(M,glm::radians(180.0f),glm::vec3(1.0,0.0,0.0));
  499. M = glm::scale(M, glm::vec3(1.0,1.0,-1.0));
  500. //M = glm::translate(M, glm::vec3(0.0001,0.0,0.0));
  501. glUniformMatrix4fv(mModel_loc,1,GL_FALSE, glm::value_ptr(M));
  502. glDrawArrays( GL_TRIANGLES, inicioPrisma, 24 );
  503. }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement