From fea6b87139cd85f59737e66553a31a8fd1aec2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Sun, 17 Oct 2021 16:27:29 +0200 Subject: [PATCH] Added fog to terrain renderer and renamed variables --- src/graphics/core/renderers.h | 3 ++ src/graphics/engine/engine.cpp | 4 ++ src/graphics/opengl/gl33renderers.cpp | 44 +++++++++++-------- src/graphics/opengl/gl33renderers.h | 23 ++++++---- .../opengl/shaders/gl33/terrain_fs.glsl | 8 ++++ 5 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/graphics/core/renderers.h b/src/graphics/core/renderers.h index 12b67cad..bc9200d2 100644 --- a/src/graphics/core/renderers.h +++ b/src/graphics/core/renderers.h @@ -97,6 +97,9 @@ public: //! Sets light parameters virtual void SetLight(const glm::vec4& position, const float& intensity, const glm::vec3& color) = 0; + //! Sets fog parameters + virtual void SetFog(float min, float max, const glm::vec3& color) = 0; + //! Draws terrain object virtual void DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer) = 0; }; diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 010c5fa2..93133d75 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -3393,6 +3393,10 @@ void CEngine::Draw3DScene() terrainRenderer->SetShadowMap(m_shadowMap); terrainRenderer->SetLight(glm::vec4(1.0, 1.0, -1.0, 0.0), 1.0f, glm::vec3(1.0)); + Color fogColor = m_fogColor[m_rankView]; + + terrainRenderer->SetFog(fogStart, fogEnd, { fogColor.r, fogColor.g, fogColor.b }); + Math::Matrix scale; scale.Set(3, 3, -1.0f); auto projectionViewMatrix = Math::MultiplyMatrices(m_matProj, scale); diff --git a/src/graphics/opengl/gl33renderers.cpp b/src/graphics/opengl/gl33renderers.cpp index 3b6d024a..9dccb1b4 100644 --- a/src/graphics/opengl/gl33renderers.cpp +++ b/src/graphics/opengl/gl33renderers.cpp @@ -260,15 +260,17 @@ CGL33TerrainRenderer::CGL33TerrainRenderer(CGL33Device* device) // Setup uniforms auto identity = glm::identity(); - uni_projectionMatrix = glGetUniformLocation(m_program, "uni_ProjectionMatrix"); - uni_viewMatrix = glGetUniformLocation(m_program, "uni_ViewMatrix"); - uni_cameraMatrix = glGetUniformLocation(m_program, "uni_CameraMatrix"); - uni_shadowMatrix = glGetUniformLocation(m_program, "uni_ShadowMatrix"); - uni_modelMatrix = glGetUniformLocation(m_program, "uni_ModelMatrix"); - uni_normalMatrix = glGetUniformLocation(m_program, "uni_NormalMatrix"); - uni_lightPosition = glGetUniformLocation(m_program, "uni_LightPosition"); - uni_lightIntensity = glGetUniformLocation(m_program, "uni_LightIntensity"); - uni_lightColor = glGetUniformLocation(m_program, "uni_LightColor"); + m_projectionMatrix = glGetUniformLocation(m_program, "uni_ProjectionMatrix"); + m_viewMatrix = glGetUniformLocation(m_program, "uni_ViewMatrix"); + m_cameraMatrix = glGetUniformLocation(m_program, "uni_CameraMatrix"); + m_shadowMatrix = glGetUniformLocation(m_program, "uni_ShadowMatrix"); + m_modelMatrix = glGetUniformLocation(m_program, "uni_ModelMatrix"); + m_normalMatrix = glGetUniformLocation(m_program, "uni_NormalMatrix"); + m_lightPosition = glGetUniformLocation(m_program, "uni_LightPosition"); + m_lightIntensity = glGetUniformLocation(m_program, "uni_LightIntensity"); + m_lightColor = glGetUniformLocation(m_program, "uni_LightColor"); + m_fogRange = glGetUniformLocation(m_program, "uni_FogRange"); + m_fogColor = glGetUniformLocation(m_program, "uni_FogColor"); // Set texture units to 10th and 11th auto texture = glGetUniformLocation(m_program, "uni_PrimaryTexture"); @@ -317,7 +319,7 @@ void CGL33TerrainRenderer::End() void CGL33TerrainRenderer::SetProjectionMatrix(const glm::mat4& matrix) { - glUniformMatrix4fv(uni_projectionMatrix, 1, GL_FALSE, value_ptr(matrix)); + glUniformMatrix4fv(m_projectionMatrix, 1, GL_FALSE, value_ptr(matrix)); } void CGL33TerrainRenderer::SetViewMatrix(const glm::mat4& matrix) @@ -328,21 +330,21 @@ void CGL33TerrainRenderer::SetViewMatrix(const glm::mat4& matrix) auto viewMatrix = scale * matrix; auto cameraMatrix = glm::inverse(viewMatrix); - glUniformMatrix4fv(uni_viewMatrix, 1, GL_FALSE, value_ptr(viewMatrix)); - glUniformMatrix4fv(uni_cameraMatrix, 1, GL_FALSE, value_ptr(cameraMatrix)); + glUniformMatrix4fv(m_viewMatrix, 1, GL_FALSE, value_ptr(viewMatrix)); + glUniformMatrix4fv(m_cameraMatrix, 1, GL_FALSE, value_ptr(cameraMatrix)); } void CGL33TerrainRenderer::SetModelMatrix(const glm::mat4& matrix) { auto normalMatrix = glm::transpose(glm::inverse(glm::mat3(matrix))); - glUniformMatrix4fv(uni_modelMatrix, 1, GL_FALSE, value_ptr(matrix)); - glUniformMatrix3fv(uni_normalMatrix, 1, GL_FALSE, value_ptr(normalMatrix)); + glUniformMatrix4fv(m_modelMatrix, 1, GL_FALSE, value_ptr(matrix)); + glUniformMatrix3fv(m_normalMatrix, 1, GL_FALSE, value_ptr(normalMatrix)); } void CGL33TerrainRenderer::SetShadowMatrix(const glm::mat4& matrix) { - glUniformMatrix4fv(uni_shadowMatrix, 1, GL_FALSE, value_ptr(matrix)); + glUniformMatrix4fv(m_shadowMatrix, 1, GL_FALSE, value_ptr(matrix)); } void CGL33TerrainRenderer::SetPrimaryTexture(const Texture& texture) @@ -389,9 +391,15 @@ void CGL33TerrainRenderer::SetShadowMap(const Texture& texture) void CGL33TerrainRenderer::SetLight(const glm::vec4& position, const float& intensity, const glm::vec3& color) { - glUniform4fv(uni_lightPosition, 1, glm::value_ptr(position)); - glUniform1f(uni_lightIntensity, intensity); - glUniform3fv(uni_lightColor, 1, glm::value_ptr(color)); + glUniform4fv(m_lightPosition, 1, glm::value_ptr(position)); + glUniform1f(m_lightIntensity, intensity); + glUniform3fv(m_lightColor, 1, glm::value_ptr(color)); +} + +void CGL33TerrainRenderer::SetFog(float min, float max, const glm::vec3& color) +{ + glUniform2f(m_fogRange, min, max); + glUniform3f(m_fogColor, color.r, color.g, color.b); } void CGL33TerrainRenderer::DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer) diff --git a/src/graphics/opengl/gl33renderers.h b/src/graphics/opengl/gl33renderers.h index 5592e72b..dc251374 100644 --- a/src/graphics/opengl/gl33renderers.h +++ b/src/graphics/opengl/gl33renderers.h @@ -121,6 +121,9 @@ public: //! Sets light parameters virtual void SetLight(const glm::vec4& position, const float& intensity, const glm::vec3& color) override; + //! Sets fog parameters + virtual void SetFog(float min, float max, const glm::vec3& color) override; + //! Draws terrain object virtual void DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer) override; @@ -130,15 +133,17 @@ private: CGL33Device* const m_device; // Uniform data - GLint uni_projectionMatrix; - GLint uni_viewMatrix; - GLint uni_cameraMatrix; - GLint uni_shadowMatrix; - GLint uni_modelMatrix; - GLint uni_normalMatrix; - GLint uni_lightPosition; - GLint uni_lightIntensity; - GLint uni_lightColor; + GLint m_projectionMatrix; + GLint m_viewMatrix; + GLint m_cameraMatrix; + GLint m_shadowMatrix; + GLint m_modelMatrix; + GLint m_normalMatrix; + GLint m_lightPosition; + GLint m_lightIntensity; + GLint m_lightColor; + GLint m_fogRange; + GLint m_fogColor; // Shader program GLuint m_program = 0; diff --git a/src/graphics/opengl/shaders/gl33/terrain_fs.glsl b/src/graphics/opengl/shaders/gl33/terrain_fs.glsl index 3c3135cb..8aa30310 100644 --- a/src/graphics/opengl/shaders/gl33/terrain_fs.glsl +++ b/src/graphics/opengl/shaders/gl33/terrain_fs.glsl @@ -25,6 +25,9 @@ uniform vec4 uni_LightPosition; uniform float uni_LightIntensity; uniform vec3 uni_LightColor; +uniform vec2 uni_FogRange; +uniform vec3 uni_FogColor; + uniform sampler2D uni_PrimaryTexture; uniform sampler2D uni_SecondaryTexture; uniform sampler2DShadow uni_ShadowMap; @@ -113,5 +116,10 @@ void main() vec3 color = lighting * shadow + albedo * skyColor * skyIntensity; + float dist = length(uni_CameraMatrix[3].xyz - data.Position); + float fogAmount = clamp((dist - uni_FogRange.x) / (uni_FogRange.y - uni_FogRange.x), 0.0, 1.0); + + color = mix(color, uni_FogColor, fogAmount); + out_FragColor = vec4(color, 1.0); }