Skip to content

Commit

Permalink
#5909: Light texture matrix can be stored in the GLSL uniforms per li…
Browse files Browse the repository at this point in the history
…ght, the object transform is already present there to perform the same math.

Save a few more calculations and uniform commands.
  • Loading branch information
codereader committed Mar 18, 2022
1 parent d34ec8a commit 07df96b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 24 deletions.
2 changes: 1 addition & 1 deletion install/gl/interaction_vp.glsl
Expand Up @@ -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(
Expand Down
18 changes: 5 additions & 13 deletions radiantcore/rendersystem/backend/LightInteractions.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -149,14 +146,11 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr
return;
}

auto worldToLight = _light.getLightTextureTransformation();
auto worldLightOrigin = _light.getLightOrigin();

std::vector<IGeometryStore::Slot> untransformedObjects;
untransformedObjects.reserve(10000);

program.setModelViewProjection(view.GetViewProjection());

// Set up textures used by this light
program.setupLightParameters(state, _light, renderTime);

Expand Down Expand Up @@ -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());
Expand All @@ -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());

Expand Down
2 changes: 2 additions & 0 deletions radiantcore/rendersystem/backend/LightingModeRenderer.cpp
Expand Up @@ -69,6 +69,8 @@ IRenderResult::Ptr LightingModeRenderer::render(RenderStateFlags globalFlagsMask
auto interactionProgram = dynamic_cast<GLSLBumpProgram*>(current.glProgram);
assert(interactionProgram);

interactionProgram->setModelViewProjection(view.GetViewProjection());

for (auto& interactionList : interactionLists)
{
interactionList.drawInteractions(current, *interactionProgram, view, time);
Expand Down
11 changes: 3 additions & 8 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp
Expand Up @@ -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();
Expand All @@ -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);

Expand All @@ -249,9 +247,6 @@ void GLSLBumpProgram::setUpObjectLighting(const Vector3& worldLightOrigin,
static_cast<float>(localLight.z())
);

// Load the light texture transform
loadMatrixUniform(_locLightTextureMatrix, local2light);

debug::assertNoGlErrors();
}

Expand Down
2 changes: 0 additions & 2 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h
Expand Up @@ -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);
};

Expand Down

0 comments on commit 07df96b

Please sign in to comment.