Advertisement
Lapprand

Untitled

Nov 27th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.77 KB | None | 0 0
  1. /****************************************************
  2.  
  3. ########## Project 1 Basis ##########
  4.  
  5. Graphics Processing - PG (if680)
  6. Center of Informatics - CIn
  7. Federal University of Pernambuco - UFPE
  8.  
  9. @authors
  10. {
  11. Caio Lins (csnrl at cin.ufpe.br),
  12. Geovane Pereira (geeosp at cin.ufpe.br),
  13. Vinicius Emanuel (vems at cin.ufpe.br)
  14. }
  15.  
  16. Reference for OpenGL commands: https://www.opengl.org/sdk/docs/man2/xhtml/
  17.  
  18. *****************************************************/
  19.  
  20. #include "Main.h"
  21.  
  22. float r = 3;
  23.  
  24. // ##### Params START #####
  25.  
  26. int old_x = 0;
  27. int old_y = 0;
  28. int valid = 0;
  29.  
  30. float tx = 0, ty = -0.5, tz = -6; // valores da matriz Rt
  31. float anguloRotacaoX = 0;
  32. float anguloRotacaoY = 0;
  33. float anguloRotacaoZ = 0;
  34. float fatorTranslacao = 0.1;
  35. float fatorRotacao = 1.0;
  36.  
  37. double znear = 0.1;
  38.  
  39. typedef float matriz4x4[16];
  40. typedef float vetor3[3];
  41.  
  42. matriz4x4 translacao = { 1, 0, 0, 0,
  43. 0, 1, 0, 0,
  44. 0, 0, 1, 0,
  45. tx, ty, tz, 1 };
  46.  
  47. matriz4x4 rotacao = { 1, 0, 0, 0,
  48. 0, 1, 0, 0,
  49. 0, 0, 1, 0,
  50. 0, 0, 0, 1 };
  51.  
  52. matriz4x4 ID = { 1, 0, 0, 0,
  53. 0, 1, 0, 0,
  54. 0, 0, 1, 0,
  55. 0, 0, 0, 1 };
  56.  
  57. matriz4x4 rotacaoX = { 1, 0, 0, 0,
  58. 0, 1, 0, 0,
  59. 0, 0, 1, 0,
  60. 0, 0, 0, 1 };
  61.  
  62. matriz4x4 rotacaoY = { 1, 0, 0, 0,
  63. 0, 1, 0, 0,
  64. 0, 0, 1, 0,
  65. 0, 0, 0, 1 };
  66.  
  67. GLfloat extrinsic[16] =
  68. {
  69. 1, 0, 0, 0,
  70. 0, 1, 0, 0,
  71. 0, 0, 1, 0,
  72. tx, ty, tz, 1
  73. };
  74.  
  75. vetor3 CameraPos = { tx, ty, tz };
  76. vetor3 CameraLook = { 0, 0, 1 };
  77.  
  78.  
  79. void CameraTranslateX(float t){
  80.  
  81. extrinsic[12] += t;
  82. translacao[12] += t;
  83.  
  84. }
  85.  
  86. void CameraTranslateZ(float t){
  87.  
  88. extrinsic[14] += t;
  89. translacao[14] += t;
  90.  
  91. }
  92.  
  93. void MultMat(matriz4x4 m1, matriz4x4 m2, matriz4x4 resultado){
  94. // Fisrt Column
  95. resultado[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
  96. resultado[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
  97. resultado[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
  98. resultado[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
  99.  
  100. // Second Column
  101. resultado[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
  102. resultado[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
  103. resultado[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
  104. resultado[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
  105.  
  106. // Third Column
  107. resultado[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
  108. resultado[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
  109. resultado[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
  110. resultado[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
  111.  
  112. // Fourth Column
  113. resultado[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
  114. resultado[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
  115. resultado[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
  116. resultado[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
  117. }
  118.  
  119. void tornarIdentidade(matriz4x4 mi){
  120. mi[0] = mi[5] = mi[10] = mi[15] = 1.0;
  121. mi[1] = mi[2] = mi[3] = mi[4] = 0.0;
  122. mi[6] = mi[7] = mi[8] = mi[9] = 0.0;
  123. mi[11] = mi[12] = mi[13] = mi[14] = 0.0;
  124. }
  125.  
  126. void CameraRotateX(matriz4x4 matriz, float ang){
  127.  
  128. float rad = ang * cento80PI;
  129.  
  130. tornarIdentidade(matriz);
  131.  
  132. matriz[5] = cosf(rad);
  133. matriz[6] = -sinf(rad);
  134. matriz[9] = -matriz[6];
  135. matriz[10] = matriz[5];
  136. }
  137.  
  138. void CameraRotateY(matriz4x4 matriz, float ang){
  139.  
  140. float rad = ang * cento80PI;
  141.  
  142. tornarIdentidade(matriz);
  143.  
  144. matriz[0] = cosf(rad);
  145. matriz[2] = sinf(rad);
  146. matriz[8] = -matriz[2];
  147. matriz[10] = matriz[0];
  148.  
  149. }
  150.  
  151. bool esconde = false;
  152. bool dolly = false;
  153.  
  154. // ##### Params END #####
  155.  
  156. // Screen params
  157. GLfloat wWidth = 1366.0;
  158. GLfloat wHeight = 768.0;
  159.  
  160. // Constants for object translation and rotation and camera translation
  161. const double translateConstant = 0.01;
  162. const double rotateConst = 1.5;
  163. const double translateCameraConst = 0.005;
  164.  
  165. bool buffer[250];
  166.  
  167. ///////////////////////////////////////////
  168. // PARTE DE CARREGAR OS OBJETOS
  169. ///////////////////////////////////////////
  170. typedef struct coord {
  171. float x, y, z;
  172. coord(float a, float b, float c) : x(a), y(b), z(c) {};
  173. };
  174.  
  175. typedef struct face {
  176. int num;
  177. bool quad = false;
  178. bool penta = false;
  179. bool hexa = false;
  180. int faces[6];
  181.  
  182. face(int n, int f1, int f2, int f3) : num(n) {
  183. faces[0] = f1;
  184. faces[1] = f2;
  185. faces[2] = f3;
  186. quad = false;
  187. }
  188.  
  189. face(int n, int f1, int f2, int f3, int f4) : num(n) {
  190. faces[0] = f1;
  191. faces[1] = f2;
  192. faces[2] = f3;
  193. faces[3] = f4;
  194. quad = true;
  195. penta = false;
  196. hexa = false;
  197. }
  198. face(int n, int f1, int f2, int f3, int f4, int f5) : num(n) {
  199. faces[0] = f1;
  200. faces[1] = f2;
  201. faces[2] = f3;
  202. faces[3] = f4;
  203. faces[4] = f5;
  204. quad = false;
  205. penta = true;
  206. hexa = false;
  207. }
  208. face(int n, int f1, int f2, int f3, int f4, int f5, int f6) : num(n) {
  209. faces[0] = f1;
  210. faces[1] = f2;
  211. faces[2] = f3;
  212. faces[3] = f4;
  213. faces[4] = f5;
  214. faces[5] = f6;
  215. quad = false;
  216. penta = false;
  217. hexa = true;
  218. }
  219. };
  220.  
  221. typedef struct objPos {
  222. float x, y, z;
  223. float scale = 1;
  224. float rotationX = 0, rotationY = 0, rotationZ = 0;
  225. objPos(float a, float b, float c) : x(a), y(b), z(c) {};
  226. };
  227.  
  228. int num, select = 0;
  229. float lx = 0, ly = 0, lz = 0;
  230. float lx1 = 0, ly1 = 0, lz1 = 0;
  231. float d = 0.1;
  232. bool temNormal, ehSimples;
  233. //float rotationX, rotationY, rotationZ;
  234. vector<int>listaObj;
  235. vector<objPos*>listaPosObj;
  236. vector<string*>listaArquivos;
  237. // vetor das normais
  238. vector<coord*> normais;
  239.  
  240. void listar(){
  241. listaArquivos.push_back(new string("./Objetos/chimp.obj"));
  242. //listaArquivos.push_back(new string("./Objetos/yoda.obj"));
  243. listaArquivos.push_back(new string("./Objetos/whale.obj"));
  244. listaArquivos.push_back(new string("./Objetos/chimp.obj"));
  245. listaArquivos.push_back(new string("./Objetos/spheretri.obj"));
  246. /*listaArquivos.push_back(new string("./Objetos/venus.obj"));
  247. listaArquivos.push_back(new string("./Objetos/lion.obj"));
  248. listaArquivos.push_back(new string("./Objetos/eagle.obj"));
  249. listaArquivos.push_back(new string("./Objetos/Dog.obj"));
  250. listaArquivos.push_back(new string("./Objetos/cubo1.obj"));
  251. listaArquivos.push_back(new string("./Objetos/cube.obj"));
  252. listaArquivos.push_back(new string("./Objetos/Cow.obj"));
  253. listaArquivos.push_back(new string("./Objetos/camel.obj"));
  254. listaArquivos.push_back(new string("./Objetos/apple.obj"));*/
  255. for (int i = 0; i < listaArquivos.size(); i++) {
  256. listaPosObj.push_back(new objPos(0, 0, 0));
  257. }
  258. }
  259.  
  260. void compVetorNormalTriangulo(coord* p1, coord* p2, coord* p3)
  261.  
  262. {
  263.  
  264. coord* v1 = new coord(0, 0, 0);
  265. coord* v2 = new coord(0, 0, 0);
  266.  
  267. double x, y, z;
  268. double len;
  269.  
  270.  
  271. /* Encontra vetor v1 */
  272.  
  273. v1->x = p2->x - p1->x;
  274.  
  275. v1->y = p2->y - p1->y;
  276.  
  277. v1->z = p2->z - p1->z;
  278.  
  279.  
  280. /* Encontra vetor v2 */
  281.  
  282. v2->x = p3->x - p1->x;
  283.  
  284. v2->y = p3->y - p1->y;
  285.  
  286. v2->z = p3->z - p1->z;
  287.  
  288.  
  289. /* Calculo do produto vetorial de v1 e v2 */
  290.  
  291. x = (v1->y * v2->z) - (v1->z * v2->y);
  292.  
  293. y = (v1->z * v2->x) - (v1->x * v2->z);
  294.  
  295. z = (v1->x * v2->y) - (v1->y * v2->x);
  296.  
  297.  
  298. /* normalizacao de n */
  299.  
  300. len = sqrt(x*x + y*y + z*z);
  301.  
  302. x /= len;
  303.  
  304. y /= len;
  305.  
  306. z /= len;
  307. //glNormal3f(x, y, z);
  308. normais.push_back(new coord(x, y, z));
  309. }
  310.  
  311. int loadObj(const char* arquivo) {
  312.  
  313. // vetor que armazena cada linha de coordenadas como uma string
  314. vector<string*> vcoord;
  315.  
  316. // vetor que armazena as coordenadas (struct coord)
  317. vector<coord*> vertices;
  318.  
  319. // vetor das faces
  320. vector<face*> faces;
  321. int n = 1;
  322.  
  323. vector<coord*> novo;
  324. normais = novo;
  325. temNormal = false;
  326.  
  327. // carrega o arquivo
  328. ifstream in(arquivo);
  329. if (!in.is_open()) {
  330. cout << "Erro, arquivo nao abriu" << endl;
  331. exit(1);
  332. }
  333.  
  334. char buffer[256];
  335.  
  336. // coloca cada linha de coordenadas no vetor vcoord
  337. while (!in.eof()) {
  338. in.getline(buffer, 256);
  339. vcoord.push_back(new string(buffer));
  340. }
  341.  
  342. for (int i = 0; i < vcoord.size(); i++) {
  343. if (vcoord[i]->c_str()[0] == '#') {
  344. continue;
  345. }
  346. else if (vcoord[i]->c_str()[0] == 'v' && vcoord[i]->c_str()[1] == ' ') {
  347. float tempx, tempy, tempz;
  348.  
  349. sscanf(vcoord[i]->c_str(), "v %f %f %f", &tempx, &tempy, &tempz);
  350. vertices.push_back(new coord(tempx, tempy, tempz));
  351. }
  352. else if (vcoord[i]->c_str()[0] == 'v' && vcoord[i]->c_str()[1] == 'n') {
  353. float tempx, tempy, tempz;
  354. sscanf(vcoord[i]->c_str(), "vn %f %f %f", &tempx, &tempy, &tempz);
  355. normais.push_back(new coord(tempx, tempy, tempz));
  356. temNormal = true;
  357. }
  358. else if (vcoord[i]->c_str()[0] == 'f') {
  359. int nv1 = 0, nv2 = 0, nv3 = 0, nv4 = 0, nv5 = 0, nv6 = 0, fn = 0, tx1 = 0, tx2 = 0, tx3 = 0, tx4 = 0, tx5 = 0, tx6 = 0;
  360.  
  361. /// retira os ultimos characteres caso eles sejam " "
  362. char lastc = *vcoord[i]->rbegin();
  363. while (lastc == ' ') {
  364. *vcoord[i] = vcoord[i]->substr(0, vcoord[i]->size() - 1);
  365. lastc = *vcoord[i]->rbegin();
  366. }
  367. ////////
  368.  
  369. if (count(vcoord[i]->begin(), vcoord[i]->end(), ' ') == 6) //hexa
  370. {
  371. if (vcoord[i]->find("//") != string::npos)
  372. {
  373. sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn, &nv4, &fn, &nv5, &fn, &nv6, &fn);
  374. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5), abs(nv6)));
  375. faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5, nv6));
  376. }
  377. else if (vcoord[i]->find("/") != string::npos)
  378. {
  379. sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn, &nv4, &tx4, &fn, &nv5, &tx5, &fn, &nv6, &tx6, &fn);
  380. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5), abs(nv6)));
  381. faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5, nv6));
  382. }
  383. else{
  384. ehSimples = true;
  385. sscanf(vcoord[i]->c_str(), "f %d %d %d %d %d %d", &nv1, &nv2, &nv3, &nv4, &nv5, &nv6);
  386. faces.push_back(new face(-1, nv1, nv2, nv3, nv4, nv5, nv6));
  387. }
  388. }
  389. else if (count(vcoord[i]->begin(), vcoord[i]->end(), ' ') == 5) //penta
  390. {
  391. if (vcoord[i]->find("//") != string::npos)
  392. {
  393. sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn, &nv4, &fn, &nv5, &fn);
  394. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5)));
  395. faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5));
  396. }
  397. else if (vcoord[i]->find("/") != string::npos)
  398. {
  399. sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn, &nv4, &tx4, &fn, &nv5, &tx5, &fn);
  400. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4), abs(nv5)));
  401. faces.push_back(new face(fn, nv1, nv2, nv3, nv4, nv5));
  402. }
  403. else{
  404. ehSimples = true;
  405. sscanf(vcoord[i]->c_str(), "f %d %d %d %d %d", &nv1, &nv2, &nv3, &nv4, &nv5);
  406. faces.push_back(new face(-1, nv1, nv2, nv3, nv4, nv5));
  407. }
  408. }
  409. else if (count(vcoord[i]->begin(), vcoord[i]->end(), ' ') == 4) //quad
  410. {
  411. if (vcoord[i]->find("//") != string::npos)
  412. {
  413. sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn, &nv4, &fn);
  414. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4)));
  415. faces.push_back(new face(fn, nv1, nv2, nv3, nv4));
  416. }
  417. else if (vcoord[i]->find("/") != string::npos)
  418. {
  419. sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn, &nv4, &tx4, &fn);
  420. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3), abs(nv4)));
  421. faces.push_back(new face(fn, nv1, nv2, nv3, nv4));
  422. }
  423. else{
  424. ehSimples = true;
  425. sscanf(vcoord[i]->c_str(), "f %d %d %d %d", &nv1, &nv2, &nv3, &nv4);
  426. faces.push_back(new face(-1, nv1, nv2, nv3, nv4));
  427. }
  428. }
  429. else { //tri
  430. if (vcoord[i]->find("//") != string::npos)
  431. {
  432. sscanf(vcoord[i]->c_str(), "f %d//%d %d//%d %d//%d", &nv1, &fn, &nv2, &fn, &nv3, &fn);
  433. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3)));
  434. faces.push_back(new face(fn, nv1, nv2, nv3));
  435. }
  436. else if (vcoord[i]->find("/") != string::npos)
  437. {
  438. sscanf(vcoord[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &nv1, &tx1, &fn, &nv2, &tx2, &fn, &nv3, &tx3, &fn);
  439. //faces.push_back(new face(abs(fn), abs(nv1), abs(nv2), abs(nv3)));
  440. faces.push_back(new face(fn, nv1, nv2, nv3));
  441. }
  442. else{
  443. ehSimples = true;
  444. sscanf(vcoord[i]->c_str(), "f %d %d %d", &nv1, &nv2, &nv3);
  445. faces.push_back(new face(n, nv1, nv2, nv3));
  446. n++;
  447. }
  448. }
  449. }
  450. }
  451.  
  452. if (!temNormal) {
  453. for (int i = 0; i < faces.size(); i++) {
  454. if (!faces[i]->quad && !(faces[i]->penta) && !(faces[i]->hexa)) {
  455. compVetorNormalTriangulo(vertices[faces[i]->faces[0] - 1], vertices[faces[i]->faces[1] - 1], vertices[faces[i]->faces[2] - 1]);
  456. }
  457. }
  458. }
  459.  
  460. // desenha
  461. num = glGenLists(1);
  462. glNewList(num, GL_COMPILE);
  463. {
  464. glPointSize(2.0);
  465. glPushMatrix();
  466. printf("Faces %d, Normais %d \n", faces.size(), normais.size());
  467. }
  468.  
  469. for (int i = 0; i < faces.size(); i++) {
  470. if (faces[i]->hexa) {
  471. if (temNormal) {
  472. glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
  473. }
  474. glBegin(GL_POLYGON);
  475. glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
  476. glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
  477. glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
  478. glVertex3f(vertices[faces[i]->faces[3] - 1]->x, vertices[faces[i]->faces[3] - 1]->y, vertices[faces[i]->faces[3] - 1]->z);
  479. glVertex3f(vertices[faces[i]->faces[4] - 1]->x, vertices[faces[i]->faces[4] - 1]->y, vertices[faces[i]->faces[4] - 1]->z);
  480. glVertex3f(vertices[faces[i]->faces[5] - 1]->x, vertices[faces[i]->faces[5] - 1]->y, vertices[faces[i]->faces[5] - 1]->z);
  481. glEnd();
  482.  
  483.  
  484. }
  485. else if (faces[i]->penta) {
  486. if (temNormal) {
  487. glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
  488. }
  489. glBegin(GL_POLYGON);
  490. glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
  491. glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
  492. glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
  493. glVertex3f(vertices[faces[i]->faces[3] - 1]->x, vertices[faces[i]->faces[3] - 1]->y, vertices[faces[i]->faces[3] - 1]->z);
  494. glVertex3f(vertices[faces[i]->faces[4] - 1]->x, vertices[faces[i]->faces[4] - 1]->y, vertices[faces[i]->faces[4] - 1]->z);
  495. glEnd();
  496.  
  497.  
  498. }
  499. else if (faces[i]->quad) {
  500. if (temNormal) {
  501. glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
  502. }
  503. glBegin(GL_QUADS);
  504. glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
  505. glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
  506. glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
  507. glVertex3f(vertices[faces[i]->faces[3] - 1]->x, vertices[faces[i]->faces[3] - 1]->y, vertices[faces[i]->faces[3] - 1]->z);
  508. glEnd();
  509. }
  510. else if (!(faces[i]->quad) && !(faces[i]->penta) && !(faces[i]->hexa)) {
  511. if (!temNormal) {
  512. //compVetorNormalTriangulo(vertices[faces[i]->faces[0] - 1], vertices[faces[i]->faces[1] - 1], vertices[faces[i]->faces[2] - 1]);
  513. glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
  514. }
  515. else if (temNormal && !ehSimples) {
  516. glNormal3f(normais[faces[i]->num - 1]->x, normais[faces[i]->num - 1]->y, normais[faces[i]->num - 1]->z);
  517. }
  518.  
  519. if (temNormal && ehSimples) {
  520. glBegin(GL_TRIANGLES);
  521. glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
  522. glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
  523. glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
  524. glEnd();
  525. }
  526. else {
  527. glBegin(GL_TRIANGLES);
  528. glVertex3f(vertices[faces[i]->faces[0] - 1]->x, vertices[faces[i]->faces[0] - 1]->y, vertices[faces[i]->faces[0] - 1]->z);
  529. glVertex3f(vertices[faces[i]->faces[1] - 1]->x, vertices[faces[i]->faces[1] - 1]->y, vertices[faces[i]->faces[1] - 1]->z);
  530. glVertex3f(vertices[faces[i]->faces[2] - 1]->x, vertices[faces[i]->faces[2] - 1]->y, vertices[faces[i]->faces[2] - 1]->z);
  531. glEnd();
  532. }
  533. }
  534. }
  535.  
  536.  
  537. glPopMatrix();
  538. glEndList();
  539. listaObj.push_back(num);
  540.  
  541. // deleta o que foi alocado para evitar memory leak
  542. for (int i = 0; i < vcoord.size(); i++) {
  543. delete vcoord[i];
  544. }
  545. for (int i = 0; i < faces.size(); i++) {
  546. delete faces[i];
  547. }
  548. for (int i = 0; i < vertices.size(); i++) {
  549. delete vertices[i];
  550. }
  551. for (int i = 0; i < normais.size(); i++) {
  552. delete normais[i];
  553. }
  554. }
  555.  
  556.  
  557.  
  558. void initialize() // Initialize opengl params
  559. {
  560. glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  561. glClearDepth(1.0f);
  562. glEnable(GL_DEPTH_TEST);
  563. glDepthFunc(GL_LEQUAL);
  564. glShadeModel(GL_SMOOTH);
  565. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  566. }
  567.  
  568. void initLight(void) {
  569. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  570. GLfloat mat_shininess[] = { 50.0 };
  571. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  572. glClearColor(0.0, 0.0, 0.0, 0.0);
  573. glShadeModel(GL_SMOOTH);
  574.  
  575. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  576. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  577. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  578.  
  579. GLfloat light1_ambient[] = { 0.0, 1.0, 0.0, 1.0 };
  580. GLfloat light1_diffuse[] = { 0.0, 1.0, 0.0, 1.0 };
  581. GLfloat light1_specular[] = { 0.0, 1.0, 0.0, 1.0 };
  582. GLfloat light1_position[] = { -2.0, 0.0, 0.0, 0.0 };
  583. GLfloat spot_direction[] = { 0.0, 0.0, 0.0 };
  584.  
  585. glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
  586. glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
  587. glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
  588. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  589.  
  590. glEnable(GL_LIGHTING);
  591. glEnable(GL_LIGHT0);
  592. glEnable(GL_LIGHT1);
  593. glEnable(GL_DEPTH_TEST);
  594. }
  595.  
  596. void myreshape(GLsizei w, GLsizei h) // Called at startup and when you move the window
  597. {
  598. float aspect = (float)wWidth / (float)wHeight;
  599. float top = tan(45 * 0.5) * znear;
  600. float bottom = -top;
  601. float left = aspect * bottom;
  602. float right = aspect * top;
  603. glViewport(0, 0, w, h);
  604. glMatrixMode(GL_PROJECTION);
  605. glLoadIdentity();
  606. wWidth = (GLfloat)w;
  607. wHeight = (GLfloat)h;
  608. if (!dolly){
  609. gluPerspective(45, wWidth / wHeight, 0.1f, 1000.0f);
  610. }
  611. else {
  612. glFrustum(-1.6, 0.9, -1.0, 1.0, znear, 1000.0f);
  613. //gluPerspective(45, wWidth / wHeight, znear, 3000.0f);
  614. }
  615. }
  616.  
  617. void draw()
  618. {
  619. glLoadMatrixf(extrinsic);
  620. double x = 0;
  621. double y = 0;
  622. double size = 0.3;
  623.  
  624. // carrega grid
  625. if (!esconde){ //desenhando o grid
  626. int HALF_GRID_SIZE = 20;
  627.  
  628. glBegin(GL_LINES);
  629. for (int i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i++)
  630. {
  631. glColor3f(0.0f, 1.0f, 0.0f);
  632. glVertex3f((float)i, 0, (float)-HALF_GRID_SIZE);
  633. glVertex3f((float)i, 0, (float)HALF_GRID_SIZE);
  634.  
  635.  
  636. glColor3f(0.0f, 0.1f, 0.0f);
  637. glVertex3f((float)-HALF_GRID_SIZE, 0, (float)i);
  638. glVertex3f((float)HALF_GRID_SIZE, 0, (float)i);
  639. }
  640. for (int i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i++)
  641. {
  642. glColor3f(0.0f, 1.0f, 0.0f);
  643. glVertex3f((float)i, (float)-HALF_GRID_SIZE, 0);
  644. glVertex3f((float)i, (float)HALF_GRID_SIZE, 0);
  645.  
  646.  
  647. glColor3f(0.0f, 0.1f, 0.0f);
  648. glVertex3f((float)-HALF_GRID_SIZE, (float)i, 0);
  649. glVertex3f((float)HALF_GRID_SIZE, (float)i, 0);
  650. }
  651. glEnd();
  652. }
  653.  
  654. // carrega objetos
  655. for (int i = 0; i < listaObj.size(); i++) {
  656. if (i != (select)) {
  657. glPushMatrix();
  658. glScalef(listaPosObj[i]->scale, listaPosObj[i]->scale, listaPosObj[i]->scale);
  659. glTranslatef(listaPosObj[i]->x, listaPosObj[i]->y, listaPosObj[i]->z);
  660. glRotatef(listaPosObj[i]->rotationX, 1, 0, 0);
  661. glRotatef(listaPosObj[i]->rotationY, 0, 1, 0);
  662. glRotatef(listaPosObj[i]->rotationZ, 0, 0, 1);
  663. glCallList(listaObj.at(i));
  664. glPopMatrix();
  665. }
  666. }
  667.  
  668. glPushMatrix();
  669. glScalef(listaPosObj[select]->scale, listaPosObj[select]->scale, listaPosObj[select]->scale);
  670. glTranslatef(listaPosObj[select]->x, listaPosObj[select]->y, listaPosObj[select]->z);
  671. glRotatef(listaPosObj[select]->rotationX, 1, 0, 0);
  672. glRotatef(listaPosObj[select]->rotationY, 0, 1, 0);
  673. glRotatef(listaPosObj[select]->rotationZ, 0, 0, 1);
  674. glCallList(listaObj.at(select));
  675. glPopMatrix();
  676. }
  677.  
  678. void mydisplay()
  679. {
  680. cout << "Don't remove me, I am running." << endl;
  681.  
  682. glMatrixMode(GL_MODELVIEW);
  683.  
  684. glClearColor(0, 0, 0, 0);
  685.  
  686. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  687.  
  688. glMatrixMode(GL_MODELVIEW);
  689.  
  690. draw();
  691.  
  692. glutSwapBuffers();
  693. }
  694. bool luz1 = false, luz2 = false;
  695. bool apertou = false;
  696. void handleKeyboardPressed(unsigned char key, int x, int y)
  697. {
  698. buffer[(int)key] = true;
  699. if (key == '>' || key == '.') {
  700. if ((select + 1) == listaObj.size()) {
  701. luz1 = false;
  702. luz2 = true;
  703. }
  704. else {
  705. luz1 = false;
  706. luz2 = false;
  707. select++;
  708. }
  709. apertou = true;
  710. }
  711. if (key == '<' || key == ',') {
  712. if ((select - 1) == -1) {
  713. luz1 = true;
  714. luz2 = false;
  715. }
  716. else {
  717. luz1 = false;
  718. luz2 = false;
  719. select--;
  720. }
  721. apertou = true;
  722. }
  723.  
  724. if (key == 'e') {
  725. dolly = !dolly;
  726. extrinsic[14] = -300.0;
  727. znear = 30;
  728. printf("%d dolly /n", dolly);
  729. }
  730.  
  731. if (key == 'm') {
  732. printf("%f dollyz /n", extrinsic[14]);
  733. printf("%f dollyznear /n", znear);
  734. }
  735. }
  736.  
  737. void handleKeyboardUp(unsigned char key, int x, int y)
  738. {
  739. apertou = false;
  740. buffer[(int)key] = false;
  741. }
  742.  
  743. void idleFunction() // Processa entradas no teclado
  744. {
  745. // #### Resumo ####
  746.  
  747. // Mover objeto: 1, 2, 3, 4, 5, 6
  748. // Rotacionar objeto: 7, 8, 9
  749. // Redimensionar objeto: -\_, =\+
  750. // Mover camera:
  751. // Exit: ESC
  752.  
  753. // #### Comandos dos Objetos ####
  754.  
  755. if (buffer['7'] == true) {
  756. listaPosObj[select]->rotationX += r;
  757. if (listaPosObj[select]->rotationX > 360) {
  758. listaPosObj[select]->rotationX -= 360;
  759. }
  760. apertou = true;
  761. }
  762. if (buffer['8'] == true) {
  763. listaPosObj[select]->rotationY += r;
  764. if (listaPosObj[select]->rotationY > 360) {
  765. listaPosObj[select]->rotationY -= 360;
  766. }
  767. apertou = true;
  768. }
  769. if (buffer['9'] == true) {
  770. listaPosObj[select]->rotationZ += r;
  771. if (listaPosObj[select]->rotationZ > 360) {
  772. listaPosObj[select]->rotationZ -= 360;
  773. }
  774. apertou = true;
  775. }
  776. if (buffer['1'] == true) {
  777. if (luz1) {
  778. lx -= 0.01;
  779. GLfloat light_position[] = { lx, ly, lz, 0.0 };
  780. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  781. }
  782. else if (luz2) {
  783. lx1 -= 0.01;
  784. GLfloat light1_position[] = { lx1, ly1, lz1, 0.0 };
  785. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  786. }
  787. else {
  788. listaPosObj[select]->x -= d;
  789. }
  790. apertou = true;
  791. }
  792. if (buffer['2'] == true) {
  793. if (luz1) {
  794. lx += 0.01;
  795. GLfloat light_position[] = { lx, ly, lz, 0.0 };
  796. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  797. }
  798. else if (luz2) {
  799. lx1 += 0.01;
  800. GLfloat light1_position[] = { lx1, ly1, lz1, 0.0 };
  801. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  802. }
  803. else {
  804. listaPosObj[select]->x += d;
  805. }
  806. apertou = true;
  807. }
  808. if (buffer['3'] == true) {
  809. if (luz1) {
  810. GLfloat light_position[] = { lx, ly - 1, lz, 0.0 };
  811. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  812. }
  813. else if (luz2) {
  814. GLfloat light1_position[] = { lx1, ly1 - 1, lz1, 0.0 };
  815. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  816. }
  817. else {
  818. listaPosObj[select]->y -= d;
  819. }
  820. apertou = true;
  821. }
  822. if (buffer['4'] == true) {
  823. if (luz1) {
  824. GLfloat light_position[] = { lx, ly + 1, lz, 0.0 };
  825. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  826. }
  827. else if (luz2) {
  828. GLfloat light1_position[] = { lx1, ly1 + 1, lz1, 0.0 };
  829. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  830. }
  831. else {
  832. listaPosObj[select]->y += d;
  833. }
  834. apertou = true;
  835. }
  836. if (buffer['5'] == true) {
  837. if (luz1) {
  838. GLfloat light_position[] = { lx, ly, lz - 1, 0.0 };
  839. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  840. }
  841. else if (luz2) {
  842. GLfloat light1_position[] = { lx1, ly1, lz1 - 1, 0.0 };
  843. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  844. }
  845. else {
  846. listaPosObj[select]->z -= d;
  847. }
  848. apertou = true;
  849. }
  850. if (buffer['6'] == true) {
  851. if (luz1) {
  852. lz += 0.01;
  853. GLfloat light_position[] = { lx, ly, lz, 0.0 };
  854. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  855. }
  856. else if (luz2) {
  857. lz1 += 0.01;
  858. GLfloat light1_position[] = { lx1, ly1, lz1 + 1, 0.0 };
  859. glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  860. }
  861. else {
  862. listaPosObj[select]->z += d;
  863. }
  864. apertou = true;
  865. }
  866. if (buffer['-'] == true || buffer['_'] == true && !luz1 && !luz2) {
  867. listaPosObj[select]->scale -= 0.01;
  868. apertou = true;
  869. }
  870. if (buffer['='] == true || buffer['+'] == true && !luz1 && !luz2) {
  871. listaPosObj[select]->scale += 0.01;
  872. apertou = true;
  873. }
  874. // #### Comandos de câmera ####
  875.  
  876. if ((buffer['w'] && buffer['s']) || (buffer['a'] && buffer['d'])) {
  877. // nada acontece
  878. }
  879. else {
  880. if (buffer['w']) {
  881. CameraTranslateZ(fatorTranslacao);
  882. CameraPos[0] = extrinsic[12];
  883. CameraPos[1] = extrinsic[13];
  884. CameraPos[2] = extrinsic[14];
  885. apertou = true;
  886. }
  887. if (buffer['s']) {
  888. CameraTranslateZ(-fatorTranslacao);
  889. CameraPos[0] = extrinsic[12];
  890. CameraPos[1] = extrinsic[13];
  891. CameraPos[2] = extrinsic[14];
  892. apertou = true;
  893. }
  894. if (buffer['d']) {
  895. CameraTranslateX(-fatorTranslacao);
  896. CameraPos[0] = extrinsic[12];
  897. CameraPos[1] = extrinsic[13];
  898. CameraPos[2] = extrinsic[14];
  899. apertou = true;
  900. }
  901. if (buffer['a']) {
  902. CameraTranslateX(fatorTranslacao);
  903. CameraPos[0] = extrinsic[12];
  904. CameraPos[1] = extrinsic[13];
  905. CameraPos[2] = extrinsic[14];
  906. apertou = true;
  907. }
  908. }
  909.  
  910. if (buffer['i']){
  911. anguloRotacaoX = anguloRotacaoX + 0.0007;
  912. CameraRotateX(rotacaoX, anguloRotacaoX);
  913. MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
  914. //MultMat(translacao, rotacao, extrinsic);
  915. MultMat(rotacao, translacao, extrinsic);
  916. CameraLook[0] = extrinsic[2];
  917. CameraLook[1] = extrinsic[6];
  918. CameraLook[2] = extrinsic[10];
  919. apertou = true;
  920.  
  921. }
  922.  
  923. if (buffer['k']){
  924. anguloRotacaoX = anguloRotacaoX - 0.0007;
  925. CameraRotateX(rotacaoX, anguloRotacaoX);
  926. MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
  927. //MultMat(translacao, rotacao, extrinsic);
  928. MultMat(rotacao, translacao, extrinsic);
  929. CameraLook[0] = extrinsic[2];
  930. CameraLook[1] = extrinsic[6];
  931. CameraLook[2] = extrinsic[10];
  932. apertou = true;
  933.  
  934. }
  935.  
  936. if (buffer['j']){
  937. anguloRotacaoY = anguloRotacaoY + 0.0007;
  938. CameraRotateY(rotacaoY, anguloRotacaoY);
  939. MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
  940. //MultMat(translacao, rotacao, extrinsic);
  941. MultMat(rotacao, translacao, extrinsic);
  942. CameraLook[0] = extrinsic[2];
  943. CameraLook[1] = extrinsic[6];
  944. CameraLook[2] = extrinsic[10];
  945. apertou = true;
  946.  
  947. }
  948.  
  949. if (buffer['l']){
  950. anguloRotacaoY = anguloRotacaoY - 0.0007;
  951. CameraRotateY(rotacaoY, anguloRotacaoY);
  952. MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
  953. //MultMat(translacao, rotacao, extrinsic);
  954. MultMat(rotacao, translacao, extrinsic);
  955. CameraLook[0] = extrinsic[2];
  956. CameraLook[1] = extrinsic[6];
  957. CameraLook[2] = extrinsic[10];
  958. apertou = true;
  959.  
  960. }
  961.  
  962. // comandos dolly
  963. if (buffer['r']) {
  964. if (dolly){
  965. znear += 0.5;
  966. CameraTranslateZ(-fatorTranslacao * 50);
  967. myreshape(wWidth, wHeight);
  968. apertou = true;
  969. }
  970. }
  971. if (buffer['f']) {
  972. if (dolly){
  973. znear -= 0.5;
  974. CameraTranslateZ(fatorTranslacao * 50);
  975. myreshape(wWidth, wHeight);
  976. apertou = true;
  977. }
  978. }
  979.  
  980. // #### Outros comandos ####
  981.  
  982. if (apertou) {
  983. mydisplay();
  984. }
  985. else {
  986. apertou = false;
  987. }
  988. if (buffer[27] == true) // ESC
  989. exit(0);
  990. }
  991.  
  992. void mouse_func(int button, int state, int x, int y) {
  993. old_x = x;
  994. old_y = y;
  995. valid = state == GLUT_DOWN;
  996. }
  997.  
  998. void motion_func(int x, int y) {
  999. if (valid) {
  1000. int dx = old_x - x;
  1001. int dy = old_y - y;
  1002. CameraRotateY(rotacaoY, dx * 0.001);
  1003. MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
  1004. //MultMat(translacao, rotacao, extrinsic);
  1005. MultMat(rotacao, translacao, extrinsic);
  1006.  
  1007. CameraRotateX(rotacaoX, dy * 0.0001);
  1008. MultMat(rotacaoX, rotacaoY, rotacao); //rotacao global
  1009. //MultMat(translacao, rotacao, extrinsic);
  1010. MultMat(rotacao, translacao, extrinsic);
  1011. }
  1012. old_x = x;
  1013. old_y = y;
  1014. mydisplay();
  1015. }
  1016.  
  1017. int main(int argc, char **argv)
  1018. {
  1019. glutInit(&argc, argv);
  1020. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  1021. glutInitWindowSize(wWidth, wHeight);
  1022. glutInitWindowPosition(0, 0);
  1023. glutCreateWindow("OpenGL");
  1024. glutFullScreen();
  1025. initLight();
  1026. glutDisplayFunc(mydisplay);
  1027. glutReshapeFunc(myreshape);
  1028. glutMouseFunc(mouse_func);
  1029. glutMotionFunc(motion_func);
  1030. glutKeyboardUpFunc(handleKeyboardUp);
  1031. glutKeyboardFunc(handleKeyboardPressed);
  1032. glutIdleFunc(idleFunction);
  1033. listar();
  1034. for (int i = 0; i < listaArquivos.size(); i++) {
  1035. loadObj(listaArquivos[i]->c_str());
  1036. }
  1037. //glTranslatef(0, 0, -50);
  1038. initialize();
  1039. glutMainLoop();
  1040. return 0;
  1041. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement