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

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