TerrainPatch.cpp 8.9 KB

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