StaticModelGroup.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Graphics/StaticModel.h"
  24. namespace Atomic
  25. {
  26. /// Renders several object instances while culling and receiving light as one unit. Can be used as a CPU-side optimization, but note that also regular StaticModels will use instanced rendering if possible.
  27. class ATOMIC_API StaticModelGroup : public StaticModel
  28. {
  29. ATOMIC_OBJECT(StaticModelGroup, StaticModel);
  30. public:
  31. /// Construct.
  32. StaticModelGroup(Context* context);
  33. /// Destruct.
  34. virtual ~StaticModelGroup();
  35. /// Register object factory. StaticModel must be registered first.
  36. static void RegisterObject(Context* context);
  37. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  38. void ApplyAttributes();
  39. /// Process octree raycast. May be called from a worker thread.
  40. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  41. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  42. virtual void UpdateBatches(const FrameInfo& frame);
  43. /// Return number of occlusion geometry triangles.
  44. virtual unsigned GetNumOccluderTriangles();
  45. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  46. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  47. /// Add an instance scene node. It does not need any drawable components of its own.
  48. void AddInstanceNode(Node* node);
  49. /// Remove an instance scene node.
  50. void RemoveInstanceNode(Node* node);
  51. /// Remove all instance scene nodes.
  52. void RemoveAllInstanceNodes();
  53. /// Return number of instance nodes.
  54. unsigned GetNumInstanceNodes() const { return instanceNodes_.Size(); }
  55. /// Return instance node by index.
  56. Node* GetInstanceNode(unsigned index) const;
  57. /// Set node IDs attribute.
  58. void SetNodeIDsAttr(const VariantVector& value);
  59. /// Return node IDs attribute.
  60. const VariantVector& GetNodeIDsAttr() const;
  61. protected:
  62. /// Handle scene node enabled status changing.
  63. virtual void OnNodeSetEnabled(Node* node);
  64. /// Recalculate the world-space bounding box.
  65. virtual void OnWorldBoundingBoxUpdate();
  66. private:
  67. /// Ensure proper size of world transforms when nodes are added/removed. Also mark node IDs dirty.
  68. void UpdateNumTransforms();
  69. /// Update node IDs attribute from the actual nodes.
  70. void UpdateNodeIDs() const;
  71. /// Instance nodes.
  72. Vector<WeakPtr<Node> > instanceNodes_;
  73. /// World transforms of valid (existing and visible) instances.
  74. PODVector<Matrix3x4> worldTransforms_;
  75. /// IDs of instance nodes for serialization.
  76. mutable VariantVector nodeIDsAttr_;
  77. /// Number of valid instance node transforms.
  78. unsigned numWorldTransforms_;
  79. /// Whether node IDs have been set and nodes should be searched for during ApplyAttributes.
  80. mutable bool nodesDirty_;
  81. /// Whether nodes have been manipulated by the API and node ID attribute should be refreshed.
  82. mutable bool nodeIDsDirty_;
  83. };
  84. }