From 07df96b4f23a1617f2263b814ca597d62f96eb3e Mon Sep 17 00:00:00 2001 From: codereader Date: Fri, 18 Mar 2022 10:57:56 +0100 Subject: [PATCH] #5909: Light texture matrix can be stored in the GLSL uniforms per light, the object transform is already present there to perform the same math. Save a few more calculations and uniform commands. --- install/gl/interaction_vp.glsl | 2 +- .../rendersystem/backend/LightInteractions.cpp | 18 +++++------------- .../backend/LightingModeRenderer.cpp | 2 ++ .../backend/glprogram/GLSLBumpProgram.cpp | 11 +++-------- .../backend/glprogram/GLSLBumpProgram.h | 2 -- 5 files changed, 11 insertions(+), 24 deletions(-) diff --git a/install/gl/interaction_vp.glsl b/install/gl/interaction_vp.glsl index a9eb52da1c..798ded6935 100644 --- a/install/gl/interaction_vp.glsl +++ b/install/gl/interaction_vp.glsl @@ -51,7 +51,7 @@ void main() var_TexSpecular.y = dot(u_SpecularTextureMatrix[1], attr_TexCoord); // calc light xy,z attenuation in light space - var_tex_atten_xy_z = u_LightTextureMatrix * attr_Position; + var_tex_atten_xy_z = u_LightTextureMatrix * worldVertex; // construct object-space-to-tangent-space 3x3 matrix var_mat_os2ts = mat3( diff --git a/radiantcore/rendersystem/backend/LightInteractions.cpp b/radiantcore/rendersystem/backend/LightInteractions.cpp index 9e3c259067..3a25a4bb98 100644 --- a/radiantcore/rendersystem/backend/LightInteractions.cpp +++ b/radiantcore/rendersystem/backend/LightInteractions.cpp @@ -74,14 +74,11 @@ void LightInteractions::fillDepthBuffer(OpenGLState& state, GLSLDepthFillAlphaPr const auto& material = shader->getMaterial(); assert(material); - // Skip translucent materials - if (material->getCoverage() == Material::MC_TRANSLUCENT) - { - continue; - } - auto coverage = material->getCoverage(); + // Skip translucent materials + if (coverage == Material::MC_TRANSLUCENT) continue; + if (coverage == Material::MC_PERFORATED) { // Evaluate the shader stages of this material @@ -149,14 +146,11 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr return; } - auto worldToLight = _light.getLightTextureTransformation(); auto worldLightOrigin = _light.getLightOrigin(); std::vector untransformedObjects; untransformedObjects.reserve(10000); - program.setModelViewProjection(view.GetViewProjection()); - // Set up textures used by this light program.setupLightParameters(state, _light, renderTime); @@ -205,8 +199,7 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr continue; } - program.setUpObjectLighting(worldLightOrigin, worldToLight, - view.getViewer(), object.get().getObjectTransform(), + program.setUpObjectLighting(worldLightOrigin, view.getViewer(), object.get().getObjectTransform().getInverse()); pass->getProgram().setObjectTransform(object.get().getObjectTransform()); @@ -217,8 +210,7 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr if (!untransformedObjects.empty()) { - program.setUpObjectLighting(worldLightOrigin, worldToLight, - view.getViewer(), Matrix4::getIdentity(), Matrix4::getIdentity()); + program.setUpObjectLighting(worldLightOrigin, view.getViewer(), Matrix4::getIdentity()); pass->getProgram().setObjectTransform(Matrix4::getIdentity()); diff --git a/radiantcore/rendersystem/backend/LightingModeRenderer.cpp b/radiantcore/rendersystem/backend/LightingModeRenderer.cpp index 843a96d839..e5302a6565 100644 --- a/radiantcore/rendersystem/backend/LightingModeRenderer.cpp +++ b/radiantcore/rendersystem/backend/LightingModeRenderer.cpp @@ -69,6 +69,8 @@ IRenderResult::Ptr LightingModeRenderer::render(RenderStateFlags globalFlagsMask auto interactionProgram = dynamic_cast(current.glProgram); assert(interactionProgram); + interactionProgram->setModelViewProjection(view.GetViewProjection()); + for (auto& interactionList : interactionLists) { interactionList.drawInteractions(current, *interactionProgram, view, time); diff --git a/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp b/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp index f682ee1066..c618ae18b3 100644 --- a/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp +++ b/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp @@ -216,12 +216,13 @@ void GLSLBumpProgram::setupLightParameters(OpenGLState& state, const RendererLig glUniform1i(_locAmbientLight, lightMat->isAmbientLight()); glUniform3fv(_locLightColour, 1, layer->getColour()); + + // Send the light texture transform + loadMatrixUniform(_locLightTextureMatrix, light.getLightTextureTransformation()); } void GLSLBumpProgram::setUpObjectLighting(const Vector3& worldLightOrigin, - const Matrix4& worldToLight, const Vector3& viewer, - const Matrix4& objectTransform, const Matrix4& inverseObjectTransform) { debug::assertNoGlErrors(); @@ -231,9 +232,6 @@ void GLSLBumpProgram::setUpObjectLighting(const Vector3& worldLightOrigin, // Calculate the light origin in object space Vector3 localLight = worldToObject.transformPoint(worldLightOrigin); - Matrix4 local2light(worldToLight); - local2light.multiplyBy(objectTransform); // local->world->light - // Calculate viewer location in object space auto osViewer = inverseObjectTransform.transformPoint(viewer); @@ -249,9 +247,6 @@ void GLSLBumpProgram::setUpObjectLighting(const Vector3& worldLightOrigin, static_cast(localLight.z()) ); - // Load the light texture transform - loadMatrixUniform(_locLightTextureMatrix, local2light); - debug::assertNoGlErrors(); } diff --git a/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h b/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h index 5992b0d4ff..f44d5ed2b4 100644 --- a/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h +++ b/radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h @@ -55,9 +55,7 @@ class GLSLBumpProgram : void setupLightParameters(OpenGLState& state, const RendererLight& light, std::size_t renderTime); void setUpObjectLighting(const Vector3& worldLightOrigin, - const Matrix4& worldToLight, const Vector3& viewer, - const Matrix4& objectTransform, const Matrix4& inverseObjectTransform); };