From 1c3e7e81049efefe8b54e4d4a719feeda995b474 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 24 Jan 2021 16:36:02 +0000 Subject: Remove the weird view/model/camera connectedness --- gfx/gl/shader.cpp | 21 +++++++++++---------- gfx/gl/shader.h | 9 ++++----- gfx/gl/shaders/basicShader.vs | 8 ++++---- gfx/gl/transform.cpp | 10 ---------- gfx/gl/transform.h | 10 ++++++---- 5 files changed, 25 insertions(+), 33 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index e59d431..31feb80 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -1,5 +1,4 @@ #include "shader.h" -#include "transform.h" #include #include #include @@ -7,7 +6,7 @@ #include #include -Shader::Shader() : mvp_uniform {}, normal_uniform {}, lightDir_uniform {} +Shader::Shader() : 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); @@ -22,8 +21,8 @@ Shader::Shader() : mvp_uniform {}, normal_uniform {}, lightDir_uniform {} glValidateProgram(m_program); CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program"); - mvp_uniform = glGetUniformLocation(m_program, "MVP"); - normal_uniform = glGetUniformLocation(m_program, "Normal"); + viewProjection_uniform = glGetUniformLocation(m_program, "viewProjection"); + model_uniform = glGetUniformLocation(m_program, "model"); lightDir_uniform = glGetUniformLocation(m_program, "lightDirection"); } @@ -34,14 +33,16 @@ Shader::Bind() const } void -Shader::Update(const Transform & transform, const Camera & camera) const +Shader::setView(glm::mat4 proj) const { - glm::mat4 MVP = transform.GetMVP(camera); - glm::mat4 Normal = transform.GetModel(); + glUniformMatrix4fv(viewProjection_uniform, 1, GL_FALSE, &proj[0][0]); + glUniform3f(lightDir_uniform, 0.0F, -1.0F, 0.0F); +} - glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, &MVP[0][0]); - glUniformMatrix4fv(normal_uniform, 1, GL_FALSE, &Normal[0][0]); - glUniform3f(lightDir_uniform, 0.0F, 0.0F, 1.0F); +void +Shader::setModel(glm::mat4 model) const +{ + glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &model[0][0]); } void diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index e253f50..a839b9b 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -3,17 +3,16 @@ #include #include +#include #include -class Camera; -class Transform; - class Shader { public: Shader(); void Bind() const; - void Update(const Transform & transform, const Camera & camera) const; + void setView(glm::mat4 view) const; + void setModel(glm::mat4 model) const; private: class Source { @@ -31,7 +30,7 @@ private: using ProgramRef = glRef; ProgramRef m_program; - GLint mvp_uniform, normal_uniform, lightDir_uniform; + GLint viewProjection_uniform, model_uniform, lightDir_uniform; }; #endif diff --git a/gfx/gl/shaders/basicShader.vs b/gfx/gl/shaders/basicShader.vs index e673548..e697315 100644 --- a/gfx/gl/shaders/basicShader.vs +++ b/gfx/gl/shaders/basicShader.vs @@ -7,12 +7,12 @@ attribute vec3 normal; varying vec2 texCoord0; varying vec3 normal0; -uniform mat4 MVP; -uniform mat4 Normal; +uniform mat4 viewProjection; +uniform mat4 model; void main() { - gl_Position = MVP * vec4(position, 1.0); + gl_Position = viewProjection * model * vec4(position, 1.0); texCoord0 = texCoord; - normal0 = (Normal * vec4(normal, 0.0)).xyz; + normal0 = (model * vec4(normal, 0.0)).xyz; } diff --git a/gfx/gl/transform.cpp b/gfx/gl/transform.cpp index 7b256af..b67bfaf 100644 --- a/gfx/gl/transform.cpp +++ b/gfx/gl/transform.cpp @@ -1,5 +1,4 @@ #include "transform.h" -#include "camera.h" #include Transform::Transform(glm::vec3 pos, glm::vec3 rot, glm::vec3 scale) : pos {pos}, rot {rot}, scale {scale} { } @@ -16,12 +15,3 @@ Transform::GetModel() const return posMat * rotMat * scaleMat; } - -glm::mat4 -Transform::GetMVP(const Camera & camera) const -{ - const auto VP = camera.GetViewProjection(); - const auto M = GetModel(); - - return VP * M; -} diff --git a/gfx/gl/transform.h b/gfx/gl/transform.h index 70fe38f..f8d8d0d 100644 --- a/gfx/gl/transform.h +++ b/gfx/gl/transform.h @@ -3,16 +3,12 @@ #include -class Camera; - class Transform { public: explicit Transform(glm::vec3 pos = {}, glm::vec3 rot = {}, glm::vec3 scale = {1.0F, 1.0F, 1.0F}); [[nodiscard]] glm::mat4 GetModel() const; - [[nodiscard]] glm::mat4 GetMVP(const Camera & camera) const; - [[nodiscard]] inline glm::vec3 & GetPos() { @@ -31,6 +27,12 @@ public: return rot; } + [[nodiscard]] inline const glm::vec3 & + GetRot() const + { + return rot; + } + [[nodiscard]] inline glm::vec3 & GetScale() { -- cgit v1.2.3