Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Material.h
Go to the documentation of this file.
1/**
2 * @file Material.h
3 * @ingroup mnd_render
4 * @brief Container that pairs a ShaderProgram with typed uniform parameters.
5 *
6 * A Material answers the question "how should this surface look?". It holds:
7 * - A ShaderProgram (the GLSL pipeline to execute).
8 * - Named float / vec2 / vec3 / vec4 parameters → uploaded as uniforms.
9 * - Named Texture parameters → bound to texture units before each draw.
10 *
11 * ## Typical per-frame usage
12 * @code
13 * material->SetParam("uTime", iTime);
14 * material->SetParam("uColor", 1.0f, 0.5f, 0.2f);
15 * material->SetParam("uAlbedo", myTexture);
16 * // Then submit a RenderCommand — RenderQueue calls Bind() internally.
17 * @endcode
18 *
19 * Material::Bind() uploads all stored params to the active ShaderProgram.
20 * It is called by RenderQueue::Draw() just before each draw call.
21 *
22 * @note Material does not own a Mesh. The two are coupled only at submit time
23 * through a RenderCommand. This allows the same material to be reused
24 * across different meshes without duplication.
25 *
26 * @see RenderCommand, RenderQueue, ShaderProgram, Texture
27 */
28
29#pragma once
30#include <memory>
31#include <string>
32#include <tuple>
33#include <unordered_map>
34#include <vector>
35
36#include <glm/mat4x4.hpp>
37
38#include "Types.h"
39#include "graphics/Texture.h"
40
41namespace mnd
42{
43class ShaderProgram;
44class Texture;
45
46/**
47 * @brief Shader + uniform storage that defines how a surface is rendered.
48 *
49 * Parameters are stored in typed maps and uploaded to the GPU lazily during
50 * Material::Bind(). Between frames, SetParam() calls simply overwrite the
51 * stored values without touching the GPU.
52 */
54{
55 public:
56 /**
57 * @brief Assign the GLSL program this material will use when drawn.
58 * @param shaderProgram Compiled shader; falls back to the default if unset.
59 */
60 void SetShaderProgram(const std::shared_ptr<ShaderProgram> &shaderProgram);
61
62 /// @name Uniform Parameter Setters
63 /// Store a named uniform value. Uploaded to the GPU in Bind().
64 /// @{
65 void SetParam(const std::string &name, float value); ///< Set a float uniform.
66 void SetParam(const std::string &name, float v0, float v1); ///< Set a vec2 uniform.
67 void SetParam(const std::string &name, const vec3 &value); ///< Set a vec3 uniform.
68 void SetParam(const std::string &name, const vec4 &value); ///< Set a vec4 uniform.
69 void SetParam(const std::string &name, const std::shared_ptr<Texture> &texture); ///< Set a sampler2D uniform.
70 void SetParam(const std::string &name, const std::vector<glm::mat4> &matrices); ///< Set a mat4[] uniform (e.g. bone palette).
71 /// @}
72
73 /**
74 * @brief Upload all stored parameters to the active ShaderProgram.
75 *
76 * Called automatically by RenderQueue::Draw() before each draw call.
77 * Binds textures to consecutive texture units starting from 0.
78 */
79 void Bind();
80
81 /// Returns the shader program assigned to this material.
83
84 /**
85 * @brief Load a material from a descriptor file.
86 * @param path Path to the material asset file.
87 * @return Shared pointer to the loaded Material.
88 */
89 static std::shared_ptr<Material> Load(const str &path);
90
91 private:
92 std::shared_ptr<ShaderProgram> m_shaderProgram; ///< GLSL program used for rendering.
93 std::unordered_map<std::string, float> m_floatParams; ///< float uniforms.
94 std::unordered_map<std::string, std::tuple<float, float>> m_float2Params; ///< vec2 uniforms.
95 std::unordered_map<std::string, vec3> m_float3Params; ///< vec3 uniforms.
96 std::unordered_map<std::string, vec4> m_float4Params; ///< vec4 uniforms.
97 std::unordered_map<std::string, std::shared_ptr<Texture>> m_textures; ///< Sampler2D uniforms.
98 std::unordered_map<std::string, std::vector<glm::mat4>> m_mat4ArrayParams; ///< mat4[] uniforms (bone palettes etc.).
99};
100
101} // namespace mnd
Engine-wide primitive type aliases and GLM math imports.
Shader + uniform storage that defines how a surface is rendered.
Definition Material.h:54
void Bind()
Upload all stored parameters to the active ShaderProgram.
Definition Material.cpp:176
void SetShaderProgram(const std::shared_ptr< ShaderProgram > &shaderProgram)
Assign the GLSL program this material will use when drawn.
Definition Material.cpp:19
void SetParam(const std::string &name, float value)
Set a float uniform.
Definition Material.cpp:24
ShaderProgram * GetShaderProgram()
Returns the shader program assigned to this material.
Definition Material.cpp:14
static std::shared_ptr< Material > Load(const str &path)
Load a material from a descriptor file.
Definition Material.cpp:54
Linked GLSL program with cached uniform locations.
glm::vec4 vec4
4-component float vector (e.g. RGBA colour, homogeneous coords).
Definition Types.h:52
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition Types.h:51
std::string str
Convenience alias for std::string.
Definition Types.h:45