TerrainPatch.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/Camera.h"
  25. #include "../Graphics/DebugRenderer.h"
  26. #include "../Graphics/Geometry.h"
  27. #include "../Graphics/IndexBuffer.h"
  28. #include "../Graphics/Material.h"
  29. #include "../Graphics/OcclusionBuffer.h"
  30. #include "../Graphics/OctreeQuery.h"
  31. #include "../Graphics/Terrain.h"
  32. #include "../Graphics/TerrainPatch.h"
  33. #include "../Graphics/VertexBuffer.h"
  34. #include "../IO/Log.h"
  35. #include "../Scene/Node.h"
  36. #include "../DebugNew.h"
  37. namespace Atomic
  38. {
  39. static const float LOD_CONSTANT = 1.0f / 150.0f;
  40. extern const char* GEOMETRY_CATEGORY;
  41. TerrainPatch::TerrainPatch(Context* context) :
  42. Drawable(context, DRAWABLE_GEOMETRY),
  43. geometry_(new Geometry(context)),
  44. maxLodGeometry_(new Geometry(context)),
  45. occlusionGeometry_(new Geometry(context)),
  46. vertexBuffer_(new VertexBuffer(context)),
  47. coordinates_(IntVector2::ZERO),
  48. lodLevel_(0)
  49. {
  50. geometry_->SetVertexBuffer(0, vertexBuffer_);
  51. maxLodGeometry_->SetVertexBuffer(0, vertexBuffer_);
  52. occlusionGeometry_->SetVertexBuffer(0, vertexBuffer_);
  53. batches_.Resize(1);
  54. batches_[0].geometry_ = geometry_;
  55. batches_[0].geometryType_ = GEOM_STATIC_NOINSTANCING;
  56. }
  57. TerrainPatch::~TerrainPatch()
  58. {
  59. }
  60. void TerrainPatch::RegisterObject(Context* context)
  61. {
  62. context->RegisterFactory<TerrainPatch>();
  63. }
  64. void TerrainPatch::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  65. {
  66. RayQueryLevel level = query.level_;
  67. switch (level)
  68. {
  69. case RAY_AABB:
  70. Drawable::ProcessRayQuery(query, results);
  71. break;
  72. case RAY_OBB:
  73. case RAY_TRIANGLE:
  74. {
  75. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  76. Ray localRay = query.ray_.Transformed(inverse);
  77. float distance = localRay.HitDistance(boundingBox_);
  78. Vector3 normal = -query.ray_.direction_;
  79. if (level == RAY_TRIANGLE && distance < query.maxDistance_)
  80. {
  81. Vector3 geometryNormal;
  82. distance = geometry_->GetHitDistance(localRay, &geometryNormal);
  83. normal = (node_->GetWorldTransform() * Vector4(geometryNormal, 0.0f)).Normalized();
  84. }
  85. if (distance < query.maxDistance_)
  86. {
  87. RayQueryResult result;
  88. result.position_ = query.ray_.origin_ + distance * query.ray_.direction_;
  89. result.normal_ = normal;
  90. result.distance_ = distance;
  91. result.drawable_ = this;
  92. result.node_ = node_;
  93. result.subObject_ = M_MAX_UNSIGNED;
  94. results.Push(result);
  95. }
  96. }
  97. break;
  98. case RAY_TRIANGLE_UV:
  99. ATOMIC_LOGWARNING("RAY_TRIANGLE_UV query level is not supported for TerrainPatch component");
  100. break;
  101. }
  102. }
  103. void TerrainPatch::UpdateBatches(const FrameInfo& frame)
  104. {
  105. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  106. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  107. float scale = worldTransform.Scale().DotProduct(DOT_SCALE);
  108. lodDistance_ = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  109. batches_[0].distance_ = distance_;
  110. batches_[0].worldTransform_ = &worldTransform;
  111. unsigned newLodLevel = 0;
  112. for (unsigned i = 0; i < lodErrors_.Size(); ++i)
  113. {
  114. if (lodErrors_[i] / lodDistance_ > LOD_CONSTANT)
  115. break;
  116. else
  117. newLodLevel = i;
  118. }
  119. lodLevel_ = GetCorrectedLodLevel(newLodLevel);
  120. }
  121. void TerrainPatch::UpdateGeometry(const FrameInfo& frame)
  122. {
  123. if (vertexBuffer_->IsDataLost())
  124. {
  125. if (owner_)
  126. owner_->CreatePatchGeometry(this);
  127. else
  128. vertexBuffer_->ClearDataLost();
  129. }
  130. if (owner_)
  131. owner_->UpdatePatchLod(this);
  132. }
  133. UpdateGeometryType TerrainPatch::GetUpdateGeometryType()
  134. {
  135. // Because there is a latency in starting worker thread updates, and the update of terrain patch LOD should not take
  136. // much time, always update in the main thread
  137. return UPDATE_MAIN_THREAD;
  138. }
  139. Geometry* TerrainPatch::GetLodGeometry(unsigned batchIndex, unsigned level)
  140. {
  141. if (!level)
  142. return maxLodGeometry_;
  143. else
  144. return geometry_;
  145. }
  146. unsigned TerrainPatch::GetNumOccluderTriangles()
  147. {
  148. // Check that the material is suitable for occlusion (default material always is)
  149. Material* mat = batches_[0].material_;
  150. if (mat && !mat->GetOcclusion())
  151. return 0;
  152. else
  153. return occlusionGeometry_->GetIndexCount() / 3;
  154. }
  155. bool TerrainPatch::DrawOcclusion(OcclusionBuffer* buffer)
  156. {
  157. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  158. Material* material = batches_[0].material_;
  159. if (material)
  160. {
  161. if (!material->GetOcclusion())
  162. return true;
  163. buffer->SetCullMode(material->GetCullMode());
  164. }
  165. else
  166. buffer->SetCullMode(CULL_CCW);
  167. const unsigned char* vertexData;
  168. unsigned vertexSize;
  169. const unsigned char* indexData;
  170. unsigned indexSize;
  171. const PODVector<VertexElement>* elements;
  172. occlusionGeometry_->GetRawData(vertexData, vertexSize, indexData, indexSize, elements);
  173. // Check for valid geometry data
  174. if (!vertexData || !indexData || !elements || VertexBuffer::GetElementOffset(*elements, TYPE_VECTOR3, SEM_POSITION) != 0)
  175. return false;
  176. // Draw and check for running out of triangles
  177. return buffer->AddTriangles(node_->GetWorldTransform(), vertexData, vertexSize, indexData, indexSize, occlusionGeometry_->GetIndexStart(),
  178. occlusionGeometry_->GetIndexCount());
  179. }
  180. void TerrainPatch::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  181. {
  182. // Intentionally no operation
  183. }
  184. void TerrainPatch::SetOwner(Terrain* terrain)
  185. {
  186. owner_ = terrain;
  187. }
  188. void TerrainPatch::SetNeighbors(TerrainPatch* north, TerrainPatch* south, TerrainPatch* west, TerrainPatch* east)
  189. {
  190. north_ = north;
  191. south_ = south;
  192. west_ = west;
  193. east_ = east;
  194. }
  195. void TerrainPatch::SetMaterial(Material* material)
  196. {
  197. batches_[0].material_ = material;
  198. }
  199. void TerrainPatch::SetBoundingBox(const BoundingBox& box)
  200. {
  201. boundingBox_ = box;
  202. OnMarkedDirty(node_);
  203. }
  204. void TerrainPatch::SetCoordinates(const IntVector2& coordinates)
  205. {
  206. coordinates_ = coordinates;
  207. }
  208. void TerrainPatch::ResetLod()
  209. {
  210. lodLevel_ = 0;
  211. }
  212. Geometry* TerrainPatch::GetGeometry() const
  213. {
  214. return geometry_;
  215. }
  216. Geometry* TerrainPatch::GetMaxLodGeometry() const
  217. {
  218. return maxLodGeometry_;
  219. }
  220. Geometry* TerrainPatch::GetOcclusionGeometry() const
  221. {
  222. return occlusionGeometry_;
  223. }
  224. VertexBuffer* TerrainPatch::GetVertexBuffer() const
  225. {
  226. return vertexBuffer_;
  227. }
  228. Terrain* TerrainPatch::GetOwner() const
  229. {
  230. return owner_;
  231. }
  232. void TerrainPatch::OnWorldBoundingBoxUpdate()
  233. {
  234. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  235. }
  236. unsigned TerrainPatch::GetCorrectedLodLevel(unsigned lodLevel)
  237. {
  238. if (north_)
  239. lodLevel = Min(lodLevel, north_->GetLodLevel() + 1);
  240. if (south_)
  241. lodLevel = Min(lodLevel, south_->GetLodLevel() + 1);
  242. if (west_)
  243. lodLevel = Min(lodLevel, west_->GetLodLevel() + 1);
  244. if (east_)
  245. lodLevel = Min(lodLevel, east_->GetLodLevel() + 1);
  246. return lodLevel;
  247. }
  248. }