Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
material_types.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
8#include <string>
9#include <vector>
10#include <cstdint>
11#include <cstring>
12
13#define GLM_FORCE_RADIANS
14#define GLM_FORCE_DEPTH_ZERO_TO_ONE
15#include <glm/vec2.hpp>
16#include <glm/vec3.hpp>
17#include <glm/vec4.hpp>
18#include <glm/mat4x4.hpp>
19
20namespace rendering_engine
21{
22
23/**
24 * @brief Specifies the rendering domain where the material is used.
25 */
27{
30};
31
32/**
33 * @brief Specifies how the material handles transparency.
34 */
35enum class BlendMode
36{
37 Opaque,
39};
40
41/**
42 * @brief Specifies the shading model used by the material.
43 */
44enum class ShadingModel
45{
46 Lit,
47 Unlit
48};
49
50/**
51 * @brief Describes the layout of a single packed parameter inside a uniform buffer.
52 */
54{
55 std::string name;
56 size_t offset;
57 size_t size;
58 enum class Type { Float, Vec2, Vec3, Vec4 };
60};
61
62/**
63 * @brief Settings required to define a material instance.
64 */
66{
67 std::string parentMaterialName; // to reuse same shader code
68 std::string materialName;
72
73 const std::vector<MaterialParameterLayoutEntry>* parameterLayout = nullptr;
74};
75
76/**
77 * @brief Contains the raw buffer data and layout metadata of packed material parameters.
78 */
80{
81 std::vector<uint8_t> buffer;
82 std::vector<MaterialParameterLayoutEntry> layout;
83
84 void SetMaterialFloat(const std::string& name, float value)
85 {
86 for (const auto& entry : layout)
87 {
88 if (entry.name == name &&
90 {
91 std::memcpy(
92 buffer.data() + entry.offset,
93 &value,
94 sizeof(float)
95 );
96 return;
97 }
98 }
99 }
100
101 void SetMaterialVec2(const std::string& name, const glm::vec2& value)
102 {
103 for (const auto& entry : layout)
104 {
105 if (entry.name == name &&
107 {
108 std::memcpy(
109 buffer.data() + entry.offset,
110 &value,
111 sizeof(glm::vec2)
112 );
113 return;
114 }
115 }
116 }
117
118 void SetMaterialVec3(const std::string& name, const glm::vec3& value)
119 {
120 for (const auto& entry : layout)
121 {
122 if (entry.name == name &&
124 {
125 std::memcpy(
126 buffer.data() + entry.offset,
127 &value,
128 sizeof(glm::vec3)
129 );
130 return;
131 }
132 }
133 }
134
135 void SetMaterialVec4(const std::string& name, const glm::vec4& value)
136 {
137 for (const auto& entry : layout)
138 {
139 if (entry.name == name && entry.type == MaterialParameterLayoutEntry::Type::Vec4)
140 {
141 std::memcpy(
142 buffer.data() + entry.offset,
143 &value,
144 sizeof(glm::vec4)
145 );
146 return;
147 }
148 }
149 }
150
151};
152
153static const std::vector<MaterialParameterLayoutEntry> Font2DLayout =
154{
155 { "FontColor", 0, 16, MaterialParameterLayoutEntry::Type::Vec4 },
156 { "OutlineColor", 16, 16, MaterialParameterLayoutEntry::Type::Vec4 },
157 { "OutlineThicknessPx", 32, 4, MaterialParameterLayoutEntry::Type::Float },
158 { "InvAtlasSizeWidth", 36, 4, MaterialParameterLayoutEntry::Type::Float },
159 { "InvAtlasSizeHeight", 40, 4, MaterialParameterLayoutEntry::Type::Float }
160};
161
162static const std::vector<MaterialParameterLayoutEntry> Rectangle2DLayout =
163{
165};
166
167} // namespace rendering_engine
2D drawable component for rendering textured quad.
Definition: sprite_2d.hpp:22
static const std::vector< MaterialParameterLayoutEntry > Font2DLayout
static const std::vector< MaterialParameterLayoutEntry > Rectangle2DLayout
MaterialDomain
Specifies the rendering domain where the material is used.
BlendMode
Specifies how the material handles transparency.
ShadingModel
Specifies the shading model used by the material.
Describes the layout of a single packed parameter inside a uniform buffer.
Settings required to define a material instance.
const std::vector< MaterialParameterLayoutEntry > * parameterLayout
Contains the raw buffer data and layout metadata of packed material parameters.
void SetMaterialVec2(const std::string &name, const glm::vec2 &value)
std::vector< MaterialParameterLayoutEntry > layout
void SetMaterialFloat(const std::string &name, float value)
void SetMaterialVec3(const std::string &name, const glm::vec3 &value)
void SetMaterialVec4(const std::string &name, const glm::vec4 &value)