StaticModelGroup.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "Batch.h"
  24. #include "Camera.h"
  25. #include "Context.h"
  26. #include "Scene.h"
  27. #include "StaticModelGroup.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. extern const char* GEOMETRY_CATEGORY;
  32. StaticModelGroup::StaticModelGroup(Context* context) :
  33. StaticModel(context)
  34. {
  35. }
  36. StaticModelGroup::~StaticModelGroup()
  37. {
  38. }
  39. void StaticModelGroup::RegisterObject(Context* context)
  40. {
  41. context->RegisterFactory<StaticModelGroup>(GEOMETRY_CATEGORY);
  42. COPY_BASE_ATTRIBUTES(StaticModelGroup, StaticModel);
  43. /// \todo Define an attribute for instance node ID's
  44. }
  45. void StaticModelGroup::UpdateBatches(const FrameInfo& frame)
  46. {
  47. // Getting the world bounding box ensures the transforms are updated
  48. const BoundingBox& worldBoundingBox = GetWorldBoundingBox();
  49. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  50. distance_ = frame.camera_->GetDistance(worldBoundingBox.Center());
  51. if (batches_.Size() > 1)
  52. {
  53. for (unsigned i = 0; i < batches_.Size(); ++i)
  54. {
  55. batches_[i].distance_ = frame.camera_->GetDistance(worldTransform * geometryData_[i].center_);
  56. batches_[i].worldTransform_ = worldTransforms_.Size() ? &worldTransforms_[0] : &Matrix3x4::IDENTITY;
  57. batches_[i].numWorldTransforms_ = worldTransforms_.Size();
  58. }
  59. }
  60. else if (batches_.Size() == 1)
  61. {
  62. batches_[0].distance_ = distance_;
  63. batches_[0].worldTransform_ = worldTransforms_.Size() ? &worldTransforms_[0] : &Matrix3x4::IDENTITY;
  64. batches_[0].numWorldTransforms_ = worldTransforms_.Size();
  65. }
  66. float scale = worldBoundingBox.Size().DotProduct(DOT_SCALE);
  67. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  68. if (newLodDistance != lodDistance_)
  69. {
  70. lodDistance_ = newLodDistance;
  71. CalculateLodLevels();
  72. }
  73. }
  74. void StaticModelGroup::AddInstanceNode(Node* node)
  75. {
  76. if (!node)
  77. return;
  78. WeakPtr<Node> instanceWeak(node);
  79. if (instanceNodes_.Contains(instanceWeak))
  80. return;
  81. // Add as a listener for the instance node, so that we know to dirty the transforms when the node moves or is enabled/disabled
  82. node->AddListener(this);
  83. instanceNodes_.Push(instanceWeak);
  84. }
  85. void StaticModelGroup::RemoveInstanceNode(Node* node)
  86. {
  87. if (!node)
  88. return;
  89. WeakPtr<Node> instanceWeak(node);
  90. node->RemoveListener(this);
  91. instanceNodes_.Remove(instanceWeak);
  92. }
  93. Node* StaticModelGroup::GetInstanceNode(unsigned index) const
  94. {
  95. return index < instanceNodes_.Size() ? instanceNodes_[index] : (Node*)0;
  96. }
  97. void StaticModelGroup::OnNodeSetEnabled(Node* node)
  98. {
  99. Drawable::OnMarkedDirty(node);
  100. }
  101. void StaticModelGroup::OnWorldBoundingBoxUpdate()
  102. {
  103. // Update transforms and bounding box at the same time to have to go through the objects only once
  104. worldBoundingBox_.defined_ = false;
  105. worldTransforms_.Resize(instanceNodes_.Size());
  106. unsigned index = 0;
  107. for (unsigned i = 0; i < instanceNodes_.Size(); ++i)
  108. {
  109. Node* node = instanceNodes_[i];
  110. if (!node || !node->IsEnabled())
  111. continue;
  112. const Matrix3x4& worldTransform = node->GetWorldTransform();
  113. worldTransforms_[index++] = worldTransform;
  114. worldBoundingBox_.Merge(boundingBox_.Transformed(worldTransform));
  115. }
  116. worldTransforms_.Resize(index);
  117. }
  118. }