TerrainPatch.cpp 9.1 KB

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