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

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