TerrainPatch.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. owner_(0),
  43. north_(0),
  44. south_(0),
  45. west_(0),
  46. east_(0),
  47. lodLevel_(0),
  48. lodDirty_(true)
  49. {
  50. drawableFlags_ = DRAWABLE_GEOMETRY;
  51. geometry_->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::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  60. {
  61. RayQueryLevel level = query.level_;
  62. switch (level)
  63. {
  64. case RAY_AABB_NOSUBOBJECTS:
  65. case RAY_AABB:
  66. Drawable::ProcessRayQuery(query, results);
  67. break;
  68. case RAY_OBB:
  69. {
  70. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  71. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  72. float distance = localRay.HitDistance(boundingBox_);
  73. if (distance <= query.maxDistance_)
  74. {
  75. RayQueryResult result;
  76. result.drawable_ = this;
  77. result.node_ = GetNode();
  78. result.distance_ = distance;
  79. result.subObject_ = M_MAX_UNSIGNED;
  80. results.Push(result);
  81. }
  82. }
  83. break;
  84. case RAY_TRIANGLE:
  85. {
  86. // Do a pretest using the OBB
  87. Matrix3x4 inverse(node_->GetWorldTransform().Inverse());
  88. Ray localRay(inverse * query.ray_.origin_, inverse * Vector4(query.ray_.direction_, 0.0f));
  89. float distance = localRay.HitDistance(boundingBox_);
  90. if (distance <= query.maxDistance_)
  91. {
  92. // Then the actual test using triangle geometry
  93. const void* indexData = geometry_->GetIndexBuffer() ? geometry_->GetIndexBuffer()->GetShadowData() : 0;
  94. if (indexData && cpuVertexData_)
  95. {
  96. distance = localRay.HitDistance(cpuVertexData_.Get(), sizeof(Vector3), indexData, sizeof(unsigned short),
  97. geometry_->GetIndexStart(), geometry_->GetIndexCount());
  98. if (distance <= query.maxDistance_)
  99. {
  100. RayQueryResult result;
  101. result.drawable_ = this;
  102. result.node_ = GetNode();
  103. result.distance_ = distance;
  104. result.subObject_ = M_MAX_UNSIGNED;
  105. results.Push(result);
  106. break;
  107. }
  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 = GetWorldBoundingBox().Size().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)
  126. break;
  127. else
  128. newLodLevel = i;
  129. }
  130. if (newLodLevel != lodLevel_)
  131. {
  132. lodLevel_ = newLodLevel;
  133. lodDirty_ = true;
  134. }
  135. }
  136. void TerrainPatch::UpdateGeometry(const FrameInfo& frame)
  137. {
  138. if (vertexBuffer_->IsDataLost())
  139. owner_->UpdatePatchGeometry(this);
  140. unsigned northLod = north_ ? north_->lodLevel_ : lodLevel_;
  141. unsigned southLod = south_ ? south_->lodLevel_ : lodLevel_;
  142. unsigned westLod = west_ ? west_->lodLevel_ : lodLevel_;
  143. unsigned eastLod = east_ ? east_->lodLevel_ : lodLevel_;
  144. owner_->UpdatePatchLOD(this, lodLevel_, northLod, southLod, westLod, eastLod);
  145. lodDirty_ = false;
  146. }
  147. unsigned TerrainPatch::GetNumOccluderTriangles()
  148. {
  149. // Check that the material is suitable for occlusion (default material always is)
  150. Material* mat = batches_[0].material_;
  151. if (mat && !mat->GetOcclusion())
  152. return 0;
  153. else
  154. return geometry_->GetIndexCount() / 3;
  155. }
  156. bool TerrainPatch::DrawOcclusion(OcclusionBuffer* buffer)
  157. {
  158. bool success = true;
  159. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  160. Material* material = batches_[0].material_;
  161. if (material)
  162. {
  163. if (!material->GetOcclusion())
  164. return success;
  165. buffer->SetCullMode(material->GetCullMode());
  166. }
  167. else
  168. buffer->SetCullMode(CULL_CCW);
  169. const unsigned char* indexData = geometry_->GetIndexBuffer() ? geometry_->GetIndexBuffer()->GetShadowData() : 0;
  170. if (!indexData || !cpuVertexData_)
  171. return success;
  172. // Draw and check for running out of triangles
  173. if (!buffer->Draw(node_->GetWorldTransform(), cpuVertexData_.Get(), sizeof(Vector3), indexData, sizeof(unsigned short),
  174. geometry_->GetIndexStart(), geometry_->GetIndexCount()))
  175. success = false;
  176. return success;
  177. }
  178. UpdateGeometryType TerrainPatch::GetUpdateGeometryType()
  179. {
  180. // If any of the neighbour patches have changed LOD, must update stitching
  181. if (vertexBuffer_->IsDataLost())
  182. return UPDATE_MAIN_THREAD;
  183. else if (lodDirty_ || (north_ && north_->lodDirty_) || (south_ && south_->lodDirty_) || (west_ && west_->lodDirty_) ||
  184. (east_ && east_->lodDirty_))
  185. return UPDATE_WORKER_THREAD;
  186. else
  187. return UPDATE_NONE;
  188. }
  189. void TerrainPatch::OnWorldBoundingBoxUpdate()
  190. {
  191. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  192. }