Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Mesh.cpp 13KB

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