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 16KB

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