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 --- application/main.cpp | 5 +++-- game/physical.cpp | 5 +++-- game/physical.h | 3 +-- 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 ++++++---- 8 files changed, 32 insertions(+), 39 deletions(-) diff --git a/application/main.cpp b/application/main.cpp index 63ceac0..015f128 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -68,6 +68,8 @@ public: Shader shader; Camera camera({0.0F, 0.0F, -5.0F}, 70.0F, (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.1F, 100.0F); + shader.Bind(); + shader.setView(camera.GetViewProjection()); SDL_Event e; bool isRunning = true; @@ -87,8 +89,7 @@ public: world.apply(&WorldObject::tick, t_passed); windows.apply(&Window::Clear, 0.0F, 0.0F, 0.0F, 1.0F); - shader.Bind(); - world.apply(&Physical::render, shader, camera); + world.apply(&Physical::render, shader); windows.apply(&Window::SwapBuffers); if (const auto snz = framelen - t_passed; snz.count() > 0) { diff --git a/game/physical.cpp b/game/physical.cpp index 5263268..17d85dd 100644 --- a/game/physical.cpp +++ b/game/physical.cpp @@ -3,6 +3,7 @@ #include "gfx/models/texture.h" #include #include +#include Cache Physical::cachedMesh; Cache Physical::cachedTexture; @@ -13,9 +14,9 @@ Physical::Physical(glm::vec3 where, const std::string & m, const std::string & t } void -Physical::render(const Shader & shader, const Camera & camera) const +Physical::render(const Shader & shader) const { - shader.Update(location, camera); + shader.setModel(location.GetModel()); texture->Bind(); mesh->Draw(); } diff --git a/game/physical.h b/game/physical.h index ad2207d..5f624b0 100644 --- a/game/physical.h +++ b/game/physical.h @@ -6,7 +6,6 @@ #include #include -class Camera; class Shader; class Mesh; class Texture; @@ -16,7 +15,7 @@ class Physical { public: Physical(glm::vec3 where, const std::string & m, const std::string & t); - void render(const Shader & shader, const Camera & camera) const; + void render(const Shader & shader) const; [[nodiscard]] const auto & getPosition() const 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