MxEngine
InstanceFactory.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <vector>
32 #include "Utilities/Math/Math.h"
33 #include "Utilities/Memory/Memory.h"
34 #include "Utilities/Profiler/Profiler.h"
35 #include "Core/Components/Transform.h"
36 #include "Utilities/AbstractFactory/AbstractFactory.h"
37 #include "Core/Resources/Mesh.h"
38 
39 namespace MxEngine
40 {
42  {
43  Vector3 color{ 1.0f };
44  public:
46 
47  inline void SetColor(const Vector3& color)
48  {
49  this->color = Clamp(color, MakeVector3(0.0f), MakeVector3(1.0f));
50  }
51 
52  inline const Vector3& GetColor() const
53  {
54  return this->color;
55  }
56  };
57 
59  {
60  MAKE_COMPONENT(InstanceFactory);
61  public:
62  struct Factory
63  {
65 
66  template<typename T>
67  void Destroy(LocalResource<T, Factory>& resource)
68  {
69  inner.GetPool<T>().Deallocate(resource.GetHandle());
70  }
71 
72  template<typename T>
73  auto& Get()
74  {
75  return inner.GetPool<T>();
76  }
77  };
78 
79  using ModelData = MxVector<Matrix4x4>;
80  using NormalData = MxVector<Matrix3x3>;
81  using ColorData = MxVector<Vector3>;
82  using BufferIndex = size_t;
83 
85  private:
86  Factory factory;
87  ModelData models;
88  NormalData normals;
89  ColorData colors;
90  BufferIndex bufferIndex = std::numeric_limits<BufferIndex>::max();
91 
92  template<typename T>
93  BufferIndex AddInstancedBuffer(Mesh& mesh, const MxVector<T>& data)
94  {
95  auto VBO = GraphicFactory::Create<VertexBuffer>();
96  auto VBL = GraphicFactory::Create<VertexBufferLayout>();
97  VBL->Push<T>();
98  VBO->Load((float*)data.data(), data.size() * sizeof(T) / sizeof(float), UsageType::DYNAMIC_DRAW);
99  return mesh.AddInstancedBuffer(std::move(VBO), std::move(VBL));
100  }
101 
102  template<typename T>
103  void BufferDataByIndex(const Mesh& mesh, size_t index, const MxVector<T>& buffer)
104  {
105  auto VBO = mesh.GetBufferByIndex(index);
106  VBO->BufferDataWithResize((float*)buffer.data(), buffer.size() * sizeof(T) / sizeof(float));
107  }
108 
109  void RemoveInstancedBuffer(Mesh& mesh, size_t index);
110  void Destroy();
111  public:
112  const auto& GetInstancePool() const { return this->factory.inner.Pool; }
113  auto& GetInstancePool() { return this->factory.inner.Pool; };
114  size_t GetCount() const { return this->GetInstancePool().Allocated(); }
115  auto GetInstances() { return ComponentView{ this->GetInstancePool() }; }
116 
117  void Init();
118  void SubmitInstances();
119  MxInstance MakeInstance();
120 
121  ModelData& GetModelData();
122  NormalData& GetNormalData();
123  ColorData& GetColorData();
124 
125  InstanceFactory() = default;
126  InstanceFactory(const InstanceFactory&) = delete;
127  InstanceFactory(InstanceFactory&&) noexcept = default;
128  InstanceFactory& operator=(const InstanceFactory&) = delete;
129  InstanceFactory& operator=(InstanceFactory&&) noexcept = default;
130  ~InstanceFactory();
131  };
132 }
Definition: Transform.h:36
Definition: InstanceFactory.h:41
Definition: InstanceFactory.h:62
Definition: ComponentView.h:42
Definition: InstanceFactory.h:58
Definition: AbstractFactory.h:399
Definition: AbstractFactory.h:240
Definition: Mesh.h:43
Definition: Application.cpp:49