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

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