CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
MaterialModel.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
13
15
22
23#include <utility>
24
25namespace CeresEngine {
26
31 template<typename ValueType> struct MaterialPropertySwizzle final {
34
36 RawType raw = {ValueType(1), ValueType(0), ValueType(0), ValueType(0)};
37
39 constexpr MaterialPropertySwizzle() = default;
40
42 CE_EXPLICIT(false) constexpr MaterialPropertySwizzle(RawType swizzle) : raw(swizzle) {} // NOLINT
43
45 CE_EXPLICIT(false) constexpr MaterialPropertySwizzle(ValueType r, ValueType g, ValueType b, ValueType a) : raw(r, g, b, a) {}
46
48 explicit operator RawType() const noexcept { return raw; }
49 };
50
52 template<glm::length_t D, typename ValueType> struct MaterialPropertySwizzle<TVector<D, ValueType>> final {
55
58
60 constexpr MaterialPropertySwizzle() = default;
61
63 CE_EXPLICIT(false) constexpr MaterialPropertySwizzle(RawType swizzle) : raw(swizzle) {} // NOLINT
64
67 template<glm::length_t L = D> requires(L == 2)
69
73 template<glm::length_t L = D> requires(L == 3)
75
79 template<glm::length_t L = D> requires(L == 4)
81
83 explicit operator RawType() const noexcept { return raw; }
84 };
85
87 template<typename ValueType> struct MaterialPropertySwizzle<TColor<ValueType>> final {
90
93
95 constexpr MaterialPropertySwizzle() = default;
96
98 CE_EXPLICIT(false) constexpr MaterialPropertySwizzle(RawType swizzle) : raw(swizzle) {} // NOLINT
99
104
106 explicit operator RawType() const noexcept { return raw; }
107 };
108
114 template<typename ValueType>
115 inline constexpr bool operator==(const MaterialPropertySwizzle<ValueType>& lhs, const MaterialPropertySwizzle<ValueType>& rhs) noexcept {
116 return rhs.raw == lhs.raw;
117 }
118
124 template<typename ValueType>
125 inline constexpr bool operator!=(const MaterialPropertySwizzle<ValueType>& lhs, const MaterialPropertySwizzle<ValueType>& rhs) noexcept {
126 return rhs.raw != lhs.raw;
127 }
128
131
134
137
140
143
146
147 extern template struct MaterialPropertySwizzle<float>;
148 extern template struct MaterialPropertySwizzle<double>;
149 extern template struct MaterialPropertySwizzle<Vector2>;
150 extern template struct MaterialPropertySwizzle<Vector3>;
151 extern template struct MaterialPropertySwizzle<Vector4>;
152 extern template struct MaterialPropertySwizzle<Color>;
153
155 template<typename ValueType, typename TextureType = HTexture> struct MaterialProperty {
157 ValueType value = ValueType(0);
158
160 TextureType texture = nullptr;
161
165 .magFilter = GPUSamplerFilter::Linear,
166 .mipMapFilter = GPUSamplerFilter::Linear,
167 .mipMapping = true,
168 };
169
172
174 MaterialProperty() = default;
175
177 CE_EXPLICIT(false) MaterialProperty(ValueType value) : value(value) {} // NOLINT
178
180 CE_EXPLICIT(false) MaterialProperty(TextureType texture, MaterialPropertySwizzle<ValueType> swizzle = {}) // NOLINT
181 : texture(texture), swizzle(swizzle) {}
182
184 explicit MaterialProperty(ValueType value, TextureType texture, MaterialPropertySwizzle<ValueType> swizzle = {})
185 : value(value), texture(texture), swizzle(swizzle) {}
186
189
192 };
193
202 return lhs.value == rhs.value && lhs.texture == rhs.texture && lhs.sampler == rhs.sampler && lhs.value == rhs.value;
203 }
204
211 template<typename ValueType, typename TextureType>
213 return !(lhs == rhs);
214 }
215
218
221
224
227
230
233
234 extern template struct MaterialProperty<float>;
235 extern template struct MaterialProperty<double>;
236 extern template struct MaterialProperty<Vector2>;
237 extern template struct MaterialProperty<Vector3>;
238 extern template struct MaterialProperty<Vector4>;
239 extern template struct MaterialProperty<Color>;
240
241 // ---------------------------------------------------------------------------------------------
242
246 Tangent,
247
249 Object,
250
252 World,
253 };
254
256 struct PBRMaterialModel final {
259 ColorMaterialProperty baseColor = Color::zero;
260
262 DoubleMaterialProperty roughness = 1.0;
263
266
269
271 ColorMaterialProperty emission = Color::zero;
272
274 Vector3MaterialProperty displacement = Vector3(0.0);
275
277 DoubleMaterialProperty ambientOcclusion = 1.0;
278
281 };
282
287 inline constexpr bool operator==(const PBRMaterialModel& lhs, const PBRMaterialModel& rhs) noexcept {
288 return lhs.baseColor == rhs.baseColor && lhs.roughness == rhs.roughness && lhs.metallic == rhs.metallic && lhs.normal == rhs.normal &&
289 lhs.emission == rhs.emission && lhs.displacement == rhs.displacement && lhs.ambientOcclusion == rhs.ambientOcclusion;
290 }
291
296 inline constexpr bool operator!=(const PBRMaterialModel& lhs, const PBRMaterialModel& rhs) noexcept { return !(lhs == rhs); }
297
298} // namespace CeresEngine
299
301template<typename ValueType> struct std::hash<CeresEngine::MaterialPropertySwizzle<ValueType>> {
303 size_t operator()(const Type& swizzle) const {
304 using namespace CeresEngine;
305 return CeresEngine::hash(swizzle.raw);
306 }
307};
308
310template<typename ValueType, typename TextureType> struct std::hash<CeresEngine::MaterialProperty<ValueType, TextureType>> {
312 size_t operator()(const Type& property) const {
313 using namespace CeresEngine;
314 return CeresEngine::hash(property.value, property.texture, property.sampler, property.swizzle);
315 }
316};
317
319template<> struct std::hash<CeresEngine::PBRMaterialModel> {
320 size_t operator()(const CeresEngine::PBRMaterialModel& model) const {
321 using namespace CeresEngine;
322 return CeresEngine::hash(model.baseColor, model.roughness, model.metallic, model.normal, model.emission, model.displacement, model.ambientOcclusion);
323 }
324};
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
Represents a reflected C++ type. Can be used to get metadata from a C++ type.
Definition Type.hpp:32
Definition World.hpp:18
Definition Application.hpp:19
TextureType
Definition Texture.hpp:32
bool operator!=(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:416
bool operator==(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:411
@ Linear
Interpolate between multiple image samples.
MaterialNormalMapSpace
Specifies the kind of normal map used by a material.
Definition MaterialModel.hpp:244
@ Object
Specifies that the normal map is in object space.
@ Tangent
Specifies that the normal map is in tangent space.
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Image sampler descriptor structure.
Definition GPUSampler.hpp:61
GPUSamplerFilter minFilter
Minification filter. By default SamplerFilter::Nearest.
Definition GPUSampler.hpp:79
A material property with an associated value and texture.
Definition MaterialModel.hpp:155
MaterialProperty(ValueType value, TextureType texture, MaterialPropertySwizzle< ValueType > swizzle={})
Creates a new material property from both a raw value and an texture.
Definition MaterialModel.hpp:184
MaterialProperty & operator=(const MaterialProperty &)=default
ValueType value
The value associated with the material property.
Definition MaterialModel.hpp:157
MaterialProperty()=default
Creates a new empty material property.
MaterialProperty(MaterialProperty &&) noexcept=default
MaterialProperty(const MaterialProperty &)=default
TMatrix< 4, 4, ValueType > RawType
The raw type used to perform the swizzle.
Definition MaterialModel.hpp:89
constexpr MaterialPropertySwizzle()=default
Creates a new empty material property swizzle.
constexpr MaterialPropertySwizzle(TVector4< ValueType > r, TVector4< ValueType > g, TVector4< ValueType > b, TVector4< ValueType > a)
Creates a new material property swizzle that maps the channels given in r to the properties red chann...
Definition MaterialModel.hpp:103
constexpr MaterialPropertySwizzle(TVector4< ValueType > x, TVector4< ValueType > y, TVector4< ValueType > z)
Creates a new material property swizzle that maps the channels given in x to the properties X channel...
Definition MaterialModel.hpp:74
constexpr MaterialPropertySwizzle(TVector4< ValueType > x, TVector4< ValueType > y, TVector4< ValueType > z, TVector4< ValueType > w)
Creates a new material property swizzle that maps the channels given in x to the properties X channel...
Definition MaterialModel.hpp:80
TMatrix< D, 4, ValueType > RawType
The raw type used to perform the swizzle.
Definition MaterialModel.hpp:54
constexpr MaterialPropertySwizzle(TVector4< ValueType > x, TVector4< ValueType > y)
Creates a new material property swizzle that maps the channels given in x to the properties X channel...
Definition MaterialModel.hpp:68
constexpr MaterialPropertySwizzle()=default
Creates a new empty material property swizzle.
A structure that describes how channels of the input texture should be mapped in the material.
Definition MaterialModel.hpp:31
RawType raw
The vector that represents the swizzle operation.
Definition MaterialModel.hpp:36
constexpr MaterialPropertySwizzle()=default
Creates a new empty material property swizzle.
TVector4< ValueType > RawType
The raw type used to perform the swizzle.
Definition MaterialModel.hpp:33
A material model that represents a PBR surface.
Definition MaterialModel.hpp:256
Vector3MaterialProperty normal
A material channel that represents the normal map of the material.
Definition MaterialModel.hpp:268
DoubleMaterialProperty ambientOcclusion
The material ambient occlusion property.
Definition MaterialModel.hpp:277
DoubleMaterialProperty metallic
A material channel that represents the metallic map of the material.
Definition MaterialModel.hpp:265
ColorMaterialProperty baseColor
A material channel that represents the base color (or albedo) of the material.
Definition MaterialModel.hpp:259
ColorMaterialProperty emission
The material emission property.
Definition MaterialModel.hpp:271
DoubleMaterialProperty roughness
A material channel that represents the roughness map of the material.
Definition MaterialModel.hpp:262
Vector3MaterialProperty displacement
The material displacement property.
Definition MaterialModel.hpp:274