TerrainPatch.cpp 9.0 KB

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