Skip to content

Commit

Permalink
#5909: More lighting set up code moved to GLSLBumpProgram.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 18, 2022
1 parent 2df6b65 commit 16a90d3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 78 deletions.
39 changes: 7 additions & 32 deletions radiantcore/rendersystem/backend/LightInteractions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,36 +136,7 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr
program.setModelViewProjection(view.GetViewProjection());

// Set up textures used by this light
{
// Get the light shader and examine its first (and only valid) layer
const auto& shader = _light.getShader();
assert(shader);

const auto& lightMat = shader->getMaterial();
auto* layer = lightMat ? lightMat->firstLayer() : nullptr;
if (!layer) return;

// Calculate all dynamic values in the layer
layer->evaluateExpressions(renderTime, _light.getLightEntity());

// Get the XY and Z falloff texture numbers.
auto attenuation_xy = layer->getTexture()->getGLTexNum();
auto attenuation_z = lightMat->lightFalloffImage()->getGLTexNum();

// Bind the falloff textures
assert(state.testRenderFlag(RENDER_TEXTURE_2D));

OpenGLState::SetTextureState(state.texture3, attenuation_xy, GL_TEXTURE3, GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

OpenGLState::SetTextureState(state.texture4, attenuation_z, GL_TEXTURE4, GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

program.setIsAmbientLight(lightMat->isAmbientLight());
program.setLightColour(layer->getColour());
}
program.setupLightParameters(state, _light, renderTime);

for (const auto& [entity, objectsByShader] : _objectsByEntity)
{
Expand Down Expand Up @@ -212,7 +183,7 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr
continue;
}

program.setUpLightingCalculation(worldLightOrigin, worldToLight,
program.setUpObjectLighting(worldLightOrigin, worldToLight,
view.getViewer(), object.get().getObjectTransform(),
object.get().getObjectTransform().getInverse());

Expand All @@ -224,7 +195,7 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr

if (!untransformedObjects.empty())
{
program.setUpLightingCalculation(worldLightOrigin, worldToLight,
program.setUpObjectLighting(worldLightOrigin, worldToLight,
view.getViewer(), Matrix4::getIdentity(), Matrix4::getIdentity());

pass->getProgram().setObjectTransform(Matrix4::getIdentity());
Expand All @@ -236,6 +207,10 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr
}
}
}

// Unbind the light textures
OpenGLState::SetTextureState(state.texture3, 0, GL_TEXTURE3, GL_TEXTURE_2D);
OpenGLState::SetTextureState(state.texture4, 0, GL_TEXTURE4, GL_TEXTURE_2D);
}

}
75 changes: 33 additions & 42 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include "GLProgramAttributes.h"
#include "itextstream.h"
#include "igame.h"
#include "ishaders.h"
#include "string/convert.h"
#include "debugging/gl.h"
#include "math/Matrix4.h"
#include "../OpenGLState.h"

namespace render
{
Expand Down Expand Up @@ -131,16 +133,6 @@ void GLSLBumpProgram::disable()
debug::assertNoGlErrors();
}

void GLSLBumpProgram::setIsAmbientLight(bool isAmbientLight)
{
glUniform1i(_locAmbientLight, isAmbientLight);
}

void GLSLBumpProgram::setLightColour(const Colour4& lightColour)
{
glUniform3fv(_locLightColour, 1, lightColour);
}

void GLSLBumpProgram::setStageVertexColour(IShaderLayer::VertexColourMode vertexColourMode, const Colour4& stageColour)
{
// Define the colour factors to blend into the final fragment
Expand Down Expand Up @@ -195,7 +187,37 @@ void GLSLBumpProgram::setSpecularTextureTransform(const Matrix4& transform)
loadTextureMatrixUniform(_locSpecularTextureMatrix, transform);
}

void GLSLBumpProgram::setUpLightingCalculation(const Vector3& worldLightOrigin,
void GLSLBumpProgram::setupLightParameters(OpenGLState& state, const RendererLight& light, std::size_t renderTime)
{
// Get the light shader and examine its first (and only valid) layer
const auto & shader = light.getShader();
assert(shader);

const auto& lightMat = shader->getMaterial();
auto* layer = lightMat ? lightMat->firstLayer() : nullptr;
if (!layer) return;

// Calculate all dynamic values in the layer
layer->evaluateExpressions(renderTime, light.getLightEntity());

// Get the XY and Z falloff texture numbers.
auto attenuation_xy = layer->getTexture()->getGLTexNum();
auto attenuation_z = lightMat->lightFalloffImage()->getGLTexNum();

// Bind the falloff textures
OpenGLState::SetTextureState(state.texture3, attenuation_xy, GL_TEXTURE3, GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

OpenGLState::SetTextureState(state.texture4, attenuation_z, GL_TEXTURE4, GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

glUniform1i(_locAmbientLight, lightMat->isAmbientLight());
glUniform3fv(_locLightColour, 1, layer->getColour());
}

void GLSLBumpProgram::setUpObjectLighting(const Vector3& worldLightOrigin,
const Matrix4& worldToLight,
const Vector3& viewer,
const Matrix4& objectTransform,
Expand Down Expand Up @@ -225,37 +247,6 @@ void GLSLBumpProgram::setUpLightingCalculation(const Vector3& worldLightOrigin,
static_cast<float>(localLight.y()),
static_cast<float>(localLight.z())
);
#if 0
glUniform3fv(_locLightColour, 1, parms.lightColour);
glUniform1f(_locLightScale, _lightScale);
glUniform1i(_locAmbientLight, parms.isAmbientLight);

// Define the colour factors to blend into the final fragment
switch (parms.vertexColourMode)
{
case IShaderLayer::VERTEX_COLOUR_NONE:
// Nullify the vertex colour, add the stage colour as additive constant
glUniform4f(_locColourModulation, 0, 0, 0, 0);
glUniform4f(_locColourAddition,
static_cast<float>(parms.stageColour.x()),
static_cast<float>(parms.stageColour.y()),
static_cast<float>(parms.stageColour.z()),
static_cast<float>(parms.stageColour.w()));
break;

case IShaderLayer::VERTEX_COLOUR_MULTIPLY:
// Multiply the fragment with 1*vertexColour
glUniform4f(_locColourModulation, 1, 1, 1, 1);
glUniform4f(_locColourAddition, 0, 0, 0, 0);
break;

case IShaderLayer::VERTEX_COLOUR_INVERSE_MULTIPLY:
// Multiply the fragment with (1 - vertexColour)
glUniform4f(_locColourModulation, -1, -1, -1, -1);
glUniform4f(_locColourAddition, 1, 1, 1, 1);
break;
}
#endif

glActiveTexture(GL_TEXTURE3);
glClientActiveTexture(GL_TEXTURE3);
Expand Down
9 changes: 5 additions & 4 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace render
{

class OpenGLState;

class GLSLBumpProgram :
public GLSLProgramBase
{
Expand Down Expand Up @@ -41,9 +43,6 @@ class GLSLBumpProgram :
void setBumpTextureTransform(const Matrix4& transform);
void setSpecularTextureTransform(const Matrix4& transform);

void setIsAmbientLight(bool isAmbientLight);
void setLightColour(const Colour4& lightColour);

// The stage's vertex colour mode and colour as defined by the rgba registers
void setStageVertexColour(IShaderLayer::VertexColourMode vertexColourMode, const Colour4& stageColour);

Expand All @@ -52,7 +51,9 @@ class GLSLBumpProgram :
const Params& lightParms) override
{ }

void setUpLightingCalculation(const Vector3& worldLightOrigin,
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,
Expand Down

0 comments on commit 16a90d3

Please sign in to comment.