3#include "render/Material.h"
5#include <nlohmann/json.hpp>
9#include "graphics/ShaderProgram.h"
16 return m_shaderProgram.get();
21 m_shaderProgram = shaderProgram;
26 m_floatParams[name] = value;
31 m_float2Params[name] = {v0, v1};
36 m_float3Params[name] = value;
41 m_float4Params[name] = value;
46 m_textures[name] = texture;
51 m_mat4ArrayParams[name] = matrices;
59 LOG_ERROR(
"Material::Load empty or missing file '%s'", path.c_str());
66 json = nlohmann::json::parse(contents);
67 }
catch (
const nlohmann::json::parse_error &e)
69 LOG_ERROR(
"Material::Load JSON parse error in '%s': %s", path.c_str(), e.what());
72 std::shared_ptr<Material> result;
74 if (json.contains(
"shader"))
76 auto shaderObj = json[
"shader"];
77 str vertexPath = shaderObj.value(
"vertex",
"");
78 str fragmentPath = shaderObj.value(
"fragment",
"");
82 auto fragmentSource = fs.LoadAssetFileText(fragmentPath);
90 "Material::Load shader program creation failed (vert='%s' frag='%s')",
97 result = std::make_shared<Material>();
98 result->SetShaderProgram(shaderProgram);
99 result->SetParam(
"color",
vec3(1.0F));
102 if (json.contains(
"params"))
104 auto paramsObj = json[
"params"];
107 if (paramsObj.contains(
"float"))
109 for (
auto ¶m : paramsObj[
"float"])
111 str name = param.value(
"name",
"");
112 f32 value = param.value(
"value", 0.0f);
113 result->SetParam(name, value);
118 if (paramsObj.contains(
"float2"))
120 for (
auto ¶m : paramsObj[
"float2"])
122 str name = param.value(
"name",
"");
123 f32 v0 = param.value(
"value0", 0.0F);
124 f32 v1 = param.value(
"value1", 0.0F);
126 result->SetParam(name, v0, v1);
131 if (paramsObj.contains(
"float3"))
133 for (
auto ¶m : paramsObj[
"float3"])
135 str name = param.value(
"name",
"");
136 f32 v0 = param.value(
"value0", 0.0F);
137 f32 v1 = param.value(
"value1", 0.0F);
138 f32 v2 = param.value(
"value2", 0.0F);
140 result->SetParam(name,
vec3(v0, v1, v2));
145 if (paramsObj.contains(
"float4"))
147 for (
auto ¶m : paramsObj[
"float4"])
149 str name = param.value(
"name",
"");
150 f32 v0 = param.value(
"value0", 0.0F);
151 f32 v1 = param.value(
"value1", 0.0F);
152 f32 v2 = param.value(
"value2", 0.0F);
153 f32 v3 = param.value(
"value3", 0.0F);
155 result->SetParam(name,
vec4(v0, v1, v2, v3));
160 if (paramsObj.contains(
"textures"))
162 for (
auto ¶m : paramsObj[
"textures"])
164 str name = param.value(
"name",
"");
165 str texPath = param.value(
"path",
"");
168 result->SetParam(name, texture);
178 if (!m_shaderProgram)
180 LOG_WARN(
"Material::Bind with no shader program set");
183 m_shaderProgram->Bind();
185 for (
const auto ¶m : m_floatParams)
187 m_shaderProgram->SetUniform(param.first, param.second);
189 for (
const auto ¶m : m_float2Params)
191 auto &v = param.second;
192 m_shaderProgram->SetUniform(param.first, std::get<0>(v), std::get<1>(v));
194 for (
const auto ¶m : m_float3Params)
196 auto &v = param.second;
197 m_shaderProgram->SetUniform(param.first, param.second);
199 for (
const auto ¶m : m_float4Params)
201 auto &v = param.second;
202 m_shaderProgram->SetUniform(param.first, param.second);
204 for (
const auto &tex : m_textures)
206 m_shaderProgram->SetTexture(tex.first, tex.second.get());
208 for (
const auto ¶m : m_mat4ArrayParams)
210 m_shaderProgram->SetUniform(param.first, param.second.data(), param.second.size());
Console logging macros for all engine and game code.
#define LOG_WARN(fmt,...)
#define LOG_ERROR(fmt,...)
GraphicsAPI & GetGraphicsAPI()
Returns the low-level OpenGL wrapper used for all draw calls.
FileSystem & GetFileSystem()
Returns the file system helper for asset-relative path resolution.
static Engine & GetInstance()
Returns the single Engine instance (created on first call).
std::string LoadAssetFileText(const std::string &relativePath)
Load a text file from the assets directory into a string.
std::shared_ptr< ShaderProgram > CreateShaderProgram(const std::string &vertexSource, const std::string &fragmentSource)
Compile vertex + fragment GLSL source and link into a program.
void Bind()
Upload all stored parameters to the active ShaderProgram.
void SetShaderProgram(const std::shared_ptr< ShaderProgram > &shaderProgram)
Assign the GLSL program this material will use when drawn.
void SetParam(const std::string &name, float value)
Set a float uniform.
ShaderProgram * GetShaderProgram()
Returns the shader program assigned to this material.
static std::shared_ptr< Material > Load(const str &path)
Load a material from a descriptor file.
Linked GLSL program with cached uniform locations.
static std::shared_ptr< Texture > Load(const std::string &path)
Load a texture from a file path using stb_image.
glm::vec4 vec4
4-component float vector (e.g. RGBA colour, homogeneous coords).
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
std::string str
Convenience alias for std::string.
float f32
32-bit IEEE float — the standard GL scalar type.