Avara3D 0.2.0
C++ API reference
Material.h
1//
2// Material.h
3// avara3d
4//
5// Created by Morgan Davis on 10/21/16.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_VISUAL_MATERIAL_MATERIAL_H
10#define AVARA3D_VISUAL_MATERIAL_MATERIAL_H
11
12#include <climits>
13#include <memory>
14#include <optional>
15#include <string>
16#include <variant>
17#include <vector>
18
19#include "a3d/Color.h"
20#include "a3d/Id.h"
21#include "a3d/util/Bitmask.h"
22
23namespace a3d {
24
25 class Texture;
26
34 class Material {
35
36 public:
37 // [Public Types]
38
40 using Property = std::variant<std::monostate, std::shared_ptr<Texture>, Color>;
41
43 enum class PropertyType : uint8_t {
44 Ambient = 0,
45 Diffuse = 1,
46 Specular = 2,
47 Emission = 3
48 };
49
51 enum class FillMode {
52 Fill,
53 Lines,
54 Points
55 };
56
58 enum class AlphaMode : uint8_t {
60 Opaque, // no discard, no blending
62 Mask, // uses discard/alpha threshold
64 Blend
65 }; // real transparency
66
68 enum class BlendFunction : uint8_t {
69 Disabled,
70 Alpha,
71 Additive,
72 PremultipliedAlpha
73 };
74
75 // [Public Static Member Functions]
76
83 static std::shared_ptr<Material> DefaultMaterial();
84
86 static std::shared_ptr<Material> DiffuseMaterial(Property property);
87
89 static std::shared_ptr<Material> EmissionMaterial(Property property);
90
91 // [Public Lifecycle Functions]
92
95
98
101 const Property& diffuse,
102 const Property& specular,
103 const Property& emission);
104
105 Material(const Material& other);
106 Material& operator=(const Material& other);
107
108 Material(Material&& other) noexcept;
109 Material& operator=(Material&& other) noexcept;
110
111 ~Material();
112
113 // [Public Member Functions]
114
116 const std::optional<std::string>& name() const;
117
119 void name(const std::string& name);
120
122 const Property& ambient() const;
123
126
128 const Property& diffuse() const;
129
132
134 const Property& specular() const;
135
138
140 const Property& emission() const;
141
149
151 float specularExponent() const;
152
154 void specularExponent(float exponent);
155
158
160 void locksAmbientWithDiffuse(bool flag);
161
163 bool doubleSided() const;
164
166 void doubleSided(bool flag);
167
170
176 void fillMode(FillMode mode);
177
184 float uvScale() const;
185
191 void uvScale(float scale);
192
195
198
199 // ! NOT IMPLEMENTED !
201 float alphaCutoff() const;
202
204 void alphaCutoff(float v);
205
208
216
218 bool depthTestEnabled() const;
219
221 void depthTestEnabled(bool enabled);
222
224 bool depthWriteEnabled() const;
225
227 void depthWriteEnabled(bool enabled);
228
229 // [Internal Types]
230
231 using PropertyList = std::vector<std::pair<const Property*, PropertyType>>;
232
233 enum class DirtyMask : uint32_t {
234 None = 0,
235 All = UINT_MAX
236 };
237
238 // [Internal Static Member Functions]
239
240 static std::shared_ptr<Material> MissingTextureMaterial();
241 static Property MissingTextureProperty();
242
243 // [Internal Member Functions]
244
245 PropertyList properties() const;
246
247 MaterialId id() const noexcept;
248
249 DirtyMask dirtyMask() const;
250 void dirtyMask(DirtyMask mask);
251
252 private:
253 // [Private Member Variables]
254
255 MaterialId _id;
256 std::optional<std::string> _name;
257 Property _ambient;
258 Property _diffuse;
259 Property _specular;
260 Property _emission;
261 float _specularExponent;
262 bool _locksAmbientWithDiffuse;
263 bool _doubleSided;
264 FillMode _fillMode;
265 float _uvScale;
266 AlphaMode _alphaMode;
267 float _alphaCutoff;
268 BlendFunction _blendFunction;
269 bool _depthTestEnabled;
270 bool _depthWriteEnabled;
271 DirtyMask _dirtyMask;
272 };
273
274 namespace util::bitmask {
275
276 template<>
277 struct enable_ops<Material::DirtyMask> : std::true_type {};
278
279 }
280}
281
282#endif // AVARA3D_VISUAL_MATERIAL_MATERIAL_H
Represents a normalized RGBA color.
Definition: Color.h:27
Describes the surface appearance and render state of Mesh geometry.
Definition: Material.h:34
void doubleSided(bool flag)
Sets whether both sides of polygons are rendered.
Material()
Creates a Material with no lighting properties and default render state.
const Property & emission() const
Returns the self-emissive material property.
void ambient(const Property &ambient)
Sets the ambient material property.
FillMode
Selects polygon rasterization mode.
Definition: Material.h:51
const Property & specular() const
Returns the specular material property.
const Property & diffuse() const
Returns the diffuse material property.
static std::shared_ptr< Material > EmissionMaterial(Property property)
Creates a Material containing only the supplied emission property.
void alphaCutoff(float v)
Sets the stored alpha-mask cutoff; the renderer does not currently consume this value.
void blendFunction(BlendFunction function)
Sets the requested transparent blending function.
void diffuse(const Property &diffuse)
Sets the diffuse material property.
void depthWriteEnabled(bool enabled)
Enables or disables depth-buffer writes for the material.
AlphaMode alphaMode() const
Returns the alpha render mode.
BlendFunction blendFunction() const
Returns the requested transparent blending function.
void fillMode(FillMode mode)
Sets the polygon rasterization mode.
bool locksAmbientWithDiffuse() const
Returns whether diffuse content replaces ambient content during lighting.
const std::optional< std::string > & name() const
Returns the optional material name.
FillMode fillMode() const
Returns the polygon rasterization mode.
BlendFunction
Selects the blending function used for transparent materials.
Definition: Material.h:68
void emission(const Property &emission)
Sets the self-emissive material property.
PropertyType
Identifies one of the material's lighting properties.
Definition: Material.h:43
void name(const std::string &name)
Sets the material name.
Material(const Property &ambient, const Property &diffuse, const Property &specular)
Creates a Material with the supplied ambient, diffuse, and specular properties.
float alphaCutoff() const
Returns the stored alpha-mask cutoff; the renderer does not currently consume this value.
void uvScale(float scale)
Sets the uniform texture-coordinate scale.
void alphaMode(AlphaMode mode)
Sets the alpha render mode.
AlphaMode
Selects how material alpha participates in render-pass selection.
Definition: Material.h:58
bool depthTestEnabled() const
Returns whether depth testing is enabled for the material.
static std::shared_ptr< Material > DefaultMaterial()
Returns the shared mutable fallback Material used for geometry without a usable material.
void specularExponent(float exponent)
Sets the Phong specular exponent controlling highlight sharpness.
static std::shared_ptr< Material > DiffuseMaterial(Property property)
Creates a Material containing only the supplied diffuse property.
void specular(const Property &specular)
Sets the specular material property.
float uvScale() const
Returns the uniform texture-coordinate scale.
Material(const Property &ambient, const Property &diffuse, const Property &specular, const Property &emission)
Creates a Material with the supplied lighting properties.
bool doubleSided() const
Returns whether both sides of polygons are rendered.
std::variant< std::monostate, std::shared_ptr< Texture >, Color > Property
Material property contents: absent, a shared Texture, or a constant Color.
Definition: Material.h:40
float specularExponent() const
Returns the Phong specular exponent controlling highlight sharpness.
const Property & ambient() const
Returns the ambient material property.
bool depthWriteEnabled() const
Returns whether rendering the material writes to the depth buffer.
void depthTestEnabled(bool enabled)
Enables or disables depth testing for the material.
void locksAmbientWithDiffuse(bool flag)
Sets whether diffuse content replaces ambient content during lighting.