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.

Mesh.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*!
  2. * \file src/Mesh.cpp
  3. * \brief Textured/Colored Mesh
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "TextureManager.h"
  9. #include "Mesh.h"
  10. #include <glbinding/gl/gl.h>
  11. #include <glm/glm.hpp>
  12. Mesh::Mesh(const std::vector<glm::vec3>& vert,
  13. const std::vector<IndexedRectangle>& rect,
  14. const std::vector<IndexedRectangle>& tri,
  15. const std::vector<IndexedColoredRectangle>& coloredRect,
  16. const std::vector<IndexedColoredRectangle>& coloredTri) {
  17. for (auto& t : rect) {
  18. indicesBuff.push_back(0);
  19. verticesBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
  20. verticesBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
  21. verticesBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
  22. verticesBuff.emplace_back(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z);
  23. texturesBuff.push_back(t.texture);
  24. }
  25. for (auto& t : tri) {
  26. indicesBuff.push_back(1);
  27. verticesBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
  28. verticesBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
  29. verticesBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
  30. texturesBuff.push_back(t.texture);
  31. }
  32. for (auto& t : coloredRect) {
  33. indicesColorBuff.push_back(0);
  34. verticesColorBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
  35. verticesColorBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
  36. verticesColorBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
  37. verticesColorBuff.emplace_back(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z);
  38. colorsIndexBuff.push_back(t.index);
  39. }
  40. for (auto& t : coloredTri) {
  41. indicesColorBuff.push_back(1);
  42. verticesColorBuff.emplace_back(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z);
  43. verticesColorBuff.emplace_back(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z);
  44. verticesColorBuff.emplace_back(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z);
  45. colorsIndexBuff.push_back(t.index);
  46. }
  47. }
  48. void Mesh::prepare() {
  49. std::vector<unsigned short> ind;
  50. std::vector<glm::vec3> vert;
  51. std::vector<glm::vec2> uvBuff;
  52. std::vector<unsigned int> tex;
  53. glm::vec3 average(0.0f, 0.0f, 0.0f);
  54. unsigned long averageCount = 0;
  55. int vertIndex = 0;
  56. for (int i = 0; i < indicesBuff.size(); i++) {
  57. unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
  58. for (int x = 0; x < ((indicesBuff.at(i) == 0) ? 4 : 3); x++) {
  59. int v = x;
  60. if (v == 0)
  61. v = 2;
  62. else if (v == 2)
  63. v = 0;
  64. ind.push_back(static_cast<unsigned short>(vert.size()));
  65. vert.push_back(verticesBuff.at(vertIndex + v));
  66. uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
  67. tex.push_back(texture);
  68. // Calculate quick-n-dirty center point of mesh
  69. average += verticesBuff.at(vertIndex + v);
  70. averageCount++;
  71. }
  72. if (indicesBuff.at(i) == 0) {
  73. ind.push_back(ind.at(ind.size() - 4));
  74. ind.push_back(ind.at(ind.size() - 3));
  75. }
  76. vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
  77. }
  78. orAssertEqual(ind.size() % 3, 0);
  79. orAssertEqual(vert.size(), tex.size());
  80. orAssertEqual(vert.size(), uvBuff.size());
  81. indicesBuff = std::move(ind);
  82. verticesBuff = std::move(vert);
  83. uvsBuff = std::move(uvBuff);
  84. texturesBuff = std::move(tex);
  85. std::vector<unsigned short> indCol;
  86. std::vector<glm::vec3> vertCol;
  87. std::vector<glm::vec3> cols;
  88. vertIndex = 0;
  89. for (int i = 0; i < indicesColorBuff.size(); i++) {
  90. for (int x = 0; x < ((indicesColorBuff.at(i) == 0) ? 4 : 3); x++) {
  91. int v = x;
  92. if (v == 0)
  93. v = 2;
  94. else if (v == 2)
  95. v = 0;
  96. indCol.push_back(static_cast<unsigned short>(vertCol.size()));
  97. vertCol.push_back(verticesColorBuff.at(vertIndex + v));
  98. glm::vec4 c = TextureManager::getPalette(colorsIndexBuff.at(i));
  99. cols.push_back(glm::vec3(c.x, c.y, c.z));
  100. average += verticesColorBuff.at(vertIndex + v);
  101. averageCount++;
  102. }
  103. if (indicesColorBuff.at(i) == 0) {
  104. indCol.push_back(indCol.at(indCol.size() - 4));
  105. indCol.push_back(indCol.at(indCol.size() - 3));
  106. }
  107. vertIndex += (indicesColorBuff.at(i) == 0) ? 4 : 3;
  108. }
  109. orAssertEqual(indCol.size() % 3, 0);
  110. orAssertEqual(vertCol.size(), cols.size());
  111. indicesColorBuff = std::move(indCol);
  112. verticesColorBuff = std::move(vertCol);
  113. colorsBuff = std::move(cols);
  114. glm::vec3 center = average / float(averageCount);
  115. float radius = 0.0f;
  116. for (auto& v : verticesBuff) {
  117. float dist = glm::distance(center, v);
  118. if (dist > radius) radius = dist;
  119. }
  120. for (auto& v : verticesColorBuff) {
  121. float dist = glm::distance(center, v);
  122. if (dist > radius) radius = dist;
  123. }
  124. sphere.setPosition(center);
  125. sphere.setRadius(radius);
  126. }
  127. void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
  128. if (indicesBuff.size() > 0) {
  129. Shader::drawGLBuffer(verticesBuff, uvsBuff);
  130. for (int i = 0; i < TextureManager::numTextures(TextureStorage::GAME); i++) {
  131. std::vector<unsigned short> indices;
  132. for (int n = 0; n < indicesBuff.size(); n++) {
  133. if (texturesBuff.at(indicesBuff.at(n)) == i) {
  134. indices.push_back(indicesBuff.at(n));
  135. }
  136. }
  137. Shader::drawGLOnly(indices, MVP, i, TextureStorage::GAME,
  138. gl::GL_TRIANGLES, shaderTexture);
  139. }
  140. }
  141. if (indicesColorBuff.size() > 0)
  142. Shader::drawGL(verticesColorBuff, colorsBuff, indicesColorBuff, MVP, gl::GL_TRIANGLES,
  143. shaderTexture);
  144. }