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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "StaticMesh.h"
  11. #ifdef EXPERIMENTAL
  12. std::vector<unsigned int> gColorTextureHACK;
  13. #endif
  14. TexturedTriangle::TexturedTriangle(int i[3], vec_t 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. bool TexturedTriangle::operator< (TexturedTriangle &t) {
  28. return texture < t.texture;
  29. }
  30. void TexturedTriangle::display(vec_t *vertices, vec_t *colors, vec_t *normals) {
  31. assert(vertices != NULL);
  32. if ((getRender().getMode() != Render::modeWireframe)
  33. && (getRender().getMode() != Render::modeSolid)) {
  34. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  35. glBindTexture(GL_TEXTURE_2D, texture + 1);
  36. }
  37. glBegin(GL_TRIANGLES);
  38. switch (getRender().getMode()) {
  39. case Render::modeSolid:
  40. case Render::modeVertexLight:
  41. if (colors != NULL) {
  42. glColor3fv(colors + index[0]);
  43. glTexCoord2fv(st);
  44. glVertex3fv(vertices + (index[0] * 3));
  45. glColor3fv(colors + index[1]);
  46. glTexCoord2fv(st + 2);
  47. glVertex3fv(vertices + (index[1] * 3));
  48. glColor3fv(colors + index[2]);
  49. glTexCoord2fv(st + 4);
  50. glVertex3fv(vertices + (index[2] * 3));
  51. } else if (normals != NULL) {
  52. glNormal3fv(normals + (index[0] * 3));
  53. glTexCoord2fv(st);
  54. glVertex3fv(vertices + (index[0] * 3));
  55. glNormal3fv(normals + (index[1] * 3));
  56. glTexCoord2fv(st + 2);
  57. glVertex3fv(vertices + (index[1] * 3));
  58. glNormal3fv(normals + (index[2] * 3));
  59. glTexCoord2fv(st + 4);
  60. glVertex3fv(vertices + (index[2] * 3));
  61. } else {
  62. glTexCoord2fv(st);
  63. glVertex3fv(vertices + (index[0] * 3));
  64. glTexCoord2fv(st + 2);
  65. glVertex3fv(vertices + (index[1] * 3));
  66. glTexCoord2fv(st + 4);
  67. glVertex3fv(vertices + (index[2] * 3));
  68. }
  69. break;
  70. case Render::modeWireframe:
  71. glVertex3fv(vertices + (index[0] * 3));
  72. glVertex3fv(vertices + (index[1] * 3));
  73. glVertex3fv(vertices + (index[2] * 3));
  74. break;
  75. default:
  76. glTexCoord2fv(st);
  77. glVertex3fv(vertices + (index[0] * 3));
  78. glTexCoord2fv(st + 2);
  79. glVertex3fv(vertices + (index[1] * 3));
  80. glTexCoord2fv(st + 4);
  81. glVertex3fv(vertices + (index[2] * 3));
  82. }
  83. glEnd();
  84. }
  85. #ifdef EXPERIMENTAL
  86. int setupTextureColor(float *colorf) {
  87. unsigned char color[4];
  88. unsigned int colorI;
  89. 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. bool found = false;
  99. unsigned int foundIndex = 0;
  100. for (foundIndex = 0; foundIndex < gColorTextureHACK.size(); foundIndex++) {
  101. if (gColorTextureHACK[foundIndex] == colorI) {
  102. found = true;
  103. break;
  104. }
  105. }
  106. if (!found) {
  107. gColorTextureHACK.push_back(colorI);
  108. texture = getGame().getTextureOffset() + gColorTextureHACK.size();
  109. getRender().loadTexture(Texture::generateColorTexture(color, 32, 32),
  110. 32, 32, texture);
  111. } else {
  112. texture = getGame().getTextureOffset() + foundIndex;
  113. }
  114. return texture;
  115. }
  116. #endif
  117. StaticMesh::StaticMesh(TombRaider &tr, unsigned int index) {
  118. int count, texture;
  119. int vertexIndices[6];
  120. float st[12];
  121. float color[4];
  122. unsigned short transparency;
  123. if (!tr.isMeshValid(index)) {
  124. dontshow = true;
  125. return;
  126. } else {
  127. dontshow = false;
  128. }
  129. // Mongoose 2002.08.30, Testing support for 'shootable' models ( traceable )
  130. tr.getMeshCollisionInfo(index, center, &radius);
  131. //! \fixme Arrays don't work either =)
  132. // Mesh geometery, colors, etc
  133. tr.getMeshVertexArrays(index,
  134. &vertexCount, &vertices,
  135. &normalCount, &normals,
  136. &colorCount, &colors);
  137. // Textured Triangles
  138. count = tr.getMeshTexturedTriangleCount(index);
  139. for (int i = 0; i < count; i++) {
  140. tr.getMeshTexturedTriangle(index, i,
  141. vertexIndices, st,
  142. &texture, &transparency);
  143. texturedTriangles.push_back(
  144. new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
  145. }
  146. // Coloured Triangles
  147. count = tr.getMeshColoredTriangleCount(index);
  148. for (int i = 0; i < count; i++) {
  149. tr.getMeshColoredTriangle(index, i,
  150. vertexIndices, color);
  151. st[0] = color[0];
  152. st[1] = color[1];
  153. st[2] = color[2];
  154. st[3] = color[3];
  155. st[4] = 1.0;
  156. st[5] = 1.0;
  157. #ifdef EXPERIMENTAL
  158. texture = setupTextureColor(color);
  159. #else
  160. texture = 0; // White texture
  161. #endif
  162. transparency = 0;
  163. coloredTriangles.push_back(
  164. new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
  165. }
  166. // Textured Rectangles
  167. count = tr.getMeshTexturedRectangleCount(index);
  168. for (int i = 0; i < count; i++) {
  169. tr.getMeshTexturedRectangle(index, i,
  170. vertexIndices, st,
  171. &texture, &transparency);
  172. texturedRectangles.push_back(
  173. new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
  174. texturedRectangles.push_back(
  175. new TexturedTriangle(vertexIndices + 3, st + 6, texture + getGame().getTextureStart(), transparency));
  176. }
  177. // Coloured Rectangles
  178. count = tr.getMeshColoredRectangleCount(index);
  179. for (int i = 0; i < count; i++) {
  180. tr.getMeshColoredRectangle(index, i,
  181. vertexIndices, color);
  182. st[0] = color[0];
  183. st[1] = color[1];
  184. st[2] = color[2];
  185. st[3] = color[3];
  186. st[4] = 1.0;
  187. st[5] = 1.0;
  188. #ifdef EXPERIMENTAL
  189. texture = setupTextureColor(color);
  190. #else
  191. texture = 0; // White texture
  192. #endif
  193. transparency = 0;
  194. coloredRectangles.push_back(
  195. new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
  196. coloredRectangles.push_back(
  197. new TexturedTriangle(vertexIndices + 3, st, texture + getGame().getTextureStart(), transparency));
  198. }
  199. // Sort faces by texture
  200. std::sort(texturedTriangles.begin(), texturedTriangles.end());
  201. std::sort(coloredTriangles.begin(), coloredTriangles.end());
  202. std::sort(texturedRectangles.begin(), texturedRectangles.end());
  203. std::sort(coloredRectangles.begin(), coloredRectangles.end());
  204. }
  205. StaticMesh::~StaticMesh() {
  206. while (!texturedTriangles.empty()) {
  207. delete texturedTriangles.back();
  208. texturedTriangles.pop_back();
  209. }
  210. while (!coloredTriangles.empty()) {
  211. delete coloredTriangles.back();
  212. coloredTriangles.pop_back();
  213. }
  214. while (!texturedRectangles.empty()) {
  215. delete texturedRectangles.back();
  216. texturedRectangles.pop_back();
  217. }
  218. while (!coloredRectangles.empty()) {
  219. delete coloredRectangles.back();
  220. coloredRectangles.pop_back();
  221. }
  222. delete [] vertices;
  223. delete [] normals;
  224. delete [] colors;
  225. }
  226. void StaticMesh::display() {
  227. if (!dontshow) {
  228. //! \fixme Duh, vis tests need to be put back
  229. //if (!isVisible(center, radius, bbox))
  230. // return;
  231. //! \fixme 'AMBIENT' -- Mongoose 2002.01.08
  232. glColor3fv(WHITE);
  233. if (getRender().getMode() == Render::modeWireframe)
  234. glColor3fv(WHITE);
  235. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  236. glBindTexture(GL_TEXTURE_2D, 1); // White texture for colors
  237. for (unsigned int i = 0; i < coloredTriangles.size(); i++)
  238. coloredTriangles.at(i)->display(vertices, colors, normals);
  239. for (unsigned int i = 0; i < coloredRectangles.size(); i++)
  240. coloredRectangles.at(i)->display(vertices, colors, normals);
  241. for (unsigned int i = 0; i < texturedTriangles.size(); i++)
  242. texturedTriangles.at(i)->display(vertices, colors, normals);
  243. for (unsigned int i = 0; i < texturedRectangles.size(); i++)
  244. texturedRectangles.at(i)->display(vertices, colors, normals);
  245. }
  246. }
  247. vec_t StaticMesh::getRadius() {
  248. return radius;
  249. }