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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  31. glBindTexture(GL_TEXTURE_2D, texture + 1);
  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. #ifdef EXPERIMENTAL
  82. #include <map>
  83. std::map<unsigned int, unsigned int> gColorTextureHACK;
  84. int setupTextureColor(float* colorf) {
  85. unsigned char color[4];
  86. unsigned int colorI;
  87. unsigned int texture;
  88. color[0] = (unsigned char)(colorf[0] * 255.0f);
  89. color[1] = (unsigned char)(colorf[1] * 255.0f);
  90. color[2] = (unsigned char)(colorf[2] * 255.0f);
  91. color[3] = (unsigned char)(colorf[3] * 255.0f);
  92. ((unsigned char*)(&colorI))[3] = color[0];
  93. ((unsigned char*)(&colorI))[2] = color[1];
  94. ((unsigned char*)(&colorI))[1] = color[2];
  95. ((unsigned char*)(&colorI))[0] = color[3];
  96. try {
  97. texture = gColorTextureHACK.at(colorI);
  98. } catch (...) {
  99. unsigned char* image = generateColorTexture(color, 32, 32, 32);
  100. texture = getTextureManager().loadBufferSlot(image, 32, 32,
  101. RGBA, 32, getTextureManager().getTextureCount());
  102. delete [] image;
  103. }
  104. return texture;
  105. }
  106. #endif
  107. StaticMesh::StaticMesh(TombRaider& tr, unsigned int index) {
  108. int count, texture;
  109. int vertexIndices[6];
  110. float st[12];
  111. float color[4];
  112. unsigned short transparency;
  113. if (!tr.isMeshValid(index)) {
  114. dontshow = true;
  115. return;
  116. } else {
  117. dontshow = false;
  118. }
  119. // Mongoose 2002.08.30, Testing support for 'shootable' models ( traceable )
  120. tr.getMeshCollisionInfo(index, center, &radius);
  121. //! \fixme Arrays don't work either =)
  122. // Mesh geometery, colors, etc
  123. tr.getMeshVertexArrays(index,
  124. &vertexCount, &vertices,
  125. &normalCount, &normals,
  126. &colorCount, &colors);
  127. // Textured Triangles
  128. count = tr.getMeshTexturedTriangleCount(index);
  129. for (int i = 0; i < count; i++) {
  130. tr.getMeshTexturedTriangle(index, i,
  131. vertexIndices, st,
  132. &texture, &transparency);
  133. triangles.push_back(
  134. new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
  135. }
  136. // Coloured Triangles
  137. count = tr.getMeshColoredTriangleCount(index);
  138. for (int i = 0; i < count; i++) {
  139. tr.getMeshColoredTriangle(index, i,
  140. vertexIndices, color);
  141. st[0] = color[0];
  142. st[1] = color[1];
  143. st[2] = color[2];
  144. st[3] = color[3];
  145. st[4] = 1.0;
  146. st[5] = 1.0;
  147. #ifdef EXPERIMENTAL
  148. texture = setupTextureColor(color);
  149. #else
  150. texture = 0; // White texture
  151. #endif
  152. transparency = 0;
  153. triangles.push_back(
  154. new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
  155. }
  156. // Textured Rectangles
  157. count = tr.getMeshTexturedRectangleCount(index);
  158. for (int i = 0; i < count; i++) {
  159. tr.getMeshTexturedRectangle(index, i,
  160. vertexIndices, st,
  161. &texture, &transparency);
  162. triangles.push_back(
  163. new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
  164. triangles.push_back(
  165. new TexturedTriangle(vertexIndices + 3, st + 6, texture + getGame().getTextureStart(),
  166. 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 + getGame().getTextureStart(), transparency));
  187. triangles.push_back(
  188. new TexturedTriangle(vertexIndices + 3, st, texture + getGame().getTextureStart(), transparency));
  189. }
  190. }
  191. StaticMesh::~StaticMesh() {
  192. while (!triangles.empty()) {
  193. delete triangles.back();
  194. triangles.pop_back();
  195. }
  196. delete [] vertices;
  197. delete [] normals;
  198. delete [] colors;
  199. }
  200. void StaticMesh::display() {
  201. if (!dontshow) {
  202. //! \fixme Duh, vis tests need to be put back
  203. //if (!isVisible(center, radius, bbox))
  204. // return;
  205. //! \fixme 'AMBIENT' -- Mongoose 2002.01.08
  206. glColor3ubv(WHITE);
  207. if (getRender().getMode() == Render::modeWireframe)
  208. glColor3ubv(WHITE);
  209. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  210. glBindTexture(GL_TEXTURE_2D, 1); // White texture for colors
  211. for (unsigned int i = 0; i < triangles.size(); i++)
  212. triangles.at(i)->display(vertices, colors, normals);
  213. }
  214. }
  215. float StaticMesh::getRadius() {
  216. return radius;
  217. }