SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Material.hpp
Go to the documentation of this file.
1#ifndef _MATERIAL_HPP_
2#define _MATERIAL_HPP_
3
4#include <Core/Object.hpp>
5#include <Core/OSDef.hpp>
7#include <Memory/RefPtr.hpp>
8#include <Runtime/Texture.hpp>
9#include <Math/Vector.hpp>
10#include <Math/Color.hpp>
11#include <string>
12#include <cstdint>
13
14// Texture binding slots (must match shader register/binding layout)
15#define TEXTURE_SLOT_DIFFUSE 0
16#define TEXTURE_SLOT_NORMAL 1
17#define TEXTURE_SLOT_SPECULAR 2
18#define TEXTURE_SLOT_ROUGHNESS 3
19#define TEXTURE_SLOT_METALLIC 4
20#define TEXTURE_SLOT_AO 5
21#define TEXTURE_SLOT_EMISSIVE 6
22
23namespace Sleak {
24
25 namespace RenderEngine {
26 class Shader;
27 class BufferBase;
28 struct MaterialGPUData;
29 }
30
31 /// Controls how a material handles transparency.
32 /// @ingroup rendering
33 enum class MaterialRenderMode : uint8_t {
34 Opaque = 0, // Fully opaque, no alpha blending
35 Cutout = 1, // Binary alpha test (clip below AlphaCutoff)
36 Transparent = 2 // Alpha blending
37 };
38
39 /// PBR-ish surface properties, textures, and GPU buffer for a drawable;
40 /// shared across any mesh that references it.
41 ///
42 /// A Material describes how a surface responds to light: a base color,
43 /// metallic, roughness, ambient occlusion, emissive, and opacity, plus
44 /// up to seven texture maps that override those scalars per texel. It
45 /// owns its own GPU constant buffer and rebuilds it when properties
46 /// change.
47 ///
48 /// Materials are meant to be shared. Wrap one in a RefPtr and hand the
49 /// same RefPtr to every MaterialComponent that should use it; changing
50 /// a property updates every object drawn with it. Textures are held by
51 /// RefPtr too. Each texture setter takes a `RefPtr<Texture>` to share
52 /// one texture between materials, a raw `Texture*` the material adopts
53 /// (do not hand the same raw pointer to two materials), or a file path
54 /// the material loads for you.
55 ///
56 /// SetRenderMode() decides how transparency is handled:
57 /// MaterialRenderMode::Opaque and ::Cutout render in the normal
58 /// (deferred, where the backend supports it) path, while ::Transparent
59 /// forces the material into a forward pass. IsForwardRendered()
60 /// reports which side a material falls on.
61 ///
62 /// @code{.cpp}
63 /// auto* mat = new Sleak::Material();
64 /// mat->SetShader("assets/shaders/default_shader.hlsl");
65 /// mat->SetDiffuseColor((uint8_t)230, (uint8_t)230, (uint8_t)230);
66 /// mat->SetMetallic(0.0f);
67 /// mat->SetRoughness(0.35f);
68 /// mat->SetAO(1.0f);
69 ///
70 /// // Texture maps, loaded by path
71 /// mat->SetDiffuseTexture("assets/textures/crate_albedo.png");
72 /// mat->SetNormalTexture("assets/textures/crate_normal.png");
73 /// mat->SetTiling(2.0f, 2.0f);
74 ///
75 /// // Share one material across many objects
76 /// Sleak::RefPtr<Sleak::Material> shared(mat);
77 /// crate->AddComponent<Sleak::MaterialComponent>(shared);
78 /// barrel->AddComponent<Sleak::MaterialComponent>(shared);
79 ///
80 /// // Alpha-blended glass
81 /// auto* glass = new Sleak::Material();
82 /// glass->SetRenderMode(Sleak::MaterialRenderMode::Transparent);
83 /// glass->SetOpacity(0.35f);
84 /// glass->SetTwoSided(true);
85 /// @endcode
86 ///
87 /// @see MaterialComponent, Texture, MaterialRenderMode, MeshBatch,
88 /// RefPtr
89 /// @ingroup rendering
90 class ENGINE_API Material : public Object {
91 public:
92 Material();
93 ~Material() override;
94
95 void Initialize();
96 void Bind();
97
98 // Shader
99 void SetShader(RenderEngine::Shader* shader);
100 void SetShader(const std::string& shaderPath);
102
103 // Texture slots: shared RefPtr, adopted Texture*, or a file path
104 void SetDiffuseTexture(const RefPtr<Texture>& texture);
105 void SetDiffuseTexture(Texture* texture);
106 void SetDiffuseTexture(const std::string& path);
107 Texture* GetDiffuseTexture() const;
108 bool HasDiffuseTexture() const;
109
110 void SetNormalTexture(const RefPtr<Texture>& texture);
111 void SetNormalTexture(Texture* texture);
112 void SetNormalTexture(const std::string& path);
113 Texture* GetNormalTexture() const;
114 bool HasNormalTexture() const;
115
116 void SetSpecularTexture(const RefPtr<Texture>& texture);
117 void SetSpecularTexture(Texture* texture);
118 void SetSpecularTexture(const std::string& path);
120 bool HasSpecularTexture() const;
121
122 void SetRoughnessTexture(const RefPtr<Texture>& texture);
123 void SetRoughnessTexture(Texture* texture);
124 void SetRoughnessTexture(const std::string& path);
126 bool HasRoughnessTexture() const;
127
128 void SetMetallicTexture(const RefPtr<Texture>& texture);
129 void SetMetallicTexture(Texture* texture);
130 void SetMetallicTexture(const std::string& path);
132 bool HasMetallicTexture() const;
133
134 void SetAOTexture(const RefPtr<Texture>& texture);
135 void SetAOTexture(Texture* texture);
136 void SetAOTexture(const std::string& path);
137 Texture* GetAOTexture() const;
138 bool HasAOTexture() const;
139
140 void SetEmissiveTexture(const RefPtr<Texture>& texture);
141 void SetEmissiveTexture(Texture* texture);
142 void SetEmissiveTexture(const std::string& path);
144 bool HasEmissiveTexture() const;
145
146 // Color properties
147 void SetDiffuseColor(Math::Color color);
148 void SetDiffuseColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
149 void SetDiffuseColor(float r, float g, float b, float a = 1.0f);
151
152 void SetSpecularColor(Math::Color color);
153 void SetSpecularColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
155
156 void SetEmissiveColor(Math::Color color);
157 void SetEmissiveColor(uint8_t r, uint8_t g, uint8_t b);
158 void SetEmissiveColor(float r, float g, float b);
160
161 // Scalar PBR/shading properties
162 void SetShininess(float shininess);
163 float GetShininess() const;
164
165 void SetMetallic(float metallic);
166 float GetMetallic() const;
167
168 void SetRoughness(float roughness);
169 float GetRoughness() const;
170
171 void SetAO(float ao);
172 float GetAO() const;
173
174 void SetNormalIntensity(float intensity);
175 float GetNormalIntensity() const;
176
177 void SetEmissiveIntensity(float intensity);
178 float GetEmissiveIntensity() const;
179
180 void SetOpacity(float opacity);
181 float GetOpacity() const;
182
183 void SetAlphaCutoff(float cutoff);
184 float GetAlphaCutoff() const;
185
186 // UV transform
187 void SetTiling(float x, float y);
188 void SetTiling(Math::Vector2D tiling);
190
191 void SetOffset(float x, float y);
192 void SetOffset(Math::Vector2D offset);
194
195 // Rendering state
198
199 void SetTwoSided(bool twoSided);
200 bool IsTwoSided() const;
201
202 /// True if this material must be rendered in a forward pass
203 /// (transparent, alpha-blended). Opaque and cutout materials return false.
204 bool IsForwardRendered() const;
205
206 /// Bind only textures and material CB (skips shader bind).
207 /// Used by the deferred geometry pass which overrides the shader.
208 void BindTexturesAndCB();
209
210 private:
211 /// Build GPU-aligned data struct from current properties.
212 RenderEngine::MaterialGPUData BuildGPUData() const;
213
214 // Shader
216
217 // GPU constant buffer (slot 1)
219
220 // Texture maps
221 RefPtr<Texture> m_diffuseTexture;
222 RefPtr<Texture> m_normalTexture;
223 RefPtr<Texture> m_specularTexture;
224 RefPtr<Texture> m_roughnessTexture;
225 RefPtr<Texture> m_metallicTexture;
226 RefPtr<Texture> m_aoTexture;
227 RefPtr<Texture> m_emissiveTexture;
228
229 // Color properties (stored as 0-255 Color, converted to float for GPU)
230 Math::Color m_diffuseColor {255, 255, 255, 255};
231 Math::Color m_specularColor {255, 255, 255, 255};
232 Math::Color m_emissiveColor {0, 0, 0, 255};
233
234 // Scalar properties
235 float m_shininess = 32.0f;
236 float m_metallic = 0.0f;
237 float m_roughness = 0.5f;
238 float m_ao = 1.0f;
239 float m_normalIntensity = 1.0f;
240 float m_emissiveIntensity = 1.0f;
241 float m_opacity = 1.0f;
242 float m_alphaCutoff = 0.5f;
243
244 // UV transform
245 float m_tilingX = 1.0f;
246 float m_tilingY = 1.0f;
247 float m_offsetX = 0.0f;
248 float m_offsetY = 0.0f;
249
250 // Rendering state
251 MaterialRenderMode m_renderMode = MaterialRenderMode::Opaque;
252 bool m_twoSided = false;
253 };
254};
255
256#endif
void SetRoughnessTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:209
float GetAlphaCutoff() const
Definition Material.cpp:386
Math::Color GetEmissiveColor() const
Definition Material.cpp:338
Texture * GetRoughnessTexture() const
Definition Material.cpp:222
void SetShader(RenderEngine::Shader *shader)
Definition Material.cpp:131
Texture * GetMetallicTexture() const
Definition Material.cpp:243
bool HasNormalTexture() const
Definition Material.cpp:184
Math::Color GetSpecularColor() const
Definition Material.cpp:319
RenderEngine::Shader * GetShader() const
Definition Material.cpp:142
Texture * GetNormalTexture() const
Definition Material.cpp:180
void SetAOTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:251
void SetAO(float ao)
Definition Material.cpp:360
void SetOpacity(float opacity)
Definition Material.cpp:378
Texture * GetDiffuseTexture() const
Definition Material.cpp:159
void SetMetallic(float metallic)
Definition Material.cpp:348
void SetShininess(float shininess)
Definition Material.cpp:342
void SetEmissiveTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:270
bool HasEmissiveTexture() const
Definition Material.cpp:287
void SetOffset(float x, float y)
Definition Material.cpp:402
bool IsTwoSided() const
Definition Material.cpp:428
bool HasAOTexture() const
Definition Material.cpp:266
~Material() override
float GetOpacity() const
Definition Material.cpp:380
void SetDiffuseTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:146
void SetNormalTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:167
void SetSpecularColor(Math::Color color)
Definition Material.cpp:310
float GetShininess() const
Definition Material.cpp:346
Texture * GetEmissiveTexture() const
Definition Material.cpp:283
void SetRenderMode(MaterialRenderMode mode)
Definition Material.cpp:416
float GetAO() const
Definition Material.cpp:362
void SetAlphaCutoff(float cutoff)
Definition Material.cpp:382
void Initialize()
Lazily allocates the material's constant buffer on first use.
Definition Material.cpp:19
bool HasRoughnessTexture() const
Definition Material.cpp:226
void BindTexturesAndCB()
Binds textures and uploads the material CB for the deferred geometry pass, skipping the shader bind.
Definition Material.cpp:114
bool HasSpecularTexture() const
Definition Material.cpp:205
void SetSpecularTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:188
Texture * GetSpecularTexture() const
Definition Material.cpp:201
Math::Vector2D GetTiling() const
Definition Material.cpp:398
Math::Color GetDiffuseColor() const
Definition Material.cpp:308
void SetMetallicTexture(const RefPtr< Texture > &texture)
Definition Material.cpp:230
void SetTwoSided(bool twoSided)
Definition Material.cpp:424
MaterialRenderMode GetRenderMode() const
Definition Material.cpp:420
bool HasDiffuseTexture() const
Definition Material.cpp:163
void SetNormalIntensity(float intensity)
Definition Material.cpp:364
Math::Vector2D GetOffset() const
Definition Material.cpp:412
void SetEmissiveColor(Math::Color color)
Definition Material.cpp:323
void SetDiffuseColor(Math::Color color)
Definition Material.cpp:291
void SetRoughness(float roughness)
Definition Material.cpp:354
bool IsForwardRendered() const
True when this material must skip the GBuffer and render in the forward transparent pass.
Definition Material.cpp:108
Texture * GetAOTexture() const
Definition Material.cpp:264
float GetNormalIntensity() const
Definition Material.cpp:368
float GetRoughness() const
Definition Material.cpp:358
void SetTiling(float x, float y)
Definition Material.cpp:388
float GetMetallic() const
Definition Material.cpp:352
void SetEmissiveIntensity(float intensity)
Definition Material.cpp:370
void Bind()
Binds shader, all set textures, and uploads the packed material CB for a forward draw.
Definition Material.cpp:31
float GetEmissiveIntensity() const
Definition Material.cpp:374
bool HasMetallicTexture() const
Definition Material.cpp:247
Object(const std::string &name="Object")
Definition Object.hpp:14
Backend-agnostic compiled shader program.
Definition Shader.hpp:11
MaterialRenderMode
Definition Material.hpp:33
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
Packed PBR material parameters uploaded to the material constant buffer.