summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
Diffstat (limited to 'gfx')
-rw-r--r--gfx/gl/shader.cpp38
-rw-r--r--gfx/gl/shader.h19
2 files changed, 41 insertions, 16 deletions
diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp
index 7fcdefe..979d57e 100644
--- a/gfx/gl/shader.cpp
+++ b/gfx/gl/shader.cpp
@@ -6,10 +6,12 @@
#include <stdexcept>
#include <string>
-Shader::Shader() : viewProjection_uniform {}, model_uniform {}, lightDir_uniform {}
+Shader::ProgramHandle::ProgramHandle(std::initializer_list<GLuint> srcs) :
+ viewProjection_uniform {}, model_uniform {}, lightDir_uniform {}
{
- glAttachShader(m_program, Source {{basicShader_vs, basicShader_vs_len}, GL_VERTEX_SHADER}.id);
- glAttachShader(m_program, Source {{basicShader_fs, basicShader_fs_len}, GL_FRAGMENT_SHADER}.id);
+ for (const auto & srcId : srcs) {
+ glAttachShader(m_program, srcId);
+ }
glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "texCoord");
@@ -26,24 +28,38 @@ Shader::Shader() : viewProjection_uniform {}, model_uniform {}, lightDir_uniform
lightDir_uniform = glGetUniformLocation(m_program, "lightDirection");
}
-void
-Shader::Bind() const
+Shader::Shader() :
+ programs {{{
+ Source {{basicShader_vs, basicShader_vs_len}, GL_VERTEX_SHADER}.id,
+ Source {{basicShader_fs, basicShader_fs_len}, GL_FRAGMENT_SHADER}.id,
+ }}}
{
- glUseProgram(m_program);
}
void
Shader::setView(glm::mat4 proj) const
{
- glUniformMatrix4fv(viewProjection_uniform, 1, GL_FALSE, &proj[0][0]);
- const glm::vec3 lightDir = glm::normalize(glm::vec3 {1, -1, 0});
- glUniform3fv(lightDir_uniform, 1, &lightDir[0]);
+ for (const auto & prog : programs) {
+ glUseProgram(prog.m_program);
+ glUniformMatrix4fv(prog.viewProjection_uniform, 1, GL_FALSE, &proj[0][0]);
+ }
+}
+
+void
+Shader::setLight(glm::vec3 lightDir) const
+{
+ for (const auto & prog : programs) {
+ glUseProgram(prog.m_program);
+ glUniform3fv(prog.lightDir_uniform, 1, &lightDir[0]);
+ }
}
void
-Shader::setModel(glm::mat4 model) const
+Shader::setModel(glm::mat4 model, Program pid) const
{
- glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &model[0][0]);
+ auto & prog = programs[(int)pid];
+ glUseProgram(prog.m_program);
+ glUniformMatrix4fv(prog.model_uniform, 1, GL_FALSE, &model[0][0]);
}
void
diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h
index a839b9b..98e0348 100644
--- a/gfx/gl/shader.h
+++ b/gfx/gl/shader.h
@@ -2,17 +2,20 @@
#define SHADER_INCLUDED_H
#include <GL/glew.h>
+#include <array>
#include <glRef.hpp>
#include <glm/glm.hpp>
#include <string_view>
class Shader {
public:
+ enum class Program { Basic = 0 };
+
Shader();
- void Bind() const;
void setView(glm::mat4 view) const;
- void setModel(glm::mat4 model) const;
+ void setModel(glm::mat4 model, Program = Program::Basic) const;
+ void setLight(glm::vec3 dir) const;
private:
class Source {
@@ -27,10 +30,16 @@ private:
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage);
- using ProgramRef = glRef<GLuint, __glewCreateProgram, __glewDeleteProgram>;
+ class ProgramHandle {
+ public:
+ ProgramHandle(std::initializer_list<GLuint>);
+ using ProgramRef = glRef<GLuint, __glewCreateProgram, __glewDeleteProgram>;
+
+ ProgramRef m_program;
+ GLint viewProjection_uniform, model_uniform, lightDir_uniform;
+ };
- ProgramRef m_program;
- GLint viewProjection_uniform, model_uniform, lightDir_uniform;
+ std::array<ProgramHandle, 1> programs;
};
#endif