StaticModelGroup.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "SceneEvents.h"
  28. #include "StaticModelGroup.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. extern const char* GEOMETRY_CATEGORY;
  33. StaticModelGroup::StaticModelGroup(Context* context) :
  34. StaticModel(context),
  35. transformsDirty_(true)
  36. {
  37. }
  38. StaticModelGroup::~StaticModelGroup()
  39. {
  40. }
  41. void StaticModelGroup::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<StaticModelGroup>(GEOMETRY_CATEGORY);
  44. COPY_BASE_ATTRIBUTES(StaticModelGroup, StaticModel);
  45. /// \todo Define an attribute for instance node ID's
  46. }
  47. void StaticModelGroup::UpdateBatches(const FrameInfo& frame)
  48. {
  49. // Getting the world bounding box ensures the transforms are updated
  50. const BoundingBox& worldBoundingBox = GetWorldBoundingBox();
  51. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  52. distance_ = frame.camera_->GetDistance(worldBoundingBox.Center());
  53. if (batches_.Size() > 1)
  54. {
  55. for (unsigned i = 0; i < batches_.Size(); ++i)
  56. {
  57. batches_[i].distance_ = frame.camera_->GetDistance(worldTransform * geometryData_[i].center_);
  58. batches_[i].worldTransform_ = worldTransforms_.Size() ? &worldTransforms_[0] : &Matrix3x4::IDENTITY;
  59. batches_[i].numWorldTransforms_ = worldTransforms_.Size();
  60. }
  61. }
  62. else if (batches_.Size() == 1)
  63. {
  64. batches_[0].distance_ = distance_;
  65. batches_[0].worldTransform_ = worldTransforms_.Size() ? &worldTransforms_[0] : &Matrix3x4::IDENTITY;
  66. batches_[0].numWorldTransforms_ = worldTransforms_.Size();
  67. }
  68. float scale = worldBoundingBox.Size().DotProduct(DOT_SCALE);
  69. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  70. if (newLodDistance != lodDistance_)
  71. {
  72. lodDistance_ = newLodDistance;
  73. CalculateLodLevels();
  74. }
  75. }
  76. void StaticModelGroup::AddInstanceNode(Node* node)
  77. {
  78. if (!node)
  79. return;
  80. WeakPtr<Node> instanceWeak(node);
  81. if (instanceNodes_.Contains(instanceWeak))
  82. return;
  83. // Add as a transform listener, so that we know to dirty the instance transforms
  84. node->AddListener(this);
  85. instanceNodes_.Push(instanceWeak);
  86. }
  87. void StaticModelGroup::RemoveInstanceNode(Node* node)
  88. {
  89. if (!node)
  90. return;
  91. WeakPtr<Node> instanceWeak(node);
  92. node->RemoveListener(this);
  93. instanceNodes_.Remove(instanceWeak);
  94. }
  95. Node* StaticModelGroup::GetInstanceNode(unsigned index) const
  96. {
  97. return index < instanceNodes_.Size() ? instanceNodes_[index] : (Node*)0;
  98. }
  99. void StaticModelGroup::OnNodeSet(Node* node)
  100. {
  101. Drawable::OnNodeSet(node);
  102. if (node)
  103. {
  104. Scene* scene = GetScene();
  105. /// \todo It is non-optimal to listen to enabled/disabled change events from the whole scene. The nodes themselves should send an event
  106. if (scene)
  107. SubscribeToEvent(scene, E_NODEENABLEDCHANGED, HANDLER(StaticModelGroup, HandleNodeEnabledChanged));
  108. }
  109. }
  110. void StaticModelGroup::OnMarkedDirty(Node* node)
  111. {
  112. Drawable::OnMarkedDirty(node);
  113. transformsDirty_ = true;
  114. }
  115. void StaticModelGroup::OnWorldBoundingBoxUpdate()
  116. {
  117. if (transformsDirty_)
  118. UpdateTransforms();
  119. worldBoundingBox_.defined_ = false;
  120. for (unsigned i = 0; i < worldTransforms_.Size(); ++i)
  121. worldBoundingBox_.Merge(boundingBox_.Transformed(worldTransforms_[i]));
  122. }
  123. void StaticModelGroup::UpdateTransforms()
  124. {
  125. worldTransforms_.Clear();
  126. for (unsigned i = 0; i < instanceNodes_.Size(); ++i)
  127. {
  128. Node* instanceNode = instanceNodes_[i];
  129. if (!instanceNode || !instanceNode->IsEnabled())
  130. continue;
  131. worldTransforms_.Push(instanceNode->GetWorldTransform());
  132. }
  133. transformsDirty_ = false;
  134. }
  135. void StaticModelGroup::HandleNodeEnabledChanged(StringHash eventType, VariantMap& eventData)
  136. {
  137. using namespace NodeEnabledChanged;
  138. WeakPtr<Node> instanceWeak((Node*)eventData[P_NODE].GetPtr());
  139. if (instanceNodes_.Contains(instanceWeak))
  140. OnMarkedDirty(instanceWeak);
  141. }
  142. }