Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Material.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_MATERIAL_HPP
8 #define NAZARA_MATERIAL_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Core/Color.hpp>
12 #include <Nazara/Core/ObjectLibrary.hpp>
13 #include <Nazara/Core/ObjectRef.hpp>
14 #include <Nazara/Core/RefCounted.hpp>
15 #include <Nazara/Core/Resource.hpp>
16 #include <Nazara/Core/ResourceLoader.hpp>
17 #include <Nazara/Core/ResourceManager.hpp>
18 #include <Nazara/Core/ResourceParameters.hpp>
19 #include <Nazara/Core/Signal.hpp>
20 #include <Nazara/Core/String.hpp>
21 #include <Nazara/Graphics/Config.hpp>
22 #include <Nazara/Graphics/Enums.hpp>
23 #include <Nazara/Graphics/MaterialPipeline.hpp>
24 #include <Nazara/Renderer/Texture.hpp>
25 #include <Nazara/Renderer/TextureSampler.hpp>
26 #include <Nazara/Renderer/UberShader.hpp>
27 
28 namespace Nz
29 {
30  struct NAZARA_GRAPHICS_API MaterialParams : ResourceParameters
31  {
32  bool loadAlphaMap = true;
33  bool loadDiffuseMap = true;
34  bool loadEmissiveMap = true;
35  bool loadHeightMap = true;
36  bool loadNormalMap = true;
37  bool loadSpecularMap = true;
38  String shaderName = "Basic";
39 
40  bool IsValid() const;
41  };
42 
43  class Material;
44 
45  using MaterialConstRef = ObjectRef<const Material>;
46  using MaterialLibrary = ObjectLibrary<Material>;
47  using MaterialLoader = ResourceLoader<Material, MaterialParams>;
48  using MaterialManager = ResourceManager<Material, MaterialParams>;
49  using MaterialRef = ObjectRef<Material>;
50 
51  class NAZARA_GRAPHICS_API Material : public RefCounted, public Resource
52  {
53  friend MaterialLibrary;
54  friend MaterialLoader;
55  friend MaterialManager;
56  friend class Graphics;
57 
58  public:
59  inline Material();
60  inline Material(const MaterialPipeline* pipeline);
61  inline Material(const MaterialPipelineInfo& pipelineInfo);
62  inline Material(const String& pipelineName);
63  inline Material(const Material& material);
64  inline ~Material();
65 
66  void Apply(const MaterialPipeline::Instance& instance) const;
67 
68  void BuildFromParameters(const ParameterList& matData, const MaterialParams& matParams = MaterialParams());
69 
70  inline void Configure(const MaterialPipeline* pipeline);
71  inline void Configure(const MaterialPipelineInfo& pipelineInfo);
72  inline bool Configure(const String& pipelineName);
73 
74  inline void EnableAlphaTest(bool alphaTest);
75  inline void EnableBlending(bool blending);
76  inline void EnableColorWrite(bool colorWrite);
77  inline void EnableDepthBuffer(bool depthBuffer);
78  inline void EnableDepthSorting(bool depthSorting);
79  inline void EnableDepthWrite(bool depthWrite);
80  inline void EnableFaceCulling(bool faceCulling);
81  inline void EnableReflectionMapping(bool reflection);
82  inline void EnableScissorTest(bool scissorTest);
83  inline void EnableShadowCasting(bool castShadows);
84  inline void EnableShadowReceive(bool receiveShadows);
85  inline void EnableStencilTest(bool stencilTest);
86 
87  inline void EnsurePipelineUpdate() const;
88 
89  inline const TextureRef& GetAlphaMap() const;
90  inline float GetAlphaThreshold() const;
91  inline Color GetAmbientColor() const;
92  inline RendererComparison GetDepthFunc() const;
93  inline const MaterialRef& GetDepthMaterial() const;
94  inline Color GetDiffuseColor() const;
95  inline const TextureRef& GetDiffuseMap() const;
96  inline TextureSampler& GetDiffuseSampler();
97  inline const TextureSampler& GetDiffuseSampler() const;
98  inline BlendFunc GetDstBlend() const;
99  inline const TextureRef& GetEmissiveMap() const;
100  inline FaceSide GetFaceCulling() const;
101  inline FaceFilling GetFaceFilling() const;
102  inline const TextureRef& GetHeightMap() const;
103  inline float GetLineWidth() const;
104  inline const TextureRef& GetNormalMap() const;
105  inline const MaterialPipeline* GetPipeline() const;
106  inline const MaterialPipelineInfo& GetPipelineInfo() const;
107  inline float GetPointSize() const;
108  inline ReflectionMode GetReflectionMode() const;
109  inline const UberShader* GetShader() const;
110  inline float GetShininess() const;
111  inline Color GetSpecularColor() const;
112  inline const TextureRef& GetSpecularMap() const;
113  inline TextureSampler& GetSpecularSampler();
114  inline const TextureSampler& GetSpecularSampler() const;
115  inline BlendFunc GetSrcBlend() const;
116 
117  inline bool HasAlphaMap() const;
118  inline bool HasDepthMaterial() const;
119  inline bool HasDiffuseMap() const;
120  inline bool HasEmissiveMap() const;
121  inline bool HasHeightMap() const;
122  inline bool HasNormalMap() const;
123  inline bool HasSpecularMap() const;
124 
125  inline bool IsAlphaTestEnabled() const;
126  inline bool IsBlendingEnabled() const;
127  inline bool IsColorWriteEnabled() const;
128  inline bool IsDepthBufferEnabled() const;
129  inline bool IsDepthSortingEnabled() const;
130  inline bool IsDepthWriteEnabled() const;
131  inline bool IsFaceCullingEnabled() const;
132  inline bool IsReflectionMappingEnabled() const;
133  inline bool IsScissorTestEnabled() const;
134  inline bool IsStencilTestEnabled() const;
135  inline bool IsShadowCastingEnabled() const;
136  inline bool IsShadowReceiveEnabled() const;
137 
138  inline bool LoadFromFile(const String& filePath, const MaterialParams& params = MaterialParams());
139  inline bool LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params = MaterialParams());
140  inline bool LoadFromStream(Stream& stream, const MaterialParams& params = MaterialParams());
141 
142  void Reset();
143 
144  void SaveToParameters(ParameterList* matData);
145 
146  inline bool SetAlphaMap(const String& textureName);
147  inline void SetAlphaMap(TextureRef alphaMap);
148  inline void SetAlphaThreshold(float alphaThreshold);
149  inline void SetAmbientColor(const Color& ambient);
150  inline void SetDepthFunc(RendererComparison depthFunc);
151  inline void SetDepthMaterial(MaterialRef depthMaterial);
152  inline void SetDiffuseColor(const Color& diffuse);
153  inline bool SetDiffuseMap(const String& textureName);
154  inline void SetDiffuseMap(TextureRef diffuseMap);
155  inline void SetDiffuseSampler(const TextureSampler& sampler);
156  inline void SetDstBlend(BlendFunc func);
157  inline bool SetEmissiveMap(const String& textureName);
158  inline void SetEmissiveMap(TextureRef textureName);
159  inline void SetFaceCulling(FaceSide faceSide);
160  inline void SetFaceFilling(FaceFilling filling);
161  inline bool SetHeightMap(const String& textureName);
162  inline void SetHeightMap(TextureRef textureName);
163  inline void SetLineWidth(float lineWidth);
164  inline bool SetNormalMap(const String& textureName);
165  inline void SetNormalMap(TextureRef textureName);
166  inline void SetPointSize(float pointSize);
167  inline void SetReflectionMode(ReflectionMode reflectionMode);
168  inline void SetShader(UberShaderConstRef uberShader);
169  inline bool SetShader(const String& uberShaderName);
170  inline void SetShininess(float shininess);
171  inline void SetSpecularColor(const Color& specular);
172  inline bool SetSpecularMap(const String& textureName);
173  inline void SetSpecularMap(TextureRef specularMap);
174  inline void SetSpecularSampler(const TextureSampler& sampler);
175  inline void SetSrcBlend(BlendFunc func);
176 
177  inline Material& operator=(const Material& material);
178 
179  inline static MaterialRef GetDefault();
180  inline static int GetTextureUnit(TextureMap textureMap);
181  template<typename... Args> static MaterialRef New(Args&&... args);
182 
183  // Signals:
184  NazaraSignal(OnMaterialReflectionModeChange, const Material* /*material*/, ReflectionMode /*newReflectionMode*/);
185  NazaraSignal(OnMaterialRelease, const Material* /*material*/);
186  NazaraSignal(OnMaterialReset, const Material* /*material*/);
187 
188  private:
189  void Copy(const Material& material);
190  inline void InvalidatePipeline();
191  inline void UpdatePipeline() const;
192 
193  static bool Initialize();
194  static void Uninitialize();
195 
196  Color m_ambientColor;
197  Color m_diffuseColor;
198  Color m_specularColor;
199  MaterialRef m_depthMaterial; //< Materialception
200  mutable const MaterialPipeline* m_pipeline;
201  MaterialPipelineInfo m_pipelineInfo;
202  ReflectionMode m_reflectionMode;
203  TextureSampler m_diffuseSampler;
204  TextureSampler m_specularSampler;
205  TextureRef m_alphaMap;
206  TextureRef m_diffuseMap;
207  TextureRef m_emissiveMap;
208  TextureRef m_heightMap;
209  TextureRef m_normalMap;
210  TextureRef m_specularMap;
211  mutable bool m_pipelineUpdated;
212  bool m_shadowCastingEnabled;
213  float m_alphaThreshold;
214  float m_shininess;
215  unsigned int m_reflectionSize;
216 
217  static std::array<int, TextureMap_Max + 1> s_textureUnits;
218  static MaterialLibrary::LibraryMap s_library;
219  static MaterialLoader::LoaderList s_loaders;
220  static MaterialManager::ManagerMap s_managerMap;
221  static MaterialManager::ManagerParams s_managerParameters;
222  static MaterialRef s_defaultMaterial;
223  };
224 }
225 
226 #include <Nazara/Graphics/Material.inl>
227 
228 #endif // NAZARA_MATERIAL_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 module initializer of Graphics.
Definition: Graphics.hpp:15
Core class that represents a string.
Definition: String.hpp:22
decltype(auto) Apply(F &&fn, Tuple &&t)
Applies the tuple to the function (e.g. calls the function using the tuple content as arguments) ...
Definition: Algorithm.inl:48
Core class that represents a loader of resources.
Definition: ResourceLoader.hpp:23
Core class that represents a resource.
Definition: Resource.hpp:15
Core class that represents a reference with a counter.
Definition: RefCounted.hpp:21
Graphics class that represents a material.
Definition: Material.hpp:51
Core class that represents a list of parameters.
Definition: ParameterList.hpp:18
Core class that represents a resource manager.
Definition: ResourceManager.hpp:18
Graphics class used to contains all rendering states that are not allowed to change individually on r...
Definition: MaterialPipeline.hpp:44
Core class that represents a stream.
Definition: Stream.hpp:19