Rendering Engine
0.2.9
Modular Graphics Rendering Engine | v0.2.9
Loading...
Searching...
No Matches
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
20
namespace
rendering_engine
21
{
22
23
/**
24
* @brief Specifies the rendering domain where the material is used.
25
*/
26
enum class
MaterialDomain
27
{
28
Surface3D
,
29
Sprite2D
30
};
31
32
/**
33
* @brief Specifies how the material handles transparency.
34
*/
35
enum class
BlendMode
36
{
37
Opaque
,
38
Translucent
39
};
40
41
/**
42
* @brief Specifies the shading model used by the material.
43
*/
44
enum 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
*/
53
struct
MaterialParameterLayoutEntry
54
{
55
std::string
name
;
56
size_t
offset
;
57
size_t
size
;
58
enum class
Type
{
Float
,
Vec2
,
Vec3
,
Vec4
};
59
Type
type
;
60
};
61
62
/**
63
* @brief Settings required to define a material instance.
64
*/
65
struct
MaterialSettings
66
{
67
std::string
parentMaterialName
;
// to reuse same shader code
68
std::string
materialName
;
69
MaterialDomain
materialDomain
;
70
BlendMode
blendMode
;
71
ShadingModel
shadingModel
;
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
*/
79
struct
PackedMaterialData
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 &&
89
entry.type ==
MaterialParameterLayoutEntry::Type::Float
)
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 &&
106
entry.type ==
MaterialParameterLayoutEntry::Type::Vec2
)
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 &&
123
entry.type ==
MaterialParameterLayoutEntry::Type::Vec3
)
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
153
static
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
162
static
const
std::vector<MaterialParameterLayoutEntry>
Rectangle2DLayout
=
163
{
164
{
"Color"
, 0, 16,
MaterialParameterLayoutEntry::Type::Vec4
}
165
};
166
167
}
// namespace rendering_engine
rendering_engine::Sprite2D
2D drawable component for rendering textured quad.
Definition
sprite_2d.hpp:22
rendering_engine
Definition
actor.hpp:13
rendering_engine::Font2DLayout
static const std::vector< MaterialParameterLayoutEntry > Font2DLayout
Definition
material_types.hpp:153
rendering_engine::Rectangle2DLayout
static const std::vector< MaterialParameterLayoutEntry > Rectangle2DLayout
Definition
material_types.hpp:162
rendering_engine::MaterialDomain
MaterialDomain
Specifies the rendering domain where the material is used.
Definition
material_types.hpp:27
rendering_engine::MaterialDomain::Surface3D
@ Surface3D
rendering_engine::BlendMode
BlendMode
Specifies how the material handles transparency.
Definition
material_types.hpp:36
rendering_engine::BlendMode::Translucent
@ Translucent
rendering_engine::BlendMode::Opaque
@ Opaque
rendering_engine::ShadingModel
ShadingModel
Specifies the shading model used by the material.
Definition
material_types.hpp:45
rendering_engine::ShadingModel::Unlit
@ Unlit
rendering_engine::ShadingModel::Lit
@ Lit
rendering_engine::MaterialParameterLayoutEntry
Describes the layout of a single packed parameter inside a uniform buffer.
Definition
material_types.hpp:54
rendering_engine::MaterialParameterLayoutEntry::offset
size_t offset
Definition
material_types.hpp:56
rendering_engine::MaterialParameterLayoutEntry::size
size_t size
Definition
material_types.hpp:57
rendering_engine::MaterialParameterLayoutEntry::name
std::string name
Definition
material_types.hpp:55
rendering_engine::MaterialParameterLayoutEntry::type
Type type
Definition
material_types.hpp:59
rendering_engine::MaterialParameterLayoutEntry::Type
Type
Definition
material_types.hpp:58
rendering_engine::MaterialParameterLayoutEntry::Type::Vec3
@ Vec3
rendering_engine::MaterialParameterLayoutEntry::Type::Float
@ Float
rendering_engine::MaterialParameterLayoutEntry::Type::Vec4
@ Vec4
rendering_engine::MaterialParameterLayoutEntry::Type::Vec2
@ Vec2
rendering_engine::MaterialSettings
Settings required to define a material instance.
Definition
material_types.hpp:66
rendering_engine::MaterialSettings::materialName
std::string materialName
Definition
material_types.hpp:68
rendering_engine::MaterialSettings::materialDomain
MaterialDomain materialDomain
Definition
material_types.hpp:69
rendering_engine::MaterialSettings::shadingModel
ShadingModel shadingModel
Definition
material_types.hpp:71
rendering_engine::MaterialSettings::parentMaterialName
std::string parentMaterialName
Definition
material_types.hpp:67
rendering_engine::MaterialSettings::blendMode
BlendMode blendMode
Definition
material_types.hpp:70
rendering_engine::MaterialSettings::parameterLayout
const std::vector< MaterialParameterLayoutEntry > * parameterLayout
Definition
material_types.hpp:73
rendering_engine::PackedMaterialData
Contains the raw buffer data and layout metadata of packed material parameters.
Definition
material_types.hpp:80
rendering_engine::PackedMaterialData::SetMaterialVec2
void SetMaterialVec2(const std::string &name, const glm::vec2 &value)
Definition
material_types.hpp:101
rendering_engine::PackedMaterialData::buffer
std::vector< uint8_t > buffer
Definition
material_types.hpp:81
rendering_engine::PackedMaterialData::layout
std::vector< MaterialParameterLayoutEntry > layout
Definition
material_types.hpp:82
rendering_engine::PackedMaterialData::SetMaterialFloat
void SetMaterialFloat(const std::string &name, float value)
Definition
material_types.hpp:84
rendering_engine::PackedMaterialData::SetMaterialVec3
void SetMaterialVec3(const std::string &name, const glm::vec3 &value)
Definition
material_types.hpp:118
rendering_engine::PackedMaterialData::SetMaterialVec4
void SetMaterialVec4(const std::string &name, const glm::vec4 &value)
Definition
material_types.hpp:135
RenderingEngine
RenderingLibrary
Include
material_types.hpp
Generated by
1.9.8