From c3eea71370eb94cff1fd96185458643fab6eb2c5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 3 Nov 2022 19:47:46 +0000 Subject: Restructure how shaders are worked with Needs a tidy-up --- gfx/gl/sceneShader.cpp | 135 +++++++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 77 deletions(-) (limited to 'gfx/gl/sceneShader.cpp') diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index 48c0f42..bec6553 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -1,77 +1,58 @@ -#include "sceneShader.h" -#include "gfx/gl/glSource.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -SceneShader::ProgramHandle::ProgramHandle(GLuint vs, GLuint fs) : ProgramHandleBase {vs, fs} -{ - glBindAttribLocation(m_program, 0, "position"); - glBindAttribLocation(m_program, 1, "texCoord"); - glBindAttribLocation(m_program, 2, "normal"); - - viewProjection_uniform = glGetUniformLocation(m_program, "viewProjection"); - model_uniform = glGetUniformLocation(m_program, "model"); -} - -SceneShader::SceneShader() : - programs {{{ - basicShader_vs.compile(), - basicShader_fs.compile(), - }, - { - waterShader_vs.compile(), - waterShader_fs.compile(), - }, - { - landmassShader_vs.compile(), - landmassShader_fs.compile(), - }, - { - landmassShader_vs.compile(), - basicShader_fs.compile(), - }}} -{ -} - -void -SceneShader::setView(glm::mat4 proj) const -{ - for (const auto & prog : programs) { - glUseProgram(prog.m_program); - glUniformMatrix4fv(prog.viewProjection_uniform, 1, GL_FALSE, glm::value_ptr(proj)); - } -} - -void -SceneShader::setUniform(const GLchar * uniform, glm::vec3 v) const -{ - for (const auto & prog : programs) { - if (auto loc = glGetUniformLocation(prog.m_program, uniform); loc >= 0) { - glUseProgram(prog.m_program); - glUniform3fv(loc, 1, glm::value_ptr(v)); - } - } -} - -void -SceneShader::setModel(const Location & loc, Program pid) const -{ - auto & prog = programs[static_cast(pid)]; - glUseProgram(prog.m_program); - if (prog.model_uniform >= 0) { - const auto model {glm::translate(loc.pos) * rotate_ypr(loc.rot)}; - glUniformMatrix4fv(prog.model_uniform, 1, GL_FALSE, glm::value_ptr(model)); - } -} +#include "sceneShader.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +SceneShader::SceneShader() : + landmass {landmassShader_vs, landmassShader_fs}, absolute {landmassShader_vs, basicShader_fs} +{ +} + +void +SceneShader::setView(glm::mat4 proj) const +{ + for (const auto & prog : std::array {&basic, &water, &landmass, &absolute}) { + prog->setView(proj); + } +} + +void +SceneShader::SceneProgram::setView(const glm::mat4 & viewProjection) const +{ + glUseProgram(*this); + glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection)); +} + +SceneShader::BasicProgram::BasicProgram() : SceneProgram {basicShader_vs, basicShader_fs}, modelLoc {*this, "model"} { } + +void +SceneShader::BasicProgram::setModel(Location const & location) const +{ + const auto model {glm::translate(location.pos) * rotate_ypr(location.rot)}; + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); +} + +void +SceneShader::BasicProgram::use(Location const & location) const +{ + Program::use(); + setModel(location); +} + +SceneShader::WaterProgram::WaterProgram() : SceneProgram {waterShader_vs, waterShader_fs}, waveLoc {*this, "waves"} { } + +void +SceneShader::WaterProgram::use(float waveCycle) const +{ + Program::use(); + glm::vec3 waves {waveCycle, 0.F, 0.F}; + glUniform3fv(waveLoc, 1, glm::value_ptr(waves)); +} -- cgit v1.2.3