diff options
Diffstat (limited to 'gfx/gl/sceneShader.cpp')
-rw-r--r-- | gfx/gl/sceneShader.cpp | 135 |
1 files changed, 58 insertions, 77 deletions
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 <array>
-#include <cstddef>
-#include <gfx/gl/programHandle.h>
-#include <gfx/gl/shaders/fs-basicShader.h>
-#include <gfx/gl/shaders/fs-landmassShader.h>
-#include <gfx/gl/shaders/fs-waterShader.h>
-#include <gfx/gl/shaders/vs-basicShader.h>
-#include <gfx/gl/shaders/vs-landmassShader.h>
-#include <gfx/gl/shaders/vs-waterShader.h>
-#include <glm/glm.hpp>
-#include <glm/gtc/type_ptr.hpp>
-#include <glm/gtx/transform.hpp>
-#include <location.hpp>
-#include <maths.h>
-
-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<std::size_t>(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 <array> +#include <gfx/gl/shaders/fs-basicShader.h> +#include <gfx/gl/shaders/fs-landmassShader.h> +#include <gfx/gl/shaders/fs-waterShader.h> +#include <gfx/gl/shaders/vs-basicShader.h> +#include <gfx/gl/shaders/vs-landmassShader.h> +#include <gfx/gl/shaders/vs-waterShader.h> +#include <glm/gtc/type_ptr.hpp> +#include <glm/gtx/transform.hpp> +#include <location.hpp> +#include <maths.h> + +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<const SceneProgram *, 4> {&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)); +} |