Open Source Tomb Raider Engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Mesh.cpp 14KB

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