Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
GraphicsComponent.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Development Kit"
3 // For conditions of distribution and use, see copyright notice in Prerequesites.hpp
4 
5 #pragma once
6 
7 #ifndef NDK_SERVER
8 #ifndef NDK_COMPONENTS_GRAPHICSCOMPONENT_HPP
9 #define NDK_COMPONENTS_GRAPHICSCOMPONENT_HPP
10 
11 #include <Nazara/Graphics/CullingList.hpp>
12 #include <Nazara/Graphics/InstancedRenderable.hpp>
13 #include <Nazara/Utility/Node.hpp>
14 #include <NDK/Component.hpp>
15 #include <unordered_map>
16 
17 namespace Ndk
18 {
19  class GraphicsComponent;
20 
21  using GraphicsComponentCullingList = Nz::CullingList<GraphicsComponent>;
22  using GraphicsComponentHandle = Nz::ObjectHandle<GraphicsComponent>;
23 
24  class NDK_API GraphicsComponent : public Component<GraphicsComponent>, public Nz::HandledObject<GraphicsComponent>
25  {
26  friend class RenderSystem;
27 
28  public:
29  using RenderableList = std::vector<Nz::InstancedRenderableRef>;
30 
31  GraphicsComponent() = default;
32  inline GraphicsComponent(const GraphicsComponent& graphicsComponent);
33  ~GraphicsComponent() = default;
34 
35  inline void AddToCullingList(GraphicsComponentCullingList* cullingList) const;
36  void AddToRenderQueue(Nz::AbstractRenderQueue* renderQueue) const;
37 
38  inline void Attach(Nz::InstancedRenderableRef renderable, int renderOrder = 0);
39  void Attach(Nz::InstancedRenderableRef renderable, const Nz::Matrix4f& localMatrix, int renderOrder = 0);
40 
41  inline void Clear();
42 
43  inline void Detach(const Nz::InstancedRenderable* renderable);
44 
45  inline bool DoesRequireRealTimeReflections() const;
46 
47  inline void EnsureBoundingVolumeUpdate() const;
48  inline void EnsureTransformMatrixUpdate() const;
49 
50  inline void GetAttachedRenderables(RenderableList* renderables) const;
51  inline std::size_t GetAttachedRenderableCount() const;
52 
53  inline const Nz::BoundingVolumef& GetBoundingVolume() const;
54 
55  inline void RemoveFromCullingList(GraphicsComponentCullingList* cullingList) const;
56 
57  inline void UpdateLocalMatrix(const Nz::InstancedRenderable* instancedRenderable, const Nz::Matrix4f& localMatrix);
58  inline void UpdateRenderOrder(const Nz::InstancedRenderable* instancedRenderable, int renderOrder);
59 
60  static ComponentIndex componentIndex;
61 
62  private:
63  struct Renderable;
64 
65  void ConnectInstancedRenderableSignals(Renderable& renderable);
66 
67  inline void InvalidateBoundingVolume() const;
68  void InvalidateRenderableData(const Nz::InstancedRenderable* renderable, Nz::UInt32 flags, std::size_t index);
69  void InvalidateRenderableMaterial(const Nz::InstancedRenderable* renderable, std::size_t skinIndex, std::size_t matIndex, const Nz::MaterialRef& newMat);
70  inline void InvalidateRenderables();
71  void InvalidateReflectionMap();
72  inline void InvalidateTransformMatrix();
73 
74  void RegisterMaterial(Nz::Material* material, std::size_t count = 1);
75 
76  void OnAttached() override;
77  void OnComponentAttached(BaseComponent& component) override;
78  void OnComponentDetached(BaseComponent& component) override;
79  void OnDetached() override;
80 
81  void OnInstancedRenderableResetMaterials(const Nz::InstancedRenderable* renderable, std::size_t newMaterialCount);
82  void OnInstancedRenderableSkinChange(const Nz::InstancedRenderable* renderable, std::size_t newSkinIndex);
83  void OnMaterialReflectionChange(const Nz::Material* material, Nz::ReflectionMode reflectionMode);
84  void OnNodeInvalidated(const Nz::Node* node);
85 
86  void UnregisterMaterial(Nz::Material* material);
87 
88  void UpdateBoundingVolume() const;
89  void UpdateTransformMatrix() const;
90 
91  NazaraSlot(Nz::Node, OnNodeInvalidation, m_nodeInvalidationSlot);
92 
93  struct MaterialEntry
94  {
95  NazaraSlot(Nz::Material, OnMaterialReflectionModeChange, reflectionModelChangeSlot);
96 
97  std::size_t renderableCounter;
98  };
99 
100  struct Renderable
101  {
102  Renderable(const Nz::Matrix4f& transformMatrix) :
103  data(transformMatrix),
104  dataUpdated(false)
105  {
106  }
107 
108  Renderable(const Renderable&) = delete;
109  Renderable(Renderable&& rhs) noexcept = default;
110 
111  ~Renderable()
112  {
113  // Disconnect release slot before releasing instanced renderable reference
114  renderableReleaseSlot.Disconnect();
115  }
116 
117  Renderable& operator=(const Renderable&) = delete;
118  Renderable& operator=(Renderable&& r) noexcept = default;
119 
120  NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableInvalidateBoundingVolume, renderableBoundingVolumeInvalidationSlot);
121  NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableInvalidateData, renderableDataInvalidationSlot);
122  NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableInvalidateMaterial, renderableMaterialInvalidationSlot);
123  NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableRelease, renderableReleaseSlot);
124  NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableResetMaterials, renderableResetMaterialsSlot);
125  NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableSkinChange, renderableSkinChangeSlot);
126 
127  mutable Nz::InstancedRenderable::InstanceData data;
128  Nz::InstancedRenderableRef renderable;
129  mutable bool dataUpdated;
130  };
131 
132  using VolumeCullingListEntry = GraphicsComponentCullingList::VolumeEntry;
133 
134  struct VolumeCullingEntry
135  {
136  VolumeCullingListEntry listEntry;
137 
138  NazaraSlot(GraphicsComponentCullingList, OnCullingListRelease, cullingListReleaseSlot);
139  };
140 
141  std::size_t m_reflectiveMaterialCount;
142  mutable std::vector<VolumeCullingEntry> m_volumeCullingEntries;
143  std::vector<Renderable> m_renderables;
144  std::unordered_map<const Nz::Material*, MaterialEntry> m_materialEntries;
145  mutable Nz::BoundingVolumef m_boundingVolume;
146  mutable Nz::Matrix4f m_transformMatrix;
147  Nz::TextureRef m_reflectionMap;
148  mutable bool m_boundingVolumeUpdated;
149  mutable bool m_transformMatrixUpdated;
150  unsigned int m_reflectionMapSize;
151  };
152 }
153 
154 #include <NDK/Components/GraphicsComponent.inl>
155 
156 #endif // NDK_COMPONENTS_GRAPHICSCOMPONENT_HPP
157 #endif // NDK_SERVER
TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so...
Definition: Algorithm.hpp:12
Graphics class that represents the rendering queue for our scene.
Definition: AbstractRenderQueue.hpp:26
Graphics class that represents an instancer renderable.
Definition: InstancedRenderable.hpp:30
Core class that represents a reference to an object.
Definition: ObjectRef.hpp:18
NDK class that represents the rendering system.
Definition: RenderSystem.hpp:24
Graphics class that represents a material.
Definition: Material.hpp:51
NDK class that represents the component for graphics.
Definition: GraphicsComponent.hpp:24
NDK class that represents the common base of all components.
Definition: BaseComponent.hpp:17