TerrainPatch.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "Node.h"
  29. #include "OcclusionBuffer.h"
  30. #include "OctreeQuery.h"
  31. #include "Terrain.h"
  32. #include "TerrainPatch.h"
  33. #include "VertexBuffer.h"
  34. #include "DebugNew.h"
  35. static const Vector3 DOT_SCALE(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  36. static const float LOD_CONSTANT = 1.0f;
  37. OBJECTTYPESTATIC(TerrainPatch);
  38. TerrainPatch::TerrainPatch(Context* context) :
  39. Drawable(context),
  40. geometry_(new Geometry(context)),
  41. vertexBuffer_(new VertexBuffer(context)),
  42. lodLevel_(0),
  43. lodDirty_(true)
  44. {
  45. drawableFlags_ = DRAWABLE_GEOMETRY;
  46. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  47. batches_.Resize(1);
  48. batches_[0].geometry_ = geometry_;
  49. batches_[0].geometryType_ = GEOM_STATIC_NOINSTANCING;
  50. }
  51. TerrainPatch::~TerrainPatch()
  52. {
  53. }
  54. void TerrainPatch::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<TerrainPatch>();
  57. }
  58. void TerrainPatch::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  59. {
  60. RayQueryLevel level = query.level_;
  61. switch (level)
  62. {
  63. case RAY_AABB_NOSUBOBJECTS:
  64. case RAY_AABB:
  65. Drawable::ProcessRayQuery(query, results);
  66. break;
  67. case RAY_OBB:
  68. {
  69. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  70. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  71. float distance = localRay.HitDistance(boundingBox_);
  72. if (distance <= query.maxDistance_)
  73. {
  74. RayQueryResult result;
  75. result.drawable_ = this;
  76. result.node_ = GetNode();
  77. result.distance_ = distance;
  78. result.subObject_ = M_MAX_UNSIGNED;
  79. results.Push(result);
  80. }
  81. }
  82. break;
  83. case RAY_TRIANGLE:
  84. {
  85. // Do a pretest using the OBB
  86. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  87. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  88. float distance = localRay.HitDistance(boundingBox_);
  89. if (distance <= query.maxDistance_)
  90. {
  91. distance = geometry_->GetHitDistance(localRay);
  92. if (distance <= query.maxDistance_)
  93. {
  94. RayQueryResult result;
  95. result.drawable_ = this;
  96. result.node_ = GetNode();
  97. result.distance_ = distance;
  98. result.subObject_ = M_MAX_UNSIGNED;
  99. results.Push(result);
  100. break;
  101. }
  102. }
  103. }
  104. break;
  105. }
  106. }
  107. void TerrainPatch::UpdateBatches(const FrameInfo& frame)
  108. {
  109. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  110. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  111. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  112. lodDistance_ = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  113. batches_[0].distance_ = distance_;
  114. batches_[0].worldTransform_ = &worldTransform;
  115. unsigned newLodLevel = 0;
  116. for (unsigned i = 0; i < lodErrors_.Size(); ++i)
  117. {
  118. if (lodErrors_[i] / lodDistance_ > LOD_CONSTANT)
  119. break;
  120. else
  121. newLodLevel = i;
  122. }
  123. if (newLodLevel != lodLevel_)
  124. {
  125. lodLevel_ = newLodLevel;
  126. lodDirty_ = true;
  127. }
  128. }
  129. void TerrainPatch::UpdateGeometry(const FrameInfo& frame)
  130. {
  131. if (vertexBuffer_->IsDataLost())
  132. {
  133. if (owner_)
  134. owner_->UpdatePatchGeometry(this);
  135. else
  136. vertexBuffer_->ClearDataLost();
  137. }
  138. unsigned northLod = north_ ? north_->lodLevel_ : lodLevel_;
  139. unsigned southLod = south_ ? south_->lodLevel_ : lodLevel_;
  140. unsigned westLod = west_ ? west_->lodLevel_ : lodLevel_;
  141. unsigned eastLod = east_ ? east_->lodLevel_ : lodLevel_;
  142. if (owner_)
  143. owner_->UpdatePatchLOD(this, lodLevel_, northLod, southLod, westLod, eastLod);
  144. lodDirty_ = false;
  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 geometry_->GetIndexCount() / 3;
  154. }
  155. bool TerrainPatch::DrawOcclusion(OcclusionBuffer* buffer)
  156. {
  157. bool success = true;
  158. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  159. Material* material = batches_[0].material_;
  160. if (material)
  161. {
  162. if (!material->GetOcclusion())
  163. return success;
  164. buffer->SetCullMode(material->GetCullMode());
  165. }
  166. else
  167. buffer->SetCullMode(CULL_CCW);
  168. const unsigned char* vertexData;
  169. unsigned vertexSize;
  170. const unsigned char* indexData;
  171. unsigned indexSize;
  172. unsigned elementMask;
  173. geometry_->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  174. // Check for valid geometry data
  175. if (!vertexData || !indexData)
  176. return success;
  177. // Draw and check for running out of triangles
  178. if (!buffer->Draw(node_->GetWorldTransform(), vertexData, vertexSize, indexData, indexSize, geometry_->GetIndexStart(),
  179. geometry_->GetIndexCount()))
  180. success = false;
  181. return success;
  182. }
  183. UpdateGeometryType TerrainPatch::GetUpdateGeometryType()
  184. {
  185. // If any of the neighbour patches have changed LOD, must update stitching
  186. if (vertexBuffer_->IsDataLost())
  187. return UPDATE_MAIN_THREAD;
  188. else if (lodDirty_ || (north_ && north_->lodDirty_) || (south_ && south_->lodDirty_) || (west_ && west_->lodDirty_) ||
  189. (east_ && east_->lodDirty_))
  190. return UPDATE_WORKER_THREAD;
  191. else
  192. return UPDATE_NONE;
  193. }
  194. void TerrainPatch::OnWorldBoundingBoxUpdate()
  195. {
  196. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  197. }