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

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