StaticModelGroup.cpp 4.8 KB

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