Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
material.h
1#ifndef OE_SCENE_MATERIAL_H
2#define OE_SCENE_MATERIAL_H
3
4#include "../common.h"
5
6#include <memory>
7#include <vector>
8#include <string>
9#include <map>
10#include <glm/mat4x4.hpp>
11
12namespace oe::render
13{
14 class Texture;
15 class ShaderBase;
16}
17
18namespace oe::scene
19{
26 {
27 public:
33 enum class DrawMode : uint8_t
34 {
36 TRIANGLES = 0x04,
37
39 TRIANGLE_STRIP = 0x05,
40
46 TRIANGLE_FAN = 0x06,
47
49 POINTS = 0x00,
50
52 LINES = 0x01,
53 };
54
61 {
69 float transmission = 0.0f;
70
76 float thickness = 0.0f;
77
78 float attenuation_distance = 0.0f;
79 glm::vec3 attenuation_color = glm::vec3(1.0f);
80 };
81
86
92 uint32_t flags = 0;
93
97 std::shared_ptr<oe::render::ShaderBase> shader = nullptr;
98
102 bool double_sided = false;
103
107 bool invert_faces = false;
108
115 glm::vec2 texture_tiling = glm::vec2(1.f);
116
123 glm::vec2 texture_offset = glm::vec2(0.f);
124
128 bool is_blend = false;
129
134
138 float alpha_threshold = 0.5f;
139
143 Material* parent = nullptr;
144
148 bool inherit_properties = false;
149
153 bool inherit_textures = false;
154
162 void setTexture(const std::string& name, std::shared_ptr<oe::render::Texture> texture, const int32_t layer = -1);
163
167 std::shared_ptr<oe::render::Texture> getTexture(const uint32_t& layer) const noexcept;
168
177 uint32_t getNextAvailableTextureLayer(const uint32_t start = 0) const;
178
183 const auto& getTextures() const
184 {
185 return _textures;
186 }
187
193 template <typename T>
194 Material* setProperty(const std::string& name, const T& value) = delete;
195
201 template <typename T>
202 Material* setArray(const std::string& name, const std::vector<T>& value) = delete;
203
204 private:
205 // todo for _textures : std::map<std::pair<TextureSettings, share<Texture>>
206 // TextureSettings would contain (among others?): scale, offset and rot
207 // todo² : see how to populate
208 std::map<std::string, std::pair<uint32_t, std::shared_ptr<oe::render::Texture>>> _textures;
209 std::map<uint32_t, std::shared_ptr<oe::render::Texture>> _textures_layers;
210
211 friend class oe::render::ShaderBase;
212
213 // Generate all material's properties containers
214 #define OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(TYPE, PROPERTY) \
215 std::map<const std::string, TYPE> _prop_ ## PROPERTY; \
216 std::map<const std::string, std::vector<TYPE>> _prop_ ## PROPERTY ## _arrays; \
217
218 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(bool, bool)
219 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(int32_t, int32_t)
220 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(uint32_t, uint32_t)
221 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(float, float)
222 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(double, double)
223 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(glm::ivec2, ivec2)
224 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(glm::vec2, vec2)
225 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(glm::vec3, vec3)
226 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(glm::vec4, vec4)
227 OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY(glm::mat4, mat4)
228
229 #undef OE_ENGINE_SCENE_MATERIAL_GENERATE_PROPERTY
230 };
231
238 {
240 glm::vec4 albedoColor = glm::vec4(1.0);
241
243 glm::vec3 emissiveColor = glm::vec4(1.0);
244
246 glm::vec3 rouMetAoColor = glm::vec3(1.0);
247
249 std::shared_ptr<oe::render::Texture> albedoTexture;
250
252 std::shared_ptr<oe::render::Texture> emissiveTexture;
253
255 std::shared_ptr<oe::render::Texture> normalTexture;
256
258 std::shared_ptr<oe::render::Texture> rouMetAoTexture;
259 };
260
261 #define OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(TYPE, PROPERTY) \
262 template <> inline Material* Material::setProperty(const std::string& name, const TYPE& value) \
263 { \
264 _prop_ ## PROPERTY[name] = value; \
265 return this; \
266 } \
267 template <> inline Material* Material::setArray(const std::string& name, const std::vector<TYPE>& value) \
268 { \
269 _prop_ ## PROPERTY ## _arrays[name] = value; \
270 return this; \
271 }
272
273 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(int32_t, int32_t)
274 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(uint32_t, uint32_t)
275 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(float, float)
276 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(double, double)
277 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(glm::ivec2, ivec2)
278 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(glm::vec2, vec2)
279 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(glm::vec3, vec3)
280 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(glm::vec4, vec4)
281 OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER(glm::mat4, mat4)
282
283 template <> inline Material* Material::setProperty(const std::string& name, const size_t& value)
284 {
285 return setProperty<int32_t>(name, value);
286 }
287
288 // Bool specialization, should be treated as an int
289 template <> inline Material* Material::setProperty(const std::string& name, const bool& value)
290 {
291 return setProperty<uint32_t>(name, value);
292 }
293
294 template <> inline Material* Material::setArray(const std::string& name, const std::vector<bool>& values)
295 {
296 std::vector<uint32_t> result(values.begin(), values.end());
297 return setArray<uint32_t>(name, result);
298 }
299
300 #undef OE_ENGINE_SCENE_MATERIAL_GENERATE_SETTER
301}
302
303#endif
Shader class.
Definition shader_base.h:33
Render agnostic material.
Definition material.h:26
bool double_sided
Draws two sides of faces.
Definition material.h:102
void setTexture(const std::string &name, std::shared_ptr< oe::render::Texture > texture, const int32_t layer=-1)
Set Texture at specified layer.
Material * parent
Material source where properties/textures should be inherited from.
Definition material.h:143
const auto & getTextures() const
Get all material textures.
Definition material.h:183
uint32_t getNextAvailableTextureLayer(const uint32_t start=0) const
Get an available texture layer for this material.
bool inherit_properties
Check if Material properties should be inherited from parent Material.
Definition material.h:148
glm::vec2 texture_tiling
Apply a tiling to the texture UV.
Definition material.h:115
float alpha_threshold
Texture with alpha below this value will be considered completely transparent.
Definition material.h:138
Material * setProperty(const std::string &name, const T &value)=delete
Set a property on a material.
uint32_t flags
General purpose user flags, may be used for collisions / sounds / etc...
Definition material.h:92
std::shared_ptr< oe::render::Texture > getTexture(const uint32_t &layer) const noexcept
Get Texture stored at specified layer.
std::shared_ptr< oe::render::ShaderBase > shader
Shader to use to render this material.
Definition material.h:97
Material * setArray(const std::string &name, const std::vector< T > &value)=delete
Set a array of properties on a material.
TransmissiveProperties transmissive_properties
Physically-based transmission properties.
Definition material.h:133
bool is_blend
This material need blending (transparency, effects, ...)
Definition material.h:128
DrawMode draw_mode
Describe how the mesh should be drawn.
Definition material.h:85
bool invert_faces
If not double sided, draws back faces instead of front ones.
Definition material.h:107
glm::vec2 texture_offset
Apply an offset to the texture UV.
Definition material.h:123
bool inherit_textures
Check if Material textures should be inherited from parent Material.
Definition material.h:153
DrawMode
Specify how the mesh vertices will be rendered.
Definition material.h:34
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures)
Definition opengl.h:10
Scene related management (Render-agnostic Geometry, Manger, etc...)
Definition debug.h:19
Material properties related to physically-based transparency related effects.
Definition material.h:61
float transmission
Percentage of light transmitted through the surface between 0.0f (opaque) and 1.0f (transparent)
Definition material.h:69
float thickness
Thickness of the volume of the material between 0.0f (thin) and +Infinite (transparent)
Definition material.h:76
Common properties/textures tied to a PBR material.
Definition material.h:238
glm::vec3 emissiveColor
Definition material.h:243
std::shared_ptr< oe::render::Texture > rouMetAoTexture
Definition material.h:258
glm::vec3 rouMetAoColor
Definition material.h:246
std::shared_ptr< oe::render::Texture > albedoTexture
Definition material.h:249
glm::vec4 albedoColor
Definition material.h:240
std::shared_ptr< oe::render::Texture > normalTexture
Definition material.h:255
std::shared_ptr< oe::render::Texture > emissiveTexture
Definition material.h:252