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

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