Added fog to terrain renderer and renamed variables
parent
4157604458
commit
fea6b87139
|
@ -97,6 +97,9 @@ public:
|
||||||
//! Sets light parameters
|
//! Sets light parameters
|
||||||
virtual void SetLight(const glm::vec4& position, const float& intensity, const glm::vec3& color) = 0;
|
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
|
//! Draws terrain object
|
||||||
virtual void DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer) = 0;
|
virtual void DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer) = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -3393,6 +3393,10 @@ void CEngine::Draw3DScene()
|
||||||
terrainRenderer->SetShadowMap(m_shadowMap);
|
terrainRenderer->SetShadowMap(m_shadowMap);
|
||||||
terrainRenderer->SetLight(glm::vec4(1.0, 1.0, -1.0, 0.0), 1.0f, glm::vec3(1.0));
|
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;
|
Math::Matrix scale;
|
||||||
scale.Set(3, 3, -1.0f);
|
scale.Set(3, 3, -1.0f);
|
||||||
auto projectionViewMatrix = Math::MultiplyMatrices(m_matProj, scale);
|
auto projectionViewMatrix = Math::MultiplyMatrices(m_matProj, scale);
|
||||||
|
|
|
@ -260,15 +260,17 @@ CGL33TerrainRenderer::CGL33TerrainRenderer(CGL33Device* device)
|
||||||
// Setup uniforms
|
// Setup uniforms
|
||||||
auto identity = glm::identity<glm::mat4>();
|
auto identity = glm::identity<glm::mat4>();
|
||||||
|
|
||||||
uni_projectionMatrix = glGetUniformLocation(m_program, "uni_ProjectionMatrix");
|
m_projectionMatrix = glGetUniformLocation(m_program, "uni_ProjectionMatrix");
|
||||||
uni_viewMatrix = glGetUniformLocation(m_program, "uni_ViewMatrix");
|
m_viewMatrix = glGetUniformLocation(m_program, "uni_ViewMatrix");
|
||||||
uni_cameraMatrix = glGetUniformLocation(m_program, "uni_CameraMatrix");
|
m_cameraMatrix = glGetUniformLocation(m_program, "uni_CameraMatrix");
|
||||||
uni_shadowMatrix = glGetUniformLocation(m_program, "uni_ShadowMatrix");
|
m_shadowMatrix = glGetUniformLocation(m_program, "uni_ShadowMatrix");
|
||||||
uni_modelMatrix = glGetUniformLocation(m_program, "uni_ModelMatrix");
|
m_modelMatrix = glGetUniformLocation(m_program, "uni_ModelMatrix");
|
||||||
uni_normalMatrix = glGetUniformLocation(m_program, "uni_NormalMatrix");
|
m_normalMatrix = glGetUniformLocation(m_program, "uni_NormalMatrix");
|
||||||
uni_lightPosition = glGetUniformLocation(m_program, "uni_LightPosition");
|
m_lightPosition = glGetUniformLocation(m_program, "uni_LightPosition");
|
||||||
uni_lightIntensity = glGetUniformLocation(m_program, "uni_LightIntensity");
|
m_lightIntensity = glGetUniformLocation(m_program, "uni_LightIntensity");
|
||||||
uni_lightColor = glGetUniformLocation(m_program, "uni_LightColor");
|
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
|
// Set texture units to 10th and 11th
|
||||||
auto texture = glGetUniformLocation(m_program, "uni_PrimaryTexture");
|
auto texture = glGetUniformLocation(m_program, "uni_PrimaryTexture");
|
||||||
|
@ -317,7 +319,7 @@ void CGL33TerrainRenderer::End()
|
||||||
|
|
||||||
void CGL33TerrainRenderer::SetProjectionMatrix(const glm::mat4& matrix)
|
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)
|
void CGL33TerrainRenderer::SetViewMatrix(const glm::mat4& matrix)
|
||||||
|
@ -328,21 +330,21 @@ void CGL33TerrainRenderer::SetViewMatrix(const glm::mat4& matrix)
|
||||||
auto viewMatrix = scale * matrix;
|
auto viewMatrix = scale * matrix;
|
||||||
auto cameraMatrix = glm::inverse(viewMatrix);
|
auto cameraMatrix = glm::inverse(viewMatrix);
|
||||||
|
|
||||||
glUniformMatrix4fv(uni_viewMatrix, 1, GL_FALSE, value_ptr(viewMatrix));
|
glUniformMatrix4fv(m_viewMatrix, 1, GL_FALSE, value_ptr(viewMatrix));
|
||||||
glUniformMatrix4fv(uni_cameraMatrix, 1, GL_FALSE, value_ptr(cameraMatrix));
|
glUniformMatrix4fv(m_cameraMatrix, 1, GL_FALSE, value_ptr(cameraMatrix));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGL33TerrainRenderer::SetModelMatrix(const glm::mat4& matrix)
|
void CGL33TerrainRenderer::SetModelMatrix(const glm::mat4& matrix)
|
||||||
{
|
{
|
||||||
auto normalMatrix = glm::transpose(glm::inverse(glm::mat3(matrix)));
|
auto normalMatrix = glm::transpose(glm::inverse(glm::mat3(matrix)));
|
||||||
|
|
||||||
glUniformMatrix4fv(uni_modelMatrix, 1, GL_FALSE, value_ptr(matrix));
|
glUniformMatrix4fv(m_modelMatrix, 1, GL_FALSE, value_ptr(matrix));
|
||||||
glUniformMatrix3fv(uni_normalMatrix, 1, GL_FALSE, value_ptr(normalMatrix));
|
glUniformMatrix3fv(m_normalMatrix, 1, GL_FALSE, value_ptr(normalMatrix));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGL33TerrainRenderer::SetShadowMatrix(const glm::mat4& matrix)
|
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)
|
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)
|
void CGL33TerrainRenderer::SetLight(const glm::vec4& position, const float& intensity, const glm::vec3& color)
|
||||||
{
|
{
|
||||||
glUniform4fv(uni_lightPosition, 1, glm::value_ptr(position));
|
glUniform4fv(m_lightPosition, 1, glm::value_ptr(position));
|
||||||
glUniform1f(uni_lightIntensity, intensity);
|
glUniform1f(m_lightIntensity, intensity);
|
||||||
glUniform3fv(uni_lightColor, 1, glm::value_ptr(color));
|
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)
|
void CGL33TerrainRenderer::DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer)
|
||||||
|
|
|
@ -121,6 +121,9 @@ public:
|
||||||
//! Sets light parameters
|
//! Sets light parameters
|
||||||
virtual void SetLight(const glm::vec4& position, const float& intensity, const glm::vec3& color) override;
|
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
|
//! Draws terrain object
|
||||||
virtual void DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer) override;
|
virtual void DrawObject(const glm::mat4& matrix, const CVertexBuffer* buffer) override;
|
||||||
|
|
||||||
|
@ -130,15 +133,17 @@ private:
|
||||||
CGL33Device* const m_device;
|
CGL33Device* const m_device;
|
||||||
|
|
||||||
// Uniform data
|
// Uniform data
|
||||||
GLint uni_projectionMatrix;
|
GLint m_projectionMatrix;
|
||||||
GLint uni_viewMatrix;
|
GLint m_viewMatrix;
|
||||||
GLint uni_cameraMatrix;
|
GLint m_cameraMatrix;
|
||||||
GLint uni_shadowMatrix;
|
GLint m_shadowMatrix;
|
||||||
GLint uni_modelMatrix;
|
GLint m_modelMatrix;
|
||||||
GLint uni_normalMatrix;
|
GLint m_normalMatrix;
|
||||||
GLint uni_lightPosition;
|
GLint m_lightPosition;
|
||||||
GLint uni_lightIntensity;
|
GLint m_lightIntensity;
|
||||||
GLint uni_lightColor;
|
GLint m_lightColor;
|
||||||
|
GLint m_fogRange;
|
||||||
|
GLint m_fogColor;
|
||||||
|
|
||||||
// Shader program
|
// Shader program
|
||||||
GLuint m_program = 0;
|
GLuint m_program = 0;
|
||||||
|
|
|
@ -25,6 +25,9 @@ uniform vec4 uni_LightPosition;
|
||||||
uniform float uni_LightIntensity;
|
uniform float uni_LightIntensity;
|
||||||
uniform vec3 uni_LightColor;
|
uniform vec3 uni_LightColor;
|
||||||
|
|
||||||
|
uniform vec2 uni_FogRange;
|
||||||
|
uniform vec3 uni_FogColor;
|
||||||
|
|
||||||
uniform sampler2D uni_PrimaryTexture;
|
uniform sampler2D uni_PrimaryTexture;
|
||||||
uniform sampler2D uni_SecondaryTexture;
|
uniform sampler2D uni_SecondaryTexture;
|
||||||
uniform sampler2DShadow uni_ShadowMap;
|
uniform sampler2DShadow uni_ShadowMap;
|
||||||
|
@ -113,5 +116,10 @@ void main()
|
||||||
|
|
||||||
vec3 color = lighting * shadow + albedo * skyColor * skyIntensity;
|
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);
|
out_FragColor = vec4(color, 1.0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue