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.3KB

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