Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
ForwardRenderQueue.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Graphics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_FORWARDRENDERQUEUE_HPP
8 #define NAZARA_FORWARDRENDERQUEUE_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Core/Color.hpp>
12 #include <Nazara/Graphics/AbstractRenderQueue.hpp>
13 #include <Nazara/Graphics/Material.hpp>
14 #include <Nazara/Math/Box.hpp>
15 #include <Nazara/Math/Matrix4.hpp>
16 #include <Nazara/Math/Plane.hpp>
17 #include <Nazara/Utility/IndexBuffer.hpp>
18 #include <Nazara/Utility/MeshData.hpp>
19 #include <Nazara/Utility/VertexBuffer.hpp>
20 #include <map>
21 
22 namespace Nz
23 {
24  class AbstractViewer;
25 
26  class NAZARA_GRAPHICS_API ForwardRenderQueue : public AbstractRenderQueue
27  {
28  friend class ForwardRenderTechnique;
29 
30  public:
31  ForwardRenderQueue() = default;
32  ~ForwardRenderQueue() = default;
33 
34  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr = nullptr, SparsePtr<const Color> colorPtr = nullptr) override;
35  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const float> alphaPtr) override;
36  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> colorPtr = nullptr) override;
37  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr) override;
38  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr = nullptr, SparsePtr<const Color> colorPtr = nullptr) override;
39  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const float> alphaPtr) override;
40  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> colorPtr = nullptr) override;
41  void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr) override;
42  void AddDrawable(int renderOrder, const Drawable* drawable) override;
43  void AddMesh(int renderOrder, const Material* material, const MeshData& meshData, const Boxf& meshAABB, const Matrix4f& transformMatrix) override;
44  void AddSprites(int renderOrder, const Material* material, const VertexStruct_XYZ_Color_UV* vertices, std::size_t spriteCount, const Texture* overlay = nullptr) override;
45 
46  void Clear(bool fully = false) override;
47 
48  void Sort(const AbstractViewer* viewer);
49 
50  struct MaterialComparator
51  {
52  bool operator()(const Material* mat1, const Material* mat2) const;
53  };
54 
55  struct MaterialPipelineComparator
56  {
57  bool operator()(const MaterialPipeline* pipeline1, const MaterialPipeline* pipeline2) const;
58  };
59 
62  {
63  Color color;
64  Vector3f center;
65  Vector2f size;
66  Vector2f sinCos;
67  };
68 
69  struct BatchedBillboardEntry
70  {
71  NazaraSlot(Material, OnMaterialRelease, materialReleaseSlot);
72 
73  std::vector<BillboardData> billboards;
74  };
75 
76  using BatchedBillboardContainer = std::map<const Material*, BatchedBillboardEntry, MaterialComparator>;
77 
78  struct BatchedBillboardPipelineEntry
79  {
80  BatchedBillboardContainer materialMap;
81  bool enabled = false;
82  };
83 
84  using BillboardPipelineBatches = std::map<const MaterialPipeline*, BatchedBillboardPipelineEntry, MaterialPipelineComparator>;
85 
88  {
89  const VertexStruct_XYZ_Color_UV* vertices;
90  std::size_t spriteCount;
91  };
92 
93  struct BatchedSpriteEntry
94  {
95  NazaraSlot(Texture, OnTextureRelease, textureReleaseSlot);
96 
97  std::vector<SpriteChain_XYZ_Color_UV> spriteChains;
98  };
99 
100  using SpriteOverlayBatches = std::map<const Texture*, BatchedSpriteEntry>;
101 
102  struct BatchedBasicSpriteEntry
103  {
104  NazaraSlot(Material, OnMaterialRelease, materialReleaseSlot);
105 
106  SpriteOverlayBatches overlayMap;
107  bool enabled = false;
108  };
109 
110  using SpriteMaterialBatches = std::map<const Material*, BatchedBasicSpriteEntry, MaterialComparator>;
111 
112  struct BatchedSpritePipelineEntry
113  {
114  SpriteMaterialBatches materialMap;
115  bool enabled = false;
116  };
117 
118  using SpritePipelineBatches = std::map<const MaterialPipeline*, BatchedSpritePipelineEntry, MaterialPipelineComparator>;
119 
122  {
123  bool operator()(const MeshData& data1, const MeshData& data2) const;
124  };
125 
126  struct MeshInstanceEntry
127  {
128  NazaraSlot(IndexBuffer, OnIndexBufferRelease, indexBufferReleaseSlot);
129  NazaraSlot(VertexBuffer, OnVertexBufferRelease, vertexBufferReleaseSlot);
130 
131  std::vector<Matrix4f> instances;
132  Spheref squaredBoundingSphere;
133  };
134 
135  using MeshInstanceContainer = std::map<MeshData, MeshInstanceEntry, MeshDataComparator>;
136 
137  struct BatchedModelEntry
138  {
139  NazaraSlot(Material, OnMaterialRelease, materialReleaseSlot);
140 
141  MeshInstanceContainer meshMap;
142  bool enabled = false;
143  };
144 
145  using MeshMaterialBatches = std::map<const Material*, BatchedModelEntry, MaterialComparator>;
146 
147  struct BatchedMaterialEntry
148  {
149  std::size_t maxInstanceCount = 0;
150  MeshMaterialBatches materialMap;
151  };
152 
153  using MeshPipelineBatches = std::map<const MaterialPipeline*, BatchedMaterialEntry, MaterialPipelineComparator>;
154 
155  struct UnbatchedModelData
156  {
157  Matrix4f transformMatrix;
158  MeshData meshData;
159  Spheref obbSphere;
160  const Material* material;
161  };
162 
163  struct UnbatchedSpriteData
164  {
165  std::size_t spriteCount;
166  const Material* material;
167  const Texture* overlay;
168  const VertexStruct_XYZ_Color_UV* vertices;
169  };
170 
171  struct Layer
172  {
173  BillboardPipelineBatches billboards;
174  SpritePipelineBatches opaqueSprites;
175  MeshPipelineBatches opaqueModels;
176  std::vector<std::size_t> depthSortedMeshes;
177  std::vector<std::size_t> depthSortedSprites;
178  std::vector<UnbatchedModelData> depthSortedMeshData;
179  std::vector<UnbatchedSpriteData> depthSortedSpriteData;
180  std::vector<const Drawable*> otherDrawables;
181  unsigned int clearCount = 0;
182  };
183 
184  std::map<int, Layer> layers;
185 
186  private:
187  BillboardData* GetBillboardData(int renderOrder, const Material* material, unsigned int count);
188  Layer& GetLayer(int i);
189 
190  void SortBillboards(Layer& layer, const Planef& nearPlane);
191  void SortForOrthographic(const AbstractViewer* viewer);
192  void SortForPerspective(const AbstractViewer* viewer);
193 
194  void OnIndexBufferInvalidation(const IndexBuffer* indexBuffer);
195  void OnMaterialInvalidation(const Material* material);
196  void OnTextureInvalidation(const Texture* texture);
197  void OnVertexBufferInvalidation(const VertexBuffer* vertexBuffer);
198  };
199 }
200 
201 #endif // NAZARA_FORWARDRENDERQUEUE_HPP
Core class that represents a color.
Definition: Color.hpp:18
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Graphics class that represents the viewer for our scene.
Definition: AbstractViewer.hpp:21
Meshes.
Definition: ForwardRenderQueue.hpp:121
Math class that represents a plane in 3D.
Definition: Plane.hpp:18
Sprites.
Definition: ForwardRenderQueue.hpp:87
Graphics class that represents the rendering queue for our scene.
Definition: AbstractRenderQueue.hpp:26
Core class that represents a pointer and the step between two elements.
Definition: SparsePtr.hpp:19
Billboards.
Definition: ForwardRenderQueue.hpp:61
Graphics class that represents the technique used in forward rendering.
Definition: ForwardRenderTechnique.hpp:21
Graphics class that represents a material.
Definition: Material.hpp:51
Graphics class used to contains all rendering states that are not allowed to change individually on r...
Definition: MaterialPipeline.hpp:44
Graphics class that represents the rendering queue for forward rendering.
Definition: ForwardRenderQueue.hpp:26
Graphics class that represents something drawable for our scene.
Definition: Drawable.hpp:15