TerrainPatch.cpp 8.8 KB

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