Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

MeshRenderData.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "graphics/MeshRenderData.h"
  19. #include "graphics/Renderer.h"
  20. #include "graphics/TextureRenderData.h"
  21. #include "resource/Environment.h"
  22. #include "resource/ImgColor.h"
  23. #include "resource/ImgTex.h"
  24. #include "resource/Model.h"
  25. #include "resource/Surface.h"
  26. #include "Config.h"
  27. #include "Core.h"
  28. #include "ResourceCache.h"
  29. #include "Structure.h"
  30. #include <algorithm>
  31. // FIXME Not the neatest thing in the world
  32. // Ought to find an existing 0x08 file with a nice transparent surface
  33. static weak_ptr<const Resource> g_hitSurface;
  34. struct SortByTexSurface
  35. {
  36. bool operator()(const TriangleFan* a, const TriangleFan* b)
  37. {
  38. return a->surfaceIndex < b->surfaceIndex;
  39. }
  40. };
  41. MeshRenderData::MeshRenderData(const Model& model)
  42. {
  43. init(model.surfaces,
  44. model.vertices,
  45. model.triangleFans,
  46. model.hitTriangleFans);
  47. }
  48. MeshRenderData::MeshRenderData(const Structure& structure)
  49. {
  50. assert(structure.partNum() < structure.environment().parts.size());
  51. const CellStruct& part = structure.environment().parts[structure.partNum()];
  52. init(structure.surfaces(),
  53. part.vertices,
  54. part.triangleFans,
  55. part.hitTriangleFans);
  56. }
  57. MeshRenderData::~MeshRenderData()
  58. {
  59. glDeleteVertexArrays(1, &vertexArray_);
  60. glDeleteBuffers(1, &vertexBuffer_);
  61. glDeleteBuffers(1, &indexBuffer_);
  62. }
  63. void MeshRenderData::render()
  64. {
  65. glBindVertexArray(vertexArray_);
  66. int indexBase = 0;
  67. for(Batch& batch : batches_)
  68. {
  69. const ImgColor& imgColor = batch
  70. .surface->cast<Surface>()
  71. .imgTex->cast<ImgTex>()
  72. .imgColor->cast<ImgColor>();
  73. if(!imgColor.renderData)
  74. {
  75. imgColor.renderData.reset(new TextureRenderData{imgColor});
  76. }
  77. TextureRenderData& renderData = static_cast<TextureRenderData&>(*imgColor.renderData);
  78. glActiveTexture(GL_TEXTURE0);
  79. renderData.bind();
  80. glDrawElements(GL_TRIANGLE_FAN, batch.indexCount, GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(indexBase * sizeof(uint16_t)));
  81. indexBase += batch.indexCount;
  82. }
  83. }
  84. void MeshRenderData::init(
  85. const vector<ResourcePtr>& surfaces,
  86. const vector<Vertex>& vertices,
  87. const vector<TriangleFan>& triangleFans,
  88. const vector<TriangleFan>& hitTriangleFans)
  89. {
  90. // vx, vy, vz, nx, ny, nz, s, t
  91. static const int kComponentsPerVertex = 8;
  92. // Sort triangle fans by texture
  93. vector<const TriangleFan*> sortedTriangleFans;
  94. for(const TriangleFan& triangleFan : triangleFans)
  95. {
  96. sortedTriangleFans.push_back(&triangleFan);
  97. }
  98. sort(sortedTriangleFans.begin(), sortedTriangleFans.end(), SortByTexSurface());
  99. // Build batches
  100. vector<float> vertexData;
  101. vector<uint16_t> indexData;
  102. for(const TriangleFan* triangleFan : sortedTriangleFans)
  103. {
  104. // Skip portal/lighting polygons
  105. if(triangleFan->stipplingType == 0x04)
  106. {
  107. continue;
  108. }
  109. if(batches_.empty() || surfaces[triangleFan->surfaceIndex].get() != batches_.back().surface.get())
  110. {
  111. // Start a new batch
  112. batches_.push_back({surfaces[triangleFan->surfaceIndex], 0});
  113. }
  114. else if(batches_.back().indexCount != 0)
  115. {
  116. // Starting a new triangle fan in existing batch
  117. indexData.push_back(0xFFFF);
  118. batches_.back().indexCount++;
  119. }
  120. for(const TriangleFan::Index& index : triangleFan->indices)
  121. {
  122. indexData.push_back(static_cast<uint16_t>(vertexData.size() / kComponentsPerVertex));
  123. batches_.back().indexCount++;
  124. const Vertex& vertex = vertices[index.vertexIndex];
  125. vertexData.push_back(static_cast<float>(vertex.position.x));
  126. vertexData.push_back(static_cast<float>(vertex.position.y));
  127. vertexData.push_back(static_cast<float>(vertex.position.z));
  128. vertexData.push_back(static_cast<float>(vertex.normal.x));
  129. vertexData.push_back(static_cast<float>(vertex.normal.y));
  130. vertexData.push_back(static_cast<float>(vertex.normal.z));
  131. if(vertex.texCoords.empty())
  132. {
  133. vertexData.push_back(0.0f);
  134. vertexData.push_back(0.0f);
  135. }
  136. else
  137. {
  138. vertexData.push_back(static_cast<float>(vertex.texCoords[index.texCoordIndex].x));
  139. vertexData.push_back(static_cast<float>(vertex.texCoords[index.texCoordIndex].y));
  140. }
  141. }
  142. }
  143. if(Core::get().renderer().renderHitGeometry())
  144. {
  145. ResourcePtr surface = g_hitSurface.lock();
  146. if(!surface)
  147. {
  148. ResourcePtr imgColor{new ImgColor{0x800000FF}};
  149. ResourcePtr imgTex{new ImgTex{imgColor}};
  150. surface.reset(new Surface{imgTex});
  151. g_hitSurface = surface;
  152. }
  153. batches_.push_back({surface, 0});
  154. for(const TriangleFan& triangleFan : hitTriangleFans)
  155. {
  156. if(batches_.back().indexCount != 0)
  157. {
  158. indexData.push_back(0xFFFF);
  159. batches_.back().indexCount++;
  160. }
  161. for(const TriangleFan::Index& index : triangleFan.indices)
  162. {
  163. indexData.push_back(static_cast<uint16_t>(vertexData.size() / kComponentsPerVertex));
  164. batches_.back().indexCount++;
  165. const Vertex& vertex = vertices[index.vertexIndex];
  166. vertexData.push_back(static_cast<float>(vertex.position.x));
  167. vertexData.push_back(static_cast<float>(vertex.position.y));
  168. vertexData.push_back(static_cast<float>(vertex.position.z));
  169. vertexData.push_back(static_cast<float>(vertex.normal.x));
  170. vertexData.push_back(static_cast<float>(vertex.normal.y));
  171. vertexData.push_back(static_cast<float>(vertex.normal.z));
  172. vertexData.push_back(0.0f);
  173. vertexData.push_back(0.0f);
  174. }
  175. }
  176. }
  177. glGenVertexArrays(1, &vertexArray_);
  178. glBindVertexArray(vertexArray_);
  179. glGenBuffers(1, &vertexBuffer_);
  180. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer_);
  181. glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(float), vertexData.data(), GL_STATIC_DRAW);
  182. glGenBuffers(1, &indexBuffer_);
  183. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer_);
  184. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size() * sizeof(uint16_t), indexData.data(), GL_STATIC_DRAW);
  185. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * kComponentsPerVertex, nullptr);
  186. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * kComponentsPerVertex, reinterpret_cast<GLvoid*>(sizeof(float) * 3));
  187. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(float) * kComponentsPerVertex, reinterpret_cast<GLvoid*>(sizeof(float) * 6));
  188. glEnableVertexAttribArray(0);
  189. glEnableVertexAttribArray(1);
  190. glEnableVertexAttribArray(2);
  191. }