Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
ShaderProgram.cpp
Go to the documentation of this file.
1#include "graphics/ShaderProgram.h"
2
3#include "Log.h"
4#include "graphics/Texture.h"
5
6namespace mnd
7{
9 : m_shaderProgramID(shaderProgramID)
10{
11 LOG_INFO("ShaderProgram constructed (id=%u)", m_shaderProgramID);
12}
13
15{
16 LOG_INFO("ShaderProgram destroyed (id=%u)", m_shaderProgramID);
17 glDeleteProgram(m_shaderProgramID);
18}
19
21{
22 glUseProgram(m_shaderProgramID);
23 m_currentTextureUnit = 0;
24}
25
27{
28 auto iter = m_uniformLocationCache.find(name);
29 if (iter != m_uniformLocationCache.end()) {
30 return iter->second;
31 }
32 GLint location = glGetUniformLocation(m_shaderProgramID, name.c_str());
33 if (location == -1) {
34 LOG_WARN("Uniform '%s' not found in shader program %u", name.c_str(), m_shaderProgramID);
35 }
36 m_uniformLocationCache[name] = location;
37 return location;
38}
39
40void ShaderProgram::SetUniform(const std::string &name, float value)
41{
42 auto location = GetUniformLocation(name);
43 glUniform1f(location, value);
44}
45
46void ShaderProgram::SetUniform(const std::string &name, float v0, float v1)
47{
48 auto location = GetUniformLocation(name);
49 glUniform2f(location, v0, v1);
50}
51
52void ShaderProgram::SetUniform(const std::string &name, float v0, float v1, float v2)
53{
54 auto location = GetUniformLocation(name);
55 glUniform3f(location, v0, v1, v2);
56}
57
58void ShaderProgram::SetUniform(const std::string &name, float v0, float v1, float v2, float v3)
59{
60 auto location = GetUniformLocation(name);
61 glUniform4f(location, v0, v1, v2, v3);
62}
63
64void ShaderProgram::SetUniform(const std::string &name, const mat4 &mat)
65{
66 auto location = GetUniformLocation(name);
67 glUniformMatrix4fv(location, 1, GL_FALSE, value_ptr(mat));
68}
69
70void ShaderProgram::SetUniform(const std::string &name, const vec3 &value)
71{
72 auto location = GetUniformLocation(name);
73 glUniform3fv(location, 1, value_ptr(value));
74}
75
76void ShaderProgram::SetUniform(const std::string &name, const mat4 *matrices, std::size_t count)
77{
78 if (count == 0 || matrices == nullptr)
79 {
80 return;
81 }
82 auto location = GetUniformLocation(name);
83 glUniformMatrix4fv(location, static_cast<GLsizei>(count), GL_FALSE, value_ptr(matrices[0]));
84}
85
86void ShaderProgram::SetTexture(const std::string &name, Texture *texture)
87{
88 auto location = GetUniformLocation(name);
89
90 glActiveTexture(GL_TEXTURE0 + m_currentTextureUnit);
91 glBindTexture(GL_TEXTURE_2D, texture->GetID());
92 glUniform1i(location, m_currentTextureUnit);
93 ++m_currentTextureUnit;
94}
95
96} // namespace mnd
int GLint
Definition GLForward.h:13
int GLsizei
Definition GLForward.h:14
unsigned int GLuint
Definition GLForward.h:11
Console logging macros for all engine and game code.
#define LOG_WARN(fmt,...)
Definition Log.h:80
#define LOG_INFO(fmt,...)
Definition Log.h:79
void SetTexture(const std::string &name, Texture *texture)
Bind a texture to the next available texture unit and set the sampler uniform.
ShaderProgram()=delete
void Bind()
Activate this program for subsequent draw calls (glUseProgram).
void SetUniform(const std::string &name, float value)
Upload a float uniform.
~ShaderProgram()
Calls glDeleteProgram() to release the GPU resource.
GLint GetUniformLocation(const std::string &name)
Look up a uniform location, caching the result for future calls.
2D OpenGL texture object wrapping a GL_TEXTURE_2D handle.
Definition Texture.h:41
GLuint GetID() const
Returns the underlying OpenGL texture handle (GL_TEXTURE_2D target).
Definition Texture.cpp:71
glm::mat4 mat4
4×4 column-major float matrix (model/view/projection).
Definition Types.h:63
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition Types.h:51