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.

StaticMesh.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*!
  2. * \file src/StaticMesh.cpp
  3. * \brief Static Model Meshes
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Game.h"
  9. #include "Render.h"
  10. #include "TextureManager.h"
  11. #include "World.h"
  12. #include "utils/pixel.h"
  13. #include "StaticMesh.h"
  14. TexturedTriangle::TexturedTriangle(int i[3], float s[6], int tex, unsigned short trans) {
  15. index[0] = i[0];
  16. index[1] = i[1];
  17. index[2] = i[2];
  18. st[0] = s[0];
  19. st[1] = s[1];
  20. st[2] = s[2];
  21. st[3] = s[3];
  22. st[4] = s[4];
  23. st[5] = s[5];
  24. texture = tex;
  25. transparency = trans;
  26. }
  27. void TexturedTriangle::display(float* vertices, float* colors, float* normals) {
  28. assert(vertices != NULL);
  29. if ((getRender().getMode() != Render::modeWireframe)
  30. && (getRender().getMode() != Render::modeSolid)) {
  31. getTextureManager().bindTextureId(texture);
  32. }
  33. glBegin(GL_TRIANGLES);
  34. switch (getRender().getMode()) {
  35. case Render::modeSolid:
  36. case Render::modeVertexLight:
  37. if (colors != NULL) {
  38. glColor3fv(colors + index[0]);
  39. glTexCoord2fv(st);
  40. glVertex3fv(vertices + (index[0] * 3));
  41. glColor3fv(colors + index[1]);
  42. glTexCoord2fv(st + 2);
  43. glVertex3fv(vertices + (index[1] * 3));
  44. glColor3fv(colors + index[2]);
  45. glTexCoord2fv(st + 4);
  46. glVertex3fv(vertices + (index[2] * 3));
  47. } else if (normals != NULL) {
  48. glNormal3fv(normals + (index[0] * 3));
  49. glTexCoord2fv(st);
  50. glVertex3fv(vertices + (index[0] * 3));
  51. glNormal3fv(normals + (index[1] * 3));
  52. glTexCoord2fv(st + 2);
  53. glVertex3fv(vertices + (index[1] * 3));
  54. glNormal3fv(normals + (index[2] * 3));
  55. glTexCoord2fv(st + 4);
  56. glVertex3fv(vertices + (index[2] * 3));
  57. } else {
  58. glTexCoord2fv(st);
  59. glVertex3fv(vertices + (index[0] * 3));
  60. glTexCoord2fv(st + 2);
  61. glVertex3fv(vertices + (index[1] * 3));
  62. glTexCoord2fv(st + 4);
  63. glVertex3fv(vertices + (index[2] * 3));
  64. }
  65. break;
  66. case Render::modeWireframe:
  67. glVertex3fv(vertices + (index[0] * 3));
  68. glVertex3fv(vertices + (index[1] * 3));
  69. glVertex3fv(vertices + (index[2] * 3));
  70. break;
  71. default:
  72. glTexCoord2fv(st);
  73. glVertex3fv(vertices + (index[0] * 3));
  74. glTexCoord2fv(st + 2);
  75. glVertex3fv(vertices + (index[1] * 3));
  76. glTexCoord2fv(st + 4);
  77. glVertex3fv(vertices + (index[2] * 3));
  78. }
  79. glEnd();
  80. }
  81. // ----------------------------------------------------------------------------
  82. StaticMesh::StaticMesh(int i, int m) : id(i), mesh(m) { }
  83. #ifdef EXPERIMENTAL
  84. #include <map>
  85. std::map<unsigned int, unsigned int> gColorTextureHACK;
  86. int setupTextureColor(float* colorf) {
  87. unsigned char color[4];
  88. unsigned int colorI;
  89. unsigned int texture;
  90. color[0] = (unsigned char)(colorf[0] * 255.0f);
  91. color[1] = (unsigned char)(colorf[1] * 255.0f);
  92. color[2] = (unsigned char)(colorf[2] * 255.0f);
  93. color[3] = (unsigned char)(colorf[3] * 255.0f);
  94. ((unsigned char*)(&colorI))[3] = color[0];
  95. ((unsigned char*)(&colorI))[2] = color[1];
  96. ((unsigned char*)(&colorI))[1] = color[2];
  97. ((unsigned char*)(&colorI))[0] = color[3];
  98. try {
  99. texture = gColorTextureHACK.at(colorI);
  100. } catch (...) {
  101. unsigned char* image = generateColorTexture(color, 32, 32, 32);
  102. texture = getTextureManager().loadBufferSlot(image, 32, 32, RGBA, 32);
  103. delete [] image;
  104. }
  105. return texture;
  106. }
  107. #endif
  108. StaticMesh::StaticMesh(TombRaider& tr, unsigned int index) {
  109. int count, texture;
  110. int vertexIndices[6];
  111. float st[12];
  112. float color[4];
  113. unsigned short transparency;
  114. if (!tr.isMeshValid(index)) {
  115. dontshow = true;
  116. return;
  117. } else {
  118. dontshow = false;
  119. }
  120. // Mongoose 2002.08.30, Testing support for 'shootable' models ( traceable )
  121. tr.getMeshCollisionInfo(index, center, &radius);
  122. //! \fixme Arrays don't work either =)
  123. // Mesh geometery, colors, etc
  124. tr.getMeshVertexArrays(index,
  125. &vertexCount, &vertices,
  126. &normalCount, &normals,
  127. &colorCount, &colors);
  128. // Textured Triangles
  129. count = tr.getMeshTexturedTriangleCount(index);
  130. for (int i = 0; i < count; i++) {
  131. tr.getMeshTexturedTriangle(index, i,
  132. vertexIndices, st,
  133. &texture, &transparency);
  134. triangles.push_back(
  135. new TexturedTriangle(vertexIndices, st, texture, transparency));
  136. }
  137. // Coloured Triangles
  138. count = tr.getMeshColoredTriangleCount(index);
  139. for (int i = 0; i < count; i++) {
  140. tr.getMeshColoredTriangle(index, i,
  141. vertexIndices, color);
  142. st[0] = color[0];
  143. st[1] = color[1];
  144. st[2] = color[2];
  145. st[3] = color[3];
  146. st[4] = 1.0;
  147. st[5] = 1.0;
  148. #ifdef EXPERIMENTAL
  149. texture = setupTextureColor(color);
  150. #else
  151. texture = 0; // White texture
  152. #endif
  153. transparency = 0;
  154. triangles.push_back(
  155. new TexturedTriangle(vertexIndices, st, texture, transparency));
  156. }
  157. // Textured Rectangles
  158. count = tr.getMeshTexturedRectangleCount(index);
  159. for (int i = 0; i < count; i++) {
  160. tr.getMeshTexturedRectangle(index, i,
  161. vertexIndices, st,
  162. &texture, &transparency);
  163. triangles.push_back(
  164. new TexturedTriangle(vertexIndices, st, texture, transparency));
  165. triangles.push_back(
  166. new TexturedTriangle(vertexIndices + 3, st + 6, texture, transparency));
  167. }
  168. // Coloured Rectangles
  169. count = tr.getMeshColoredRectangleCount(index);
  170. for (int i = 0; i < count; i++) {
  171. tr.getMeshColoredRectangle(index, i,
  172. vertexIndices, color);
  173. st[0] = color[0];
  174. st[1] = color[1];
  175. st[2] = color[2];
  176. st[3] = color[3];
  177. st[4] = 1.0;
  178. st[5] = 1.0;
  179. #ifdef EXPERIMENTAL
  180. texture = setupTextureColor(color);
  181. #else
  182. texture = 0; // White texture
  183. #endif
  184. transparency = 0;
  185. triangles.push_back(
  186. new TexturedTriangle(vertexIndices, st, texture, transparency));
  187. triangles.push_back(
  188. new TexturedTriangle(vertexIndices + 3, st, texture, transparency));
  189. }
  190. id = mesh = -1;
  191. }
  192. StaticMesh::~StaticMesh() {
  193. while (!triangles.empty()) {
  194. delete triangles.back();
  195. triangles.pop_back();
  196. }
  197. delete [] vertices;
  198. delete [] normals;
  199. delete [] colors;
  200. }
  201. void StaticMesh::display() {
  202. if ((id != -1) && (mesh != -1)) {
  203. getWorld().getMesh(mesh).drawSolid();
  204. return;
  205. }
  206. if (!dontshow) {
  207. //! \fixme Duh, vis tests need to be put back
  208. //if (!isVisible(center, radius, bbox))
  209. // return;
  210. //! \fixme 'AMBIENT' -- Mongoose 2002.01.08
  211. glColor3ubv(WHITE);
  212. if (getRender().getMode() == Render::modeWireframe)
  213. glColor3ubv(WHITE);
  214. getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
  215. for (unsigned int i = 0; i < triangles.size(); i++)
  216. triangles.at(i)->display(vertices, colors, normals);
  217. }
  218. }
  219. float StaticMesh::getRadius() {
  220. assert((id != -1) && (mesh != -1));
  221. return radius;
  222. }