GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Decals3D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_3D_DECALS_3D_HPP
5#define GREM_GRAPHICS_3D_DECALS_3D_HPP
6
8
13#include <GREM/core/math.hpp>
17
18#include <cstddef> // std::size_t
19#include <functional> // std::hash
20
21namespace grem::graphics {
22
23class Device; // Forward declaration, to avoid including Device.hpp.
24class Renderer3D; // Forward declaration, to avoid a circular include of Renderer3D.hpp.
25class Decals3D; // Forward declaration.
26
30struct DecalMaterialID {
31 [[nodiscard]] constexpr bool operator==(const DecalMaterialID&) const noexcept = default;
32
33private:
34 friend Renderer3D;
35 friend Decals3D;
36 friend std::hash<DecalMaterialID>;
37
38 constexpr explicit DecalMaterialID(uint32_t index) noexcept
39 : index(index) {}
40
41 uint32_t index;
42};
43
47struct DecalID {
51 constexpr DecalID() noexcept = default;
52
59 constexpr explicit operator bool() const noexcept {
60 return *this != DecalID{};
61 }
62
70 [[nodiscard]] constexpr bool operator==(const DecalID& other) const noexcept = default;
71
79 [[nodiscard]] constexpr std::strong_ordering operator<=>(const DecalID& other) const noexcept = default;
80
81private:
82 friend Renderer3D;
83 friend Decals3D;
84 friend std::hash<DecalID>;
85
86 constexpr explicit DecalID(uint64_t value) noexcept
87 : value(value) {}
88
89 uint64_t value = 0;
90};
91
170
178 vec3 position{0.0f, 0.0f, 0.0f};
179
184 float range = 0.5f;
185
189 quat orientation{0.0f, 0.0f, 0.0f, 1.0f};
190
194 vec2 size{1.0f, 1.0f};
195
201 vec2 origin{0.5f, 0.5f};
202
212
220 vec3 emissiveFactor{1.0f, 1.0f, 1.0f};
221
227
235 [[nodiscard]] constexpr bool operator==(const DecalOptions3D& other) const noexcept = default;
236};
237
249 [[nodiscard]] constexpr bool operator==(const Decals3DOptions& other) const noexcept = default;
250};
251
255class Decals3D {
256public:
262
264 using ParameterBuffer = UniformBuffer<Parameters, "Decals3DParameters">;
265
277 GREM_API(graphics_3d) explicit Decals3D(Device& device, const Decals3DOptions& options = {});
278
283 GREM_API(graphics_3d) void clearDecalMaterials() noexcept;
284
298 [[nodiscard]] GREM_API(graphics_3d) DecalMaterialID createDecalMaterial(const DecalMaterialOptions& options);
299
303 GREM_API(graphics_3d) void clearDecals() noexcept;
304
320 GREM_API(graphics_3d) DecalID createDecal(DecalMaterialID materialID, const DecalOptions3D& options);
321
331 [[nodiscard]] GREM_API(graphics_3d) bool containsDecal(DecalID id) const noexcept;
332
344 GREM_API(graphics_3d) bool destroyDecal(DecalID id);
345
354 GREM_API(graphics_3d) void setDecalPosition(DecalID id, vec3 newPosition);
355
365 GREM_API(graphics_3d) void setDecalRange(DecalID id, float newRange);
366
375 GREM_API(graphics_3d) void setDecalOrientation(DecalID id, quat newOrientation);
376
385 GREM_API(graphics_3d) void setDecalSize(DecalID id, vec2 newSize);
386
397 GREM_API(graphics_3d) void setDecalOrigin(DecalID id, vec2 newOrigin);
398
407 GREM_API(graphics_3d) void setDecalColor(DecalID id, Color newColor);
408
417 GREM_API(graphics_3d) void setDecalEmissiveFactor(DecalID id, vec3 newEmissiveFactor);
418
428 GREM_API(graphics_3d) void setDecalModelInstanceIdentifier(DecalID id, uint32_t newModelInstanceIdentifier);
429
435 void setOptions(const Decals3DOptions& newOptions) {
436 options = newOptions;
437 }
438
444 [[nodiscard]] Decals3DOptions getOptions() const noexcept {
445 return options;
446 }
447
469 auto forEachDecal(auto callback) {
470 constexpr bool CALLBACK_RETURNS_BOOL = convertible_to<decltype(callback(DecalID{})), bool>;
471 for (const auto& [decalID, decal] : decals) {
472 if constexpr (CALLBACK_RETURNS_BOOL) {
473 if (callback(decalID)) {
474 return true;
475 }
476 } else {
477 callback(decalID);
478 }
479 }
480 if constexpr (CALLBACK_RETURNS_BOOL) {
481 return false;
482 }
483 }
484
485private:
486 friend Renderer3D;
487
488 static constexpr uint32_t INITIAL_RESOLUTION = 1024;
489 static constexpr uint32_t PADDING = 8;
490 static constexpr uint32_t ALIGNMENT = 16;
491
492 struct DecalMaterial {
493 vec4 baseColorMapPositionAndSize;
494 vec4 normalMapPositionAndSize;
495 vec4 occlusionRoughnessMetallicMapPositionAndSize;
496 vec4 emissiveMapPositionAndSize;
497 vec4 baseColorFactor;
498 vec3 occlusionRoughnessMetallicFactor;
499 float normalScale;
500 vec3 emissiveFactor;
501 };
502
503 struct Decal {
504 DecalMaterialID materialID;
505 vec3 position;
506 float range;
507 quat orientation;
508 vec2 size;
509 vec2 origin;
510 Color color;
511 vec3 emissiveFactor;
512 uint32_t modelInstanceIdentifier;
513 };
514
515 [[nodiscard]] GREM_API(graphics_3d) vec2 getDefaultDecalBaseColorMapPosition();
516 [[nodiscard]] GREM_API(graphics_3d) vec2 getDefaultDecalNormalMapPosition();
517 [[nodiscard]] GREM_API(graphics_3d) vec2 getDefaultDecalOcclusionRoughnessMetallicMapPosition();
518 [[nodiscard]] GREM_API(graphics_3d) vec2 getDefaultDecalEmissiveMapPosition();
519
520 GREM_API(graphics_3d) void flush(Renderer3D& renderer3D) const;
521
522 Device* device;
523 [[no_unique_address]] Decals3DOptions options;
524 OrderedMap<DecalID, Decal> decals{};
525 uint64_t lastDecalIDValue = 0;
526 resource::AtlasPacker atlasPacker{{.initialResolution = INITIAL_RESOLUTION, .padding = PADDING, .alignment = ALIGNMENT}};
527 Texture atlasTexture{};
528 Optional<vec2> defaultBaseColorMapPosition{};
529 Optional<vec2> defaultNormalMapPosition{};
530 Optional<vec2> defaultOcclusionRoughnessMetallicMapPosition{};
531 Optional<vec2> defaultEmissiveMapPosition{};
532 Buffer<DecalMaterial> decalMaterials{};
533 mutable ParameterBuffer parameterBuffer{*device};
534 mutable bool parameterBufferDirty = true;
535};
536
537} // namespace grem::graphics
538
539template <>
540struct std::hash<grem::graphics::DecalMaterialID> {
541 [[nodiscard]] std::size_t operator()(const grem::graphics::DecalMaterialID& decalMaterialID) const {
542 return hasher(decalMaterialID.index);
543 }
544
545private:
546 [[no_unique_address]] std::hash<grem::uint32_t> hasher;
547};
548
549template <>
550struct std::hash<grem::graphics::DecalID> {
551 [[nodiscard]] std::size_t operator()(const grem::graphics::DecalID& decalID) const {
552 return hasher(decalID.value);
553 }
554
555private:
556 [[no_unique_address]] std::hash<grem::uint64_t> hasher;
557};
558
559#endif
Floating-point linear RGBA color in the Rec.
Definition Color.hpp:60
static const Color WHITE
Definition Color.hpp:215
Set of decals in 3D space.
Definition Decals3D.hpp:255
UniformBuffer< Parameters, "Decals3DParameters"> ParameterBuffer
Shader buffer for decal parameters.
Definition Decals3D.hpp:264
auto forEachDecal(auto callback)
Execute a callback function for each decal in the decal set.
Definition Decals3D.hpp:469
void clearDecals() noexcept
Remove all decals from the set.
void clearDecalMaterials() noexcept
Remove all decal materials and images from the set, and consequently all decals as well.
bool containsDecal(DecalID id) const noexcept
Check if a specific decal handle is still valid, meaning that the decal has not yet been destroyed.
void setDecalColor(DecalID id, Color newColor)
Update the tint color of a decal.
bool destroyDecal(DecalID id)
Destroy a decal and remove it from the set.
DecalID createDecal(DecalMaterialID materialID, const DecalOptions3D &options)
Create a decal.
void setDecalEmissiveFactor(DecalID id, vec3 newEmissiveFactor)
Update the emissive factor of a decal.
void setDecalModelInstanceIdentifier(DecalID id, uint32_t newModelInstanceIdentifier)
Update the model instance identifier of a decal.
void setDecalOrientation(DecalID id, quat newOrientation)
Update the rotation of a decal.
void setOptions(const Decals3DOptions &newOptions)
Set the configuration options of the decal set.
Definition Decals3D.hpp:435
Decals3DOptions getOptions() const noexcept
Get the configuration options of the decal set.
Definition Decals3D.hpp:444
DecalMaterialID createDecalMaterial(const DecalMaterialOptions &options)
Create a decal material.
void setDecalSize(DecalID id, vec2 newSize)
Update the size of a decal.
void setDecalPosition(DecalID id, vec3 newPosition)
Update the position of a decal.
void setDecalOrigin(DecalID id, vec2 newOrigin)
Update the origin of a decal.
void setDecalRange(DecalID id, float newRange)
Update the range of a decal.
Decals3D(Device &device, const Decals3DOptions &options={})
Construct a set of decals.
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
Persistent system that provides 3D rendering capabilities.
Definition Renderer3D.hpp:120
GPU memory buffer containing a fixed-size struct of uniform parameters to be read by a shader program...
Definition buffers.hpp:426
Read-only non-owning view over a 2D image.
Definition Image.hpp:108
Definition concepts.hpp:47
Definition concepts.hpp:243
Definition buffer_layouts.hpp:20
Definition aliases.hpp:9
qua< float > quat
Definition math.hpp:892
vec< 4, float > vec4
Definition math.hpp:440
vec< 2, float > vec2
Definition math.hpp:438
vec< 3, float > vec3
Definition math.hpp:439
static constexpr T MAX
Definition fundamentals.hpp:29
Opaque handle to a specific decal in a Decals3D set.
Definition Decals3D.hpp:47
constexpr bool operator==(const DecalID &other) const noexcept=default
Compare this handle to another for equality.
constexpr std::strong_ordering operator<=>(const DecalID &other) const noexcept=default
Compare this handle to another.
constexpr DecalID() noexcept=default
Construct an invalid decal handle.
Opaque identifier for a specific decal material in a Decals3D set.
Definition Decals3D.hpp:30
constexpr bool operator==(const DecalMaterialID &) const noexcept=default
Configuration options for a decal material.
Definition Decals3D.hpp:95
float occlusionStrength
Occlusion strength of the decal material.
Definition Decals3D.hpp:140
resource::ImageView emissiveMapImage
Emissive map image of the decal material, or an empty image view to use a fully white texture.
Definition Decals3D.hpp:130
bool convertToPremultipliedAlpha
Convert the decal base color map image from straight to pre-multiplied alpha.
Definition Decals3D.hpp:168
resource::ImageView occlusionRoughnessMetallicMapImage
Occlusion-roughness-metallic map image of the decal material, or an empty image view to use a fully w...
Definition Decals3D.hpp:121
float metallicFactor
Metallic factor of the decal material.
Definition Decals3D.hpp:150
float roughnessFactor
Roughness factor of the decal material.
Definition Decals3D.hpp:145
resource::ImageView baseColorMapImage
Base color map image of the decal material, or an empty image view to use a fully white texture.
Definition Decals3D.hpp:103
vec3 emissiveFactor
Emissive factor of the decal material.
Definition Decals3D.hpp:160
float normalScale
Normal scale of the decal material.
Definition Decals3D.hpp:155
vec4 baseColorFactor
Base color factor of the decal material.
Definition Decals3D.hpp:135
resource::ImageView normalMapImage
Normal map image of the decal material, or an empty image view to use a flat normal texture.
Definition Decals3D.hpp:112
Configuration options for a 3D decal.
Definition Decals3D.hpp:174
vec2 size
Size of the decal, in world coordinates.
Definition Decals3D.hpp:194
quat orientation
Rotation of the decal around its DecalOptions3D::origin.
Definition Decals3D.hpp:189
vec2 origin
Offset, in local vertex coordinates, specifying the origin relative to the bottom left of the decal.
Definition Decals3D.hpp:201
vec3 position
Position, in world coordinates, to render the decal at.
Definition Decals3D.hpp:178
vec3 emissiveFactor
Emissive factor to use in the shader.
Definition Decals3D.hpp:220
constexpr bool operator==(const DecalOptions3D &other) const noexcept=default
Compare these options to another set of options for equality.
uint32_t modelInstanceIdentifier
Model instance identifier that the decal applies to, or the maximum value to apply to all instances.
Definition Decals3D.hpp:226
Color color
Tint color to use in the shader.
Definition Decals3D.hpp:211
Configuration options for a Decals3D set.
Definition Decals3D.hpp:241
constexpr bool operator==(const Decals3DOptions &other) const noexcept=default
Compare these options to another set of options for equality.
Struct of shader parameters representing the decals.
Definition Decals3D.hpp:258
sampler2D decalsAtlasTexture
Sampler for the decal atlas texture.
Definition Decals3D.hpp:260
Reference to a sampler2D Texture that may be used as a parameter value type in a shader-read buffer.
Definition ParameterDescription.hpp:131
std::size_t operator()(const grem::graphics::DecalID &decalID) const
Definition Decals3D.hpp:551
std::size_t operator()(const grem::graphics::DecalMaterialID &decalMaterialID) const
Definition Decals3D.hpp:541