Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Mesh.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*!
  2. * \file src/Mesh.cpp
  3. * \brief OpenGL Mesh
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "TextureManager.h"
  9. #include "Mesh.h"
  10. Mesh::Mesh(const std::vector<glm::vec3>& vert,
  11. const std::vector<IndexedRectangle>& rect,
  12. const std::vector<IndexedRectangle>& tri,
  13. const std::vector<IndexedColoredRectangle>& coloredRect,
  14. const std::vector<IndexedColoredRectangle>& coloredTri) {
  15. for (auto& t : rect) {
  16. indicesBuff.push_back(0);
  17. verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  18. verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  19. verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  20. verticesBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
  21. texturesBuff.push_back(t.texture);
  22. }
  23. for (auto& t : tri) {
  24. indicesBuff.push_back(1);
  25. verticesBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  26. verticesBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  27. verticesBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  28. texturesBuff.push_back(t.texture);
  29. }
  30. for (auto& t : coloredRect) {
  31. indicesColorBuff.push_back(0);
  32. verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  33. verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  34. verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  35. verticesColorBuff.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
  36. colorsBuff.push_back(glm::vec3(t.r, t.g, t.b));
  37. }
  38. for (auto& t : coloredTri) {
  39. indicesColorBuff.push_back(1);
  40. verticesColorBuff.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
  41. verticesColorBuff.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
  42. verticesColorBuff.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
  43. colorsBuff.push_back(glm::vec3(t.r, t.g, t.b));
  44. }
  45. }
  46. void Mesh::prepare() {
  47. std::vector<unsigned short> ind;
  48. std::vector<glm::vec3> vert;
  49. std::vector<glm::vec2> uvBuff;
  50. std::vector<unsigned int> tex;
  51. int vertIndex = 0;
  52. for (int i = 0; i < indicesBuff.size(); i++) {
  53. unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
  54. for (int x = 0; x < ((indicesBuff.at(i) == 0) ? 4 : 3); x++) {
  55. int v = x;
  56. if (v == 0)
  57. v = 2;
  58. else if (v == 2)
  59. v = 0;
  60. ind.push_back(vert.size());
  61. vert.push_back(verticesBuff.at(vertIndex + v));
  62. uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
  63. tex.push_back(texture);
  64. }
  65. if (indicesBuff.at(i) == 0) {
  66. ind.push_back(ind.at(ind.size() - 4));
  67. ind.push_back(ind.at(ind.size() - 3));
  68. }
  69. vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
  70. }
  71. assert((ind.size() % 3) == 0);
  72. assert(vert.size() == tex.size());
  73. assert(vert.size() == uvBuff.size());
  74. indicesBuff = std::move(ind);
  75. vertices.bufferData(vert);
  76. uvs.bufferData(uvBuff);
  77. texturesBuff = std::move(tex);
  78. std::vector<unsigned short> indCol;
  79. std::vector<glm::vec3> vertCol;
  80. std::vector<glm::vec3> cols;
  81. vertIndex = 0;
  82. for (int i = 0; i < indicesColorBuff.size(); i++) {
  83. for (int x = 0; x < ((indicesColorBuff.at(i) == 0) ? 4 : 3); x++) {
  84. int v = x;
  85. if (v == 0)
  86. v = 2;
  87. else if (v == 2)
  88. v = 0;
  89. indCol.push_back(vertCol.size());
  90. vertCol.push_back(verticesColorBuff.at(vertIndex + v));
  91. cols.push_back(colorsBuff.at(i));
  92. }
  93. if (indicesColorBuff.at(i) == 0) {
  94. indCol.push_back(indCol.at(indCol.size() - 4));
  95. indCol.push_back(indCol.at(indCol.size() - 3));
  96. }
  97. vertIndex += (indicesColorBuff.at(i) == 0) ? 4 : 3;
  98. }
  99. assert((indCol.size() % 3) == 0);
  100. assert(vertCol.size() == cols.size());
  101. indicesColor.bufferData(indCol);
  102. verticesColor.bufferData(vertCol);
  103. colors.bufferData(cols);
  104. verticesBuff.clear();
  105. indicesColorBuff.clear();
  106. verticesColorBuff.clear();
  107. colorsBuff.clear();
  108. }
  109. void Mesh::display(glm::mat4 MVP) {
  110. if (indicesBuff.size() > 0) {
  111. unsigned int indexStart = 0;
  112. unsigned int indexPos = 1;
  113. unsigned int texture = texturesBuff.at(indicesBuff.at(0));
  114. while ((indexStart != indexPos) && (indexPos < indicesBuff.size())) {
  115. while ((indexPos < indicesBuff.size())
  116. && (texturesBuff.at(indicesBuff.at(indexPos)) == texture)) {
  117. indexPos++;
  118. }
  119. std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
  120. indicesBuff.begin() + indexPos);
  121. indices.bufferData(ind);
  122. Shader::drawGL(vertices, uvs, indices, texture, MVP);
  123. if (indexPos < indicesBuff.size()) {
  124. indexStart = indexPos;
  125. indexPos += 1;
  126. texture = texturesBuff.at(indicesBuff.at(indexStart));
  127. }
  128. }
  129. }
  130. if (indicesColor.getSize() > 0)
  131. Shader::drawGL(verticesColor, colors, indicesColor, MVP);
  132. }