TerrainPatch.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // Copyright (c) 2008-2015 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 "../Graphics/Camera.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/Geometry.h"
  25. #include "../Graphics/IndexBuffer.h"
  26. #include "../Graphics/Material.h"
  27. #include "../Scene/Node.h"
  28. #include "../Graphics/OcclusionBuffer.h"
  29. #include "../Graphics/OctreeQuery.h"
  30. #include "../Core/Profiler.h"
  31. #include "../Graphics/Terrain.h"
  32. #include "../Graphics/TerrainPatch.h"
  33. #include "../Graphics/VertexBuffer.h"
  34. #include "../DebugNew.h"
  35. namespace Urho3D
  36. {
  37. static const float LOD_CONSTANT = 1.0f / 150.0f;
  38. extern const char* GEOMETRY_CATEGORY;
  39. TerrainPatch::TerrainPatch(Context* context) :
  40. Drawable(context, DRAWABLE_GEOMETRY),
  41. geometry_(new Geometry(context)),
  42. maxLodGeometry_(new Geometry(context)),
  43. minLodGeometry_(new Geometry(context)),
  44. vertexBuffer_(new VertexBuffer(context)),
  45. coordinates_(IntVector2::ZERO),
  46. lodLevel_(0),
  47. occlusionOffset_(0.0f)
  48. {
  49. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  50. maxLodGeometry_->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  51. minLodGeometry_->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  52. batches_.Resize(1);
  53. batches_[0].geometry_ = geometry_;
  54. batches_[0].geometryType_ = GEOM_STATIC_NOINSTANCING;
  55. }
  56. TerrainPatch::~TerrainPatch()
  57. {
  58. }
  59. void TerrainPatch::RegisterObject(Context* context)
  60. {
  61. context->RegisterFactory<TerrainPatch>();
  62. }
  63. void TerrainPatch::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  64. {
  65. RayQueryLevel level = query.level_;
  66. switch (level)
  67. {
  68. case RAY_AABB:
  69. Drawable::ProcessRayQuery(query, results);
  70. break;
  71. case RAY_OBB:
  72. case RAY_TRIANGLE:
  73. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  74. Ray localRay = query.ray_.Transformed(inverse);
  75. float distance = localRay.HitDistance(boundingBox_);
  76. Vector3 normal = -query.ray_.direction_;
  77. if (level == RAY_TRIANGLE && distance < query.maxDistance_)
  78. {
  79. Vector3 geometryNormal;
  80. distance = geometry_->GetHitDistance(localRay, &geometryNormal);
  81. normal = (node_->GetWorldTransform() * Vector4(geometryNormal, 0.0f)).Normalized();
  82. }
  83. if (distance < query.maxDistance_)
  84. {
  85. RayQueryResult result;
  86. result.position_ = query.ray_.origin_ + distance * query.ray_.direction_;
  87. result.normal_ = normal;
  88. result.distance_ = distance;
  89. result.drawable_ = this;
  90. result.node_ = node_;
  91. result.subObject_ = M_MAX_UNSIGNED;
  92. results.Push(result);
  93. }
  94. break;
  95. }
  96. }
  97. void TerrainPatch::UpdateBatches(const FrameInfo& frame)
  98. {
  99. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  100. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  101. float scale = worldTransform.Scale().DotProduct(DOT_SCALE);
  102. lodDistance_ = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  103. batches_[0].distance_ = distance_;
  104. batches_[0].worldTransform_ = &worldTransform;
  105. unsigned newLodLevel = 0;
  106. for (unsigned i = 0; i < lodErrors_.Size(); ++i)
  107. {
  108. if (lodErrors_[i] / lodDistance_ > LOD_CONSTANT)
  109. break;
  110. else
  111. newLodLevel = i;
  112. }
  113. lodLevel_ = GetCorrectedLodLevel(newLodLevel);
  114. }
  115. void TerrainPatch::UpdateGeometry(const FrameInfo& frame)
  116. {
  117. if (vertexBuffer_->IsDataLost())
  118. {
  119. if (owner_)
  120. owner_->CreatePatchGeometry(this);
  121. else
  122. vertexBuffer_->ClearDataLost();
  123. }
  124. if (owner_)
  125. owner_->UpdatePatchLod(this);
  126. }
  127. UpdateGeometryType TerrainPatch::GetUpdateGeometryType()
  128. {
  129. // Because there is a latency in starting worker thread updates, and the update of terrain patch LOD should not take
  130. // much time, always update in the main thread
  131. return UPDATE_MAIN_THREAD;
  132. }
  133. Geometry* TerrainPatch::GetLodGeometry(unsigned batchIndex, unsigned level)
  134. {
  135. if (!level)
  136. return maxLodGeometry_;
  137. else
  138. return geometry_;
  139. }
  140. unsigned TerrainPatch::GetNumOccluderTriangles()
  141. {
  142. // Check that the material is suitable for occlusion (default material always is)
  143. Material* mat = batches_[0].material_;
  144. if (mat && !mat->GetOcclusion())
  145. return 0;
  146. else
  147. return minLodGeometry_->GetIndexCount() / 3;
  148. }
  149. bool TerrainPatch::DrawOcclusion(OcclusionBuffer* buffer)
  150. {
  151. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  152. Material* material = batches_[0].material_;
  153. if (material)
  154. {
  155. if (!material->GetOcclusion())
  156. return true;
  157. buffer->SetCullMode(material->GetCullMode());
  158. }
  159. else
  160. buffer->SetCullMode(CULL_CCW);
  161. const unsigned char* vertexData;
  162. unsigned vertexSize;
  163. const unsigned char* indexData;
  164. unsigned indexSize;
  165. unsigned elementMask;
  166. minLodGeometry_->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  167. // Check for valid geometry data
  168. if (!vertexData || !indexData)
  169. return true;
  170. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  171. Matrix3x4 occlusionTransform(worldTransform.Translation() + worldTransform * Vector4(0.0f, occlusionOffset_, 0.0f,
  172. 0.0f), worldTransform.Rotation(), worldTransform.Scale());
  173. // Draw and check for running out of triangles
  174. return buffer->Draw(occlusionTransform, vertexData, vertexSize, indexData, indexSize, minLodGeometry_->GetIndexStart(),
  175. minLodGeometry_->GetIndexCount());
  176. }
  177. void TerrainPatch::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  178. {
  179. // Intentionally no action
  180. }
  181. void TerrainPatch::SetOwner(Terrain* terrain)
  182. {
  183. owner_ = terrain;
  184. }
  185. void TerrainPatch::SetNeighbors(TerrainPatch* north, TerrainPatch* south, TerrainPatch* west, TerrainPatch* east)
  186. {
  187. north_ = north;
  188. south_ = south;
  189. west_ = west;
  190. east_ = east;
  191. }
  192. void TerrainPatch::SetMaterial(Material* material)
  193. {
  194. batches_[0].material_ = material;
  195. }
  196. void TerrainPatch::SetBoundingBox(const BoundingBox& box)
  197. {
  198. boundingBox_ = box;
  199. OnMarkedDirty(node_);
  200. }
  201. void TerrainPatch::SetCoordinates(const IntVector2& coordinates)
  202. {
  203. coordinates_ = coordinates;
  204. }
  205. void TerrainPatch::SetOcclusionOffset(float offset)
  206. {
  207. occlusionOffset_ = offset;
  208. }
  209. void TerrainPatch::ResetLod()
  210. {
  211. lodLevel_ = 0;
  212. }
  213. Geometry* TerrainPatch::GetGeometry() const
  214. {
  215. return geometry_;
  216. }
  217. Geometry* TerrainPatch::GetMaxLodGeometry() const
  218. {
  219. return maxLodGeometry_;
  220. }
  221. Geometry* TerrainPatch::GetMinLodGeometry() const
  222. {
  223. return minLodGeometry_;
  224. }
  225. VertexBuffer* TerrainPatch::GetVertexBuffer() const
  226. {
  227. return vertexBuffer_;
  228. }
  229. Terrain* TerrainPatch::GetOwner() const
  230. {
  231. return owner_;
  232. }
  233. void TerrainPatch::OnWorldBoundingBoxUpdate()
  234. {
  235. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  236. }
  237. unsigned TerrainPatch::GetCorrectedLodLevel(unsigned lodLevel)
  238. {
  239. if (north_)
  240. lodLevel = Min((int)lodLevel, north_->GetLodLevel() + 1);
  241. if (south_)
  242. lodLevel = Min((int)lodLevel, south_->GetLodLevel() + 1);
  243. if (west_)
  244. lodLevel = Min((int)lodLevel, west_->GetLodLevel() + 1);
  245. if (east_)
  246. lodLevel = Min((int)lodLevel, east_->GetLodLevel() + 1);
  247. return lodLevel;
  248. }
  249. }