Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Mesh.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*!
  2. * \file src/Mesh.cpp
  3. * \brief OpenGL Mesh
  4. *
  5. * \author Mongoose
  6. */
  7. #include <stdlib.h>
  8. #include "global.h"
  9. #include "TextureManager.h"
  10. #include "Mesh.h"
  11. void Mesh::addTexturedRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d,
  12. uint16_t textile) {
  13. texturedRectangles.emplace_back(a, b, c, d, textile);
  14. }
  15. void Mesh::addTexturedTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, uint16_t textile) {
  16. texturedTriangles.emplace_back(a, b, c, glm::vec3(), textile);
  17. }
  18. void Mesh::addColoredRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d, float re,
  19. float gr, float bl) {
  20. coloredRectangles.emplace_back(a, b, c, d, -1, re, gr, bl);
  21. }
  22. void Mesh::addColoredTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, float re, float gr, float bl) {
  23. coloredTriangles.emplace_back(a, b, c, glm::vec3(), -1, re, gr, bl);
  24. }
  25. void Mesh::addNormal(glm::vec3 n) {
  26. normals.emplace_back(n);
  27. }
  28. void Mesh::drawAlpha() {
  29. if ((texturedRectangles.size() == 0)
  30. && (texturedTriangles.size() == 0)
  31. && (coloredRectangles.size() == 0)
  32. && (coloredTriangles.size() == 0)
  33. && (normals.size() == 0)) {
  34. drawAlphaOld();
  35. return;
  36. }
  37. // TODO
  38. }
  39. void Mesh::drawSolid() {
  40. if ((texturedRectangles.size() == 0)
  41. && (texturedTriangles.size() == 0)
  42. && (coloredRectangles.size() == 0)
  43. && (coloredTriangles.size() == 0)
  44. && (normals.size() == 0)) {
  45. drawSolidOld();
  46. return;
  47. }
  48. for (auto& q : texturedRectangles) {
  49. if (mMode == MeshModeWireframe) {
  50. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  51. /*
  52. glBegin(GL_QUADS);
  53. glTexCoord2f(0.0f, 0.0f);
  54. glVertex3f(q.a.x, q.a.y, q.a.z);
  55. glTexCoord2f(1.0f, 0.0f);
  56. glVertex3f(q.b.x, q.b.y, q.b.z);
  57. glTexCoord2f(1.0f, 1.0f);
  58. glVertex3f(q.c.x, q.c.y, q.c.z);
  59. glTexCoord2f(0.0f, 1.0f);
  60. glVertex3f(q.d.x, q.d.y, q.d.z);
  61. glEnd();
  62. */
  63. } else if (mMode == MeshModeSolid) {
  64. // TODO
  65. } else if (mMode == MeshModeTexture) {
  66. //getTextureManager().getTile(q.texture).displayRectangle(q.a, q.b, q.c, q.d);
  67. }
  68. }
  69. for (auto& t : texturedTriangles) {
  70. if (mMode == MeshModeWireframe) {
  71. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  72. /*
  73. glBegin(GL_TRIANGLES);
  74. glTexCoord2f(0.0f, 0.0f);
  75. glVertex3f(t.a.x, t.a.y, t.a.z);
  76. glTexCoord2f(1.0f, 0.0f);
  77. glVertex3f(t.b.x, t.b.y, t.b.z);
  78. glTexCoord2f(1.0f, 1.0f);
  79. glVertex3f(t.c.x, t.c.y, t.c.z);
  80. glEnd();
  81. */
  82. } else if (mMode == MeshModeSolid) {
  83. // TODO
  84. } else if (mMode == MeshModeTexture) {
  85. //getTextureManager().getTile(t.texture).displayTriangle(t.a, t.b, t.c);
  86. }
  87. }
  88. for (auto& q : coloredRectangles) {
  89. if (mMode == MeshModeWireframe) {
  90. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  91. /*
  92. glBegin(GL_QUADS);
  93. glTexCoord2f(0.0f, 0.0f);
  94. glVertex3f(q.a.x, q.a.y, q.a.z);
  95. glTexCoord2f(1.0f, 0.0f);
  96. glVertex3f(q.b.x, q.b.y, q.b.z);
  97. glTexCoord2f(1.0f, 1.0f);
  98. glVertex3f(q.c.x, q.c.y, q.c.z);
  99. glTexCoord2f(0.0f, 1.0f);
  100. glVertex3f(q.d.x, q.d.y, q.d.z);
  101. glEnd();
  102. */
  103. } else if (mMode == MeshModeSolid) {
  104. // TODO
  105. } else if (mMode == MeshModeTexture) {
  106. // TODO
  107. }
  108. }
  109. for (auto& t : coloredTriangles) {
  110. if (mMode == MeshModeWireframe) {
  111. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  112. /*
  113. glBegin(GL_TRIANGLES);
  114. glTexCoord2f(0.0f, 0.0f);
  115. glVertex3f(t.a.x, t.a.y, t.a.z);
  116. glTexCoord2f(1.0f, 0.0f);
  117. glVertex3f(t.b.x, t.b.y, t.b.z);
  118. glTexCoord2f(1.0f, 1.0f);
  119. glVertex3f(t.c.x, t.c.y, t.c.z);
  120. glEnd();
  121. */
  122. } else if (mMode == MeshModeSolid) {
  123. // TODO
  124. } else if (mMode == MeshModeTexture) {
  125. // TODO
  126. }
  127. }
  128. }
  129. ////////////////////////////////////////////////////////////
  130. // Constructors
  131. ////////////////////////////////////////////////////////////
  132. Mesh::Mesh() {
  133. mNumVertices = 0;
  134. mVertices = 0x0;
  135. mNumNormals = 0;
  136. mNormals = 0x0;
  137. mNumColors = 0;
  138. mColors = 0x0;
  139. mNumTris = 0;
  140. mTris = 0x0;
  141. mNumQuads = 0;
  142. mQuads = 0x0;
  143. mVertexArray = 0x0;
  144. mNormalArray = 0x0;
  145. mColorArray = 0x0;
  146. mTriangleCount = 0;
  147. mTriangleTextures = 0x0;
  148. mTriangleIndices = 0x0;
  149. mTriangleFlags = 0x0;
  150. mTriangleTexCoordArray = 0x0;
  151. mFlags = 0;
  152. mMode = MeshModeTexture;
  153. }
  154. Mesh::~Mesh() {
  155. for (unsigned int i = 0; i < mNumVertices; i++)
  156. delete [] mVertices[i];
  157. delete [] mVertices;
  158. mVertices = nullptr;
  159. for (unsigned int i = 0; i < mNumNormals; i++)
  160. delete [] mNormals[i];
  161. delete [] mNormals;
  162. mNormals = nullptr;
  163. for (unsigned int i = 0; i < mNumColors; i++)
  164. delete [] mColors[i];
  165. delete [] mColors;
  166. mColors = nullptr;
  167. if (mTris) {
  168. for (unsigned int i = 0; i < mNumTris; ++i) {
  169. delete [] mTris[i].triangles;
  170. delete [] mTris[i].alpha_triangles;
  171. delete [] mTris[i].texcoors;
  172. delete [] mTris[i].texcoors2;
  173. }
  174. delete [] mTris;
  175. mTris = nullptr;
  176. }
  177. if (mQuads) {
  178. for (unsigned int i = 0; i < mNumQuads; ++i) {
  179. delete [] mQuads[i].quads;
  180. delete [] mQuads[i].alpha_quads;
  181. delete [] mQuads[i].texcoors;
  182. delete [] mQuads[i].texcoors2;
  183. }
  184. delete [] mQuads;
  185. mQuads = nullptr;
  186. }
  187. delete [] mVertexArray;
  188. mVertexArray = nullptr;
  189. delete [] mNormalArray;
  190. mNormalArray = nullptr;
  191. delete [] mColorArray;
  192. mColorArray = nullptr;
  193. delete [] mTriangleTextures;
  194. mTriangleTextures = nullptr;
  195. delete [] mTriangleIndices;
  196. mTriangleIndices = nullptr;
  197. delete [] mTriangleFlags;
  198. mTriangleFlags = nullptr;
  199. delete [] mTriangleTexCoordArray;
  200. mTriangleTexCoordArray = nullptr;
  201. }
  202. ////////////////////////////////////////////////////////////
  203. // Public Accessors
  204. ////////////////////////////////////////////////////////////
  205. void Mesh::drawAlphaOld() {
  206. unsigned int i, j, k, index;
  207. /*
  208. // Render quadralaterals
  209. for (mQuads ? i = 0 : i = mNumQuads; i < mNumQuads; ++i) {
  210. switch (mMode) {
  211. case MeshModeWireframe:
  212. glColor3f(0.0, 0.0, 1.0);
  213. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  214. break;
  215. case MeshModeSolid:
  216. // Bind WHITE texture for solid colors
  217. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  218. break;
  219. case MeshModeTexture:
  220. case MeshModeMultiTexture:
  221. // Bind texture id for textures
  222. getTextureManager().bindTextureId(mQuads[i].texture);
  223. break;
  224. }
  225. glBegin(GL_QUADS);
  226. for (j = 0; j < mQuads[i].num_alpha_quads; ++j) {
  227. for (k = 0; k < 4; ++k) {
  228. index = mQuads[i].alpha_quads[j * 4 + k];
  229. glTexCoord2fv(mQuads[i].texcoors2[j * 4 + k]);
  230. glColor4fv(mColors[index]);
  231. glVertex3fv(mVertices[index]);
  232. }
  233. }
  234. glEnd();
  235. }
  236. // Render triangles
  237. for (mTris ? i = 0 : i = mNumTris; i < mNumTris; ++i) {
  238. switch (mMode) {
  239. case MeshModeWireframe:
  240. glColor3f(0.0, 1.0, 0.0);
  241. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  242. break;
  243. case MeshModeSolid:
  244. // Bind WHITE texture for solid colors
  245. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  246. break;
  247. case MeshModeTexture:
  248. case MeshModeMultiTexture:
  249. // Bind texture id for textures
  250. getTextureManager().bindTextureId(mTris[i].texture);
  251. break;
  252. }
  253. glBegin(GL_TRIANGLES);
  254. for (j = 0; j < mTris[i].num_alpha_triangles; ++j) {
  255. for (k = 0; k < 3; ++k) {
  256. index = mTris[i].alpha_triangles[j * 3 + k];
  257. glTexCoord2fv(mTris[i].texcoors2[j * 3 + k]);
  258. glColor4fv(mColors[index]);
  259. glVertex3fv(mVertices[index]);
  260. }
  261. }
  262. glEnd();
  263. }
  264. */
  265. }
  266. void Mesh::drawSolidOld() {
  267. unsigned int i, j, k, index;
  268. /*
  269. // Render quadralaterals
  270. for (mQuads ? i = 0 : i = mNumQuads; i < mNumQuads; ++i) {
  271. switch (mMode) {
  272. case MeshModeSolid:
  273. glColor3f(0.0, 0.0, 0.0);
  274. break;
  275. case MeshModeWireframe:
  276. // Bind WHITE texture for solid colors
  277. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  278. break;
  279. #ifdef MULTITEXTURE
  280. case MeshModeMultiTexture:
  281. glActiveTextureARB(GL_TEXTURE0_ARB);
  282. glEnable(GL_TEXTURE_2D);
  283. getTextureManager().bindTextureId(mQuads[i].texture);
  284. glActiveTextureARB(GL_TEXTURE1_ARB);
  285. glEnable(GL_TEXTURE_2D);
  286. getTextureManager().bindTextureId(mQuads[i].bumpmap);
  287. break;
  288. #else
  289. case MeshModeMultiTexture:
  290. #endif
  291. case MeshModeTexture:
  292. // Bind texture id for textures
  293. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  294. getTextureManager().bindTextureId(mQuads[i].texture);
  295. break;
  296. }
  297. glBegin(GL_QUADS);
  298. for (j = 0; j < mQuads[i].num_quads; ++j) {
  299. for (k = 0; k < 4; ++k) {
  300. index = mQuads[i].quads[j * 4 + k];
  301. glColor4fv(mColors[index]);
  302. #ifdef MULTITEXTURE
  303. if (mMode == MeshModeMultiTexture) {
  304. glMultiTexCoord2fvARB(GL_TEXTURE0_ARB,
  305. mQuads[i].texcoors[j * 4 + k]);
  306. glMultiTexCoord2fvARB(GL_TEXTURE1_ARB,
  307. mQuads[i].texcoors[j * 4 + k]);
  308. } else
  309. #endif
  310. glTexCoord2fv(mQuads[i].texcoors[j * 4 + k]);
  311. glVertex3fv(mVertices[index]);
  312. }
  313. }
  314. glEnd();
  315. }
  316. // Render triangles
  317. for (mTris ? i = 0 : i = mNumTris; i < mNumTris; ++i) {
  318. switch (mMode) {
  319. case MeshModeSolid:
  320. glColor3f(1.0, 0.0, 0.0);
  321. break;
  322. case MeshModeWireframe:
  323. // Bind WHITE texture for solid colors
  324. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  325. break;
  326. #ifdef MULTITEXTURE
  327. case MeshModeMultiTexture:
  328. glActiveTextureARB(GL_TEXTURE0_ARB);
  329. glEnable(GL_TEXTURE_2D);
  330. getTextureManager().bindTextureId(mTris[i].texture);
  331. glActiveTextureARB(GL_TEXTURE1_ARB);
  332. glEnable(GL_TEXTURE_2D);
  333. getTextureManager().bindTextureId(mTris[i].bumpmap);
  334. break;
  335. #else
  336. case MeshModeMultiTexture:
  337. #endif
  338. case MeshModeTexture:
  339. // Bind texture id for textures
  340. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  341. getTextureManager().bindTextureId(mTris[i].texture);
  342. break;
  343. }
  344. glBegin(GL_TRIANGLES);
  345. for (j = 0; j < mTris[i].num_triangles; ++j) {
  346. for (k = 0; k < 3; ++k) {
  347. index = mTris[i].triangles[j * 3 + k];
  348. #ifdef MULTITEXTURE
  349. if (mMode == MeshModeMultiTexture) {
  350. glMultiTexCoord2fvARB(GL_TEXTURE0_ARB,
  351. mTris[i].texcoors[j * 3 + k]);
  352. glMultiTexCoord2fvARB(GL_TEXTURE1_ARB,
  353. mTris[i].texcoors[j * 3 + k]);
  354. } else
  355. #endif
  356. glTexCoord2fv(mTris[i].texcoors[j * 3 + k]);
  357. glColor4fv(mColors[index]);
  358. glVertex3fv(mVertices[index]);
  359. }
  360. }
  361. glEnd();
  362. }
  363. #ifdef MULTITEXTURE
  364. if (mMode == MeshModeMultiTexture) {
  365. glDisable(GL_TEXTURE_2D);
  366. glActiveTextureARB(GL_TEXTURE0_ARB);
  367. }
  368. #endif
  369. */
  370. }
  371. ////////////////////////////////////////////////////////////
  372. // Public Mutators
  373. ////////////////////////////////////////////////////////////
  374. void Mesh::allocateColors(unsigned int n) {
  375. if (mColors) {
  376. for (unsigned int i = 0; i < mNumColors; i++)
  377. delete [] mColors[i];
  378. delete [] mColors;
  379. mNumColors = 0;
  380. }
  381. if (!n) {
  382. return;
  383. }
  384. mNumColors = n;
  385. mColors = new float *[mNumColors];
  386. for (unsigned int i = 0; i < mNumColors; i++)
  387. mColors[i] = new float[4];
  388. }
  389. void Mesh::allocateNormals(unsigned int n) {
  390. if (mNormals) {
  391. for (unsigned int i = 0; i < mNumNormals; i++)
  392. delete [] mNormals[i];
  393. delete [] mNormals;
  394. mNumNormals = 0;
  395. }
  396. if (!n) {
  397. return;
  398. }
  399. mNumNormals = n;
  400. mNormals = new float *[mNumNormals];
  401. for (unsigned int i = 0; i < mNumNormals; i++)
  402. mNormals[i] = new float[3];
  403. }
  404. void Mesh::allocateRectangles(unsigned int n) {
  405. if (mQuads) {
  406. mNumQuads = 0;
  407. delete [] mQuads;
  408. }
  409. if (!n) {
  410. return;
  411. }
  412. mNumQuads = n;
  413. mQuads = new rect_t[mNumQuads];
  414. }
  415. void Mesh::allocateTriangles(unsigned int n) {
  416. if (mTris) {
  417. mNumTris = 0;
  418. delete [] mTris;
  419. }
  420. if (!n) {
  421. return;
  422. }
  423. mNumTris = n;
  424. mTris = new tris_t[mNumTris];
  425. }
  426. void Mesh::allocateVertices(unsigned int n) {
  427. if (mVertices) {
  428. for (unsigned int i = 0; i < mNumVertices; i++)
  429. delete [] mVertices[i];
  430. delete [] mVertices;
  431. mNumVertices = 0;
  432. }
  433. if (!n) {
  434. return;
  435. }
  436. mNumVertices = n;
  437. mVertices = new float *[mNumVertices];
  438. for (unsigned int i = 0; i < mNumVertices; i++)
  439. mVertices[i] = new float[3];
  440. }
  441. void Mesh::bufferColorArray(unsigned int colorCount, float* colors) {
  442. if (mColors) {
  443. for (unsigned int i = 0; i < mNumColors; i++)
  444. delete [] mColors[i];
  445. delete [] mColors;
  446. mNumColors = 0;
  447. }
  448. if (!colorCount) {
  449. return;
  450. }
  451. mNumColors = colorCount;
  452. mColorArray = colors;
  453. }
  454. void Mesh::bufferNormalArray(unsigned int normalCount, float* normals) {
  455. if (mNormals) {
  456. for (unsigned int i = 0; i < mNumNormals; i++)
  457. delete [] mNormals[i];
  458. delete [] mNormals;
  459. mNumNormals = 0;
  460. }
  461. if (!normalCount) {
  462. return;
  463. }
  464. mNumNormals = normalCount;
  465. mNormalArray = normals;
  466. }
  467. void Mesh::bufferTriangles(unsigned int count,
  468. unsigned int* indices, float* texCoords,
  469. int* textures, unsigned int* flags) {
  470. mTriangleCount = count;
  471. mTriangleTextures = textures;
  472. mTriangleIndices = indices;
  473. mTriangleFlags = flags;
  474. mTriangleTexCoordArray = texCoords;
  475. //! \fixme sortTrianglesByTexture();
  476. }
  477. void Mesh::bufferVertexArray(unsigned int vertexCount, float* vertices) {
  478. if (mVertices) {
  479. for (unsigned int i = 0; i < mNumVertices; i++)
  480. delete [] mVertices[i];
  481. delete [] mVertices;
  482. mNumVertices = 0;
  483. }
  484. if (!vertexCount) {
  485. return;
  486. }
  487. mNumVertices = vertexCount;
  488. mVertexArray = vertices;
  489. mFlags |= fMesh_UseVertexArray;
  490. }
  491. void Mesh::setColor(unsigned int index,
  492. float r, float g, float b, float a) {
  493. assert(index < mNumColors);
  494. mColors[index][0] = r;
  495. mColors[index][1] = g;
  496. mColors[index][2] = b;
  497. mColors[index][3] = a;
  498. }
  499. void Mesh::setColor(unsigned int index, float rgba[4]) {
  500. assert(index < mNumColors);
  501. mColors[index][0] = rgba[0];
  502. mColors[index][1] = rgba[1];
  503. mColors[index][2] = rgba[2];
  504. mColors[index][3] = rgba[3];
  505. }
  506. void Mesh::setNormal(unsigned int index, float i, float j, float k) {
  507. assert(index < mNumNormals);
  508. mNormals[index][0] = i;
  509. mNormals[index][1] = j;
  510. mNormals[index][2] = k;
  511. }
  512. void Mesh::setVertex(unsigned int index, float x, float y, float z) {
  513. assert(index < mNumVertices);
  514. mVertices[index][0] = x;
  515. mVertices[index][1] = y;
  516. mVertices[index][2] = z;
  517. }
  518. ////////////////////////////////////////////////////////////
  519. // Private Accessors
  520. ////////////////////////////////////////////////////////////
  521. ////////////////////////////////////////////////////////////
  522. // Private Mutators
  523. ////////////////////////////////////////////////////////////