TerrainPatch.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "Terrain.h"
  30. #include "TerrainPatch.h"
  31. #include "VertexBuffer.h"
  32. #include "DebugNew.h"
  33. static const Vector3 DOT_SCALE(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  34. static const float LOD_CONSTANT = 1.0f;
  35. OBJECTTYPESTATIC(TerrainPatch);
  36. TerrainPatch::TerrainPatch(Context* context) :
  37. Drawable(context),
  38. geometry_(new Geometry(context)),
  39. vertexBuffer_(new VertexBuffer(context)),
  40. owner_(0),
  41. north_(0),
  42. south_(0),
  43. west_(0),
  44. east_(0),
  45. lodLevel_(0),
  46. lodDirty_(true)
  47. {
  48. drawableFlags_ = DRAWABLE_GEOMETRY;
  49. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_NORMAL | MASK_TEXCOORD1 | MASK_TANGENT);
  50. batches_.Resize(1);
  51. batches_[0].geometry_ = geometry_;
  52. batches_[0].geometryType_ = GEOM_STATIC_NOINSTANCING;
  53. }
  54. TerrainPatch::~TerrainPatch()
  55. {
  56. }
  57. void TerrainPatch::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  58. {
  59. /// \todo Implement
  60. }
  61. void TerrainPatch::UpdateBatches(const FrameInfo& frame)
  62. {
  63. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  64. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  65. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  66. lodDistance_ = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  67. batches_[0].distance_ = distance_;
  68. batches_[0].worldTransform_ = &worldTransform;
  69. unsigned newLodLevel = 0;
  70. for (unsigned i = 0; i < lodErrors_.Size(); ++i)
  71. {
  72. if (lodErrors_[i] / lodDistance_ > LOD_CONSTANT)
  73. break;
  74. else
  75. newLodLevel = i;
  76. }
  77. if (newLodLevel != lodLevel_)
  78. {
  79. lodLevel_ = newLodLevel;
  80. lodDirty_ = true;
  81. }
  82. }
  83. void TerrainPatch::UpdateGeometry(const FrameInfo& frame)
  84. {
  85. if (vertexBuffer_->IsDataLost())
  86. owner_->UpdatePatchGeometry(this);
  87. unsigned northLod = north_ ? north_->lodLevel_ : lodLevel_;
  88. unsigned southLod = south_ ? south_->lodLevel_ : lodLevel_;
  89. unsigned westLod = west_ ? west_->lodLevel_ : lodLevel_;
  90. unsigned eastLod = east_ ? east_->lodLevel_ : lodLevel_;
  91. owner_->UpdatePatchLOD(this, lodLevel_, northLod, southLod, westLod, eastLod);
  92. lodDirty_ = false;
  93. }
  94. UpdateGeometryType TerrainPatch::GetUpdateGeometryType()
  95. {
  96. // If any of the neighbour patches have changed LOD, must update stitching
  97. if (vertexBuffer_->IsDataLost())
  98. return UPDATE_MAIN_THREAD;
  99. else if (lodDirty_ || (north_ && north_->lodDirty_) || (south_ && south_->lodDirty_) || (west_ && west_->lodDirty_) ||
  100. (east_ && east_->lodDirty_))
  101. return UPDATE_WORKER_THREAD;
  102. else
  103. return UPDATE_NONE;
  104. }
  105. void TerrainPatch::OnWorldBoundingBoxUpdate()
  106. {
  107. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  108. }