From 5cbf8f04ec2702ee53bd94a9cd795accbee5229c Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Tue, 19 Jan 2021 19:19:04 +0000 Subject: [PATCH] Removes code duplications for binding shadows to shaders. This was SVN commit r24710. --- source/renderer/RenderModifiers.cpp | 10 ++------- source/renderer/ShadowMap.cpp | 30 +++++++-------------------- source/renderer/ShadowMap.h | 32 ++++------------------------- source/renderer/TerrainRenderer.cpp | 16 ++------------- 4 files changed, 15 insertions(+), 73 deletions(-) diff --git a/source/renderer/RenderModifiers.cpp b/source/renderer/RenderModifiers.cpp index 1c14269b26..955a2668d8 100644 --- a/source/renderer/RenderModifiers.cpp +++ b/source/renderer/RenderModifiers.cpp @@ -76,14 +76,8 @@ void ShaderRenderModifier::BeginPass(const CShaderProgramPtr& shader) shader->Uniform(str_transform, g_Renderer.GetViewCamera().GetViewProjection()); shader->Uniform(str_cameraPos, g_Renderer.GetViewCamera().GetOrientation().GetTranslation()); - if (GetShadowMap() && shader->GetTextureBinding(str_shadowTex).Active()) - { - shader->BindTexture(str_shadowTex, GetShadowMap()->GetTexture()); - shader->Uniform(str_shadowTransform, GetShadowMap()->GetTextureMatrix()); - int width = GetShadowMap()->GetWidth(); - int height = GetShadowMap()->GetHeight(); - shader->Uniform(str_shadowScale, width, height, 1.0f / width, 1.0f / height); - } + if (GetShadowMap()) + GetShadowMap()->BindTo(shader); if (GetLightEnv()) { diff --git a/source/renderer/ShadowMap.cpp b/source/renderer/ShadowMap.cpp index 39d6ff5d43..479c314d23 100644 --- a/source/renderer/ShadowMap.cpp +++ b/source/renderer/ShadowMap.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -664,20 +664,16 @@ void ShadowMap::EndRender() glColorMask(1,1,1,1); } - -/////////////////////////////////////////////////////////////////////////////////////////////////// -// Retrieve the texture handle and texture matrix for shadowing -GLuint ShadowMap::GetTexture() const +void ShadowMap::BindTo(const CShaderProgramPtr& shader) const { - return m->Texture; -} + if (!shader->GetTextureBinding(str_shadowTex).Active()) + return; -const CMatrix3D& ShadowMap::GetTextureMatrix() const -{ - return m->TextureMatrix; + shader->BindTexture(str_shadowTex, m->Texture); + shader->Uniform(str_shadowTransform, m->TextureMatrix); + shader->Uniform(str_shadowScale, m->Width, m->Height, 1.0f / m->Width, 1.0f / m->Height); } - /////////////////////////////////////////////////////////////////////////////////////////////////// // Depth texture bits int ShadowMap::GetDepthTextureBits() const @@ -700,18 +696,6 @@ void ShadowMap::SetDepthTextureBits(int bits) } } -/////////////////////////////////////////////////////////////////////////////////////////////////// -// Depth texture size -int ShadowMap::GetWidth() const -{ - return m->Width; -} - -int ShadowMap::GetHeight() const -{ - return m->Height; -} - ////////////////////////////////////////////////////////////////////////////// void ShadowMap::RenderDebugBounds() diff --git a/source/renderer/ShadowMap.h b/source/renderer/ShadowMap.h index a9a88748ca..e5208cf89a 100644 --- a/source/renderer/ShadowMap.h +++ b/source/renderer/ShadowMap.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -22,6 +22,7 @@ #ifndef INCLUDED_SHADOWMAP #define INCLUDED_SHADOWMAP +#include "graphics/ShaderProgramPtr.h" #include "lib/ogl.h" class CBoundingBoxAligned; @@ -68,20 +69,6 @@ public: */ void SetDepthTextureBits(int bits); - /** - * GetWidth: Return the width of the depth texture. - * - * @return depth texture width - */ - int GetWidth() const; - - /** - * GetHeight: Return the height of the depth texture - * - * @return depth texture height - */ - int GetHeight() const; - /** * SetupFrame: Configure light space for the given camera and light direction, * create the shadow texture if necessary, etc. @@ -132,20 +119,9 @@ public: void EndRender(); /** - * GetTexture: Retrieve the OpenGL texture object name that contains the shadow map. - * - * @return the texture name of the shadow map texture + * Binds all needed resources and uniforms to draw shadows using the shader. */ - GLuint GetTexture() const; - - /** - * GetTextureMatrix: Retrieve the world-space to shadow map texture coordinates - * transformation matrix. - * - * @return the matrix that transforms world-space coordinates into homogenous - * shadow map texture coordinates - */ - const CMatrix3D& GetTextureMatrix() const; + void BindTo(const CShaderProgramPtr& shader) const; /** * Visualize shadow mapping calculations to help in diff --git a/source/renderer/TerrainRenderer.cpp b/source/renderer/TerrainRenderer.cpp index 48ee31476a..ee28822e30 100644 --- a/source/renderer/TerrainRenderer.cpp +++ b/source/renderer/TerrainRenderer.cpp @@ -261,13 +261,7 @@ void TerrainRenderer::PrepareShader(const CShaderProgramPtr& shader, ShadowMap* const CLightEnv& lightEnv = g_Renderer.GetLightEnv(); if (shadow) - { - shader->BindTexture(str_shadowTex, shadow->GetTexture()); - shader->Uniform(str_shadowTransform, shadow->GetTextureMatrix()); - int width = shadow->GetWidth(); - int height = shadow->GetHeight(); - shader->Uniform(str_shadowScale, width, height, 1.0f / width, 1.0f / height); - } + shadow->BindTo(shader); CLOSTexture& los = g_Renderer.GetScene().GetLOSTexture(); shader->BindTexture(str_losTex, los.GetTextureSmooth()); @@ -599,13 +593,7 @@ bool TerrainRenderer::RenderFancyWater(const CShaderDefines& context, int cullGr } if (shadow) - { - m->fancyWaterShader->BindTexture(str_shadowTex, shadow->GetTexture()); - m->fancyWaterShader->Uniform(str_shadowTransform, shadow->GetTextureMatrix()); - int width = shadow->GetWidth(); - int height = shadow->GetHeight(); - m->fancyWaterShader->Uniform(str_shadowScale, width, height, 1.0f / width, 1.0f / height); - } + shadow->BindTo(m->fancyWaterShader); std::vector& visiblePatches = m->visiblePatches[cullGroup]; for (size_t i = 0; i < visiblePatches.size(); ++i)