Skip to content

Commit

Permalink
#5909: 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 7535c43 commit 2df6b65
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 106 deletions.
8 changes: 5 additions & 3 deletions radiantcore/rendersystem/backend/LightInteractions.cpp
Expand Up @@ -128,6 +128,7 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr
}

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

std::vector<IGeometryStore::Slot> untransformedObjects;
untransformedObjects.reserve(10000);
Expand Down Expand Up @@ -211,8 +212,9 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr
continue;
}

OpenGLShaderPass::SetUpLightingCalculation(program, &_light, worldToLight,
view.getViewer(), object.get().getObjectTransform(), object.get().getObjectTransform().getInverse());
program.setUpLightingCalculation(worldLightOrigin, worldToLight,
view.getViewer(), object.get().getObjectTransform(),
object.get().getObjectTransform().getInverse());

pass->getProgram().setObjectTransform(object.get().getObjectTransform());

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

if (!untransformedObjects.empty())
{
OpenGLShaderPass::SetUpLightingCalculation(program, &_light, worldToLight,
program.setUpLightingCalculation(worldLightOrigin, worldToLight,
view.getViewer(), Matrix4::getIdentity(), Matrix4::getIdentity());

pass->getProgram().setObjectTransform(Matrix4::getIdentity());
Expand Down
53 changes: 0 additions & 53 deletions radiantcore/rendersystem/backend/OpenGLShaderPass.cpp
Expand Up @@ -118,59 +118,6 @@ bool OpenGLShaderPass::stateIsActive()
(_glState.stage3 == NULL || _glState.stage3->isVisible()));
}

// Setup lighting
void OpenGLShaderPass::SetUpLightingCalculation(GLSLBumpProgram& program,
const RendererLight* light,
const Matrix4& worldToLight,
const Vector3& viewer,
const Matrix4& objectTransform,
const Matrix4& inverseObjectTransform)
{
#if 0
// Get the light shader and examine its first (and only valid) layer
assert(light);
const auto& shader = light->getShader();
assert(shader);

const MaterialPtr& lightMat = shader->getMaterial();
IShaderLayer* layer = lightMat ? lightMat->firstLayer() : nullptr;
if (!layer) return;
#endif
// Calculate viewer location in object space
auto osViewer = inverseObjectTransform.transformPoint(viewer);
#if 0
// Calculate all dynamic values in the layer
layer->evaluateExpressions(time, 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(current.testRenderFlag(RENDER_TEXTURE_2D));

OpenGLState::SetTextureState(current.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(current.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);
#endif

// Set the GL program parameters
GLProgram::Params parms(
//current.getVertexColourMode(),
//current.getColour(),
light->getLightOrigin(),
//layer->getColour(),
worldToLight
);
#if 0
parms.isAmbientLight = lightMat->isAmbientLight();
#endif
program.applyRenderParams(osViewer, objectTransform, inverseObjectTransform, parms);
}

void OpenGLShaderPass::SetUpNonInteractionProgram(OpenGLState& current, const Vector3& viewer, const Matrix4& objTransform)
{
static GLProgram::Params parms({ 0, 0, 0 }, Matrix4::getIdentity());
Expand Down
8 changes: 0 additions & 8 deletions radiantcore/rendersystem/backend/OpenGLShaderPass.h
Expand Up @@ -140,14 +140,6 @@ class OpenGLShaderPass
// Evaluates the time- and entity-dependent expressions in the shader stages
void evaluateShaderStages(std::size_t time, const IRenderEntity* entity);

// Set up lighting calculation
static void SetUpLightingCalculation(GLSLBumpProgram& program,
const RendererLight* light,
const Matrix4& worldToLight,
const Vector3& viewer,
const Matrix4& objectTransform,
const Matrix4& inverseObjectTransform);

static void SetUpNonInteractionProgram(OpenGLState& current, const Vector3& viewer, const Matrix4& objTransform);

friend std::ostream& operator<<(std::ostream& st, const OpenGLShaderPass& self);
Expand Down
78 changes: 41 additions & 37 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp
Expand Up @@ -90,13 +90,13 @@ void GLSLBumpProgram::create()
samplerLoc = glGetUniformLocation(_programObj, "u_attenuationmap_z");
glUniform1i(samplerLoc, 4);

// Light scale is constant at this point
glUniform1f(_locLightScale, _lightScale);

debug::assertNoGlErrors();
glUseProgram(0);

debug::assertNoGlErrors();

// Light scale is constant at this point
glUniform1f(_locLightScale, _lightScale);
}

void GLSLBumpProgram::enable()
Expand Down Expand Up @@ -170,26 +170,55 @@ void GLSLBumpProgram::setStageVertexColour(IShaderLayer::VertexColourMode vertex
}
}

void GLSLBumpProgram::applyRenderParams(const Vector3& viewer,
const Matrix4& objectTransform,
const Matrix4& inverseObjectTransform,
const Params& parms)
void GLSLBumpProgram::setModelViewProjection(const Matrix4& modelViewProjection)
{
loadMatrixUniform(_locModelViewProjection, modelViewProjection);
}

void GLSLBumpProgram::setObjectTransform(const Matrix4& transform)
{
loadMatrixUniform(_locObjectTransform, transform);
}

void GLSLBumpProgram::setDiffuseTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locDiffuseTextureMatrix, transform);
}

void GLSLBumpProgram::setBumpTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locBumpTextureMatrix, transform);
}

void GLSLBumpProgram::setSpecularTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locSpecularTextureMatrix, transform);
}

void GLSLBumpProgram::setUpLightingCalculation(const Vector3& worldLightOrigin,
const Matrix4& worldToLight,
const Vector3& viewer,
const Matrix4& objectTransform,
const Matrix4& inverseObjectTransform)
{
debug::assertNoGlErrors();

const auto& worldToObject = inverseObjectTransform;

// Calculate the light origin in object space
Vector3 localLight = worldToObject.transformPoint(parms.lightOrigin);
Vector3 localLight = worldToObject.transformPoint(worldLightOrigin);

Matrix4 local2light(parms.world2Light);
Matrix4 local2light(worldToLight);
local2light.multiplyBy(objectTransform); // local->world->light

// Calculate viewer location in object space
auto osViewer = inverseObjectTransform.transformPoint(viewer);

// Set lighting parameters in the shader
glUniform3f(_locViewOrigin,
static_cast<float>(viewer.x()),
static_cast<float>(viewer.y()),
static_cast<float>(viewer.z())
static_cast<float>(osViewer.x()),
static_cast<float>(osViewer.y()),
static_cast<float>(osViewer.z())
);
glUniform3f(_locLightOrigin,
static_cast<float>(localLight.x()),
Expand Down Expand Up @@ -238,31 +267,6 @@ void GLSLBumpProgram::applyRenderParams(const Vector3& viewer,
debug::assertNoGlErrors();
}

void GLSLBumpProgram::setModelViewProjection(const Matrix4& modelViewProjection)
{
loadMatrixUniform(_locModelViewProjection, modelViewProjection);
}

void GLSLBumpProgram::setObjectTransform(const Matrix4& transform)
{
loadMatrixUniform(_locObjectTransform, transform);
}

void GLSLBumpProgram::setDiffuseTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locDiffuseTextureMatrix, transform);
}

void GLSLBumpProgram::setBumpTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locBumpTextureMatrix, transform);
}

void GLSLBumpProgram::setSpecularTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locSpecularTextureMatrix, transform);
}

}


Expand Down
11 changes: 6 additions & 5 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h
Expand Up @@ -47,15 +47,16 @@ class GLSLBumpProgram :
// The stage's vertex colour mode and colour as defined by the rgba registers
void setStageVertexColour(IShaderLayer::VertexColourMode vertexColourMode, const Colour4& stageColour);

void applyRenderParams(const Vector3& viewer,
const Matrix4& objectTransform,
const Matrix4& inverseObjectTransform,
const Params&);

void applyRenderParams(const Vector3& viewer,
const Matrix4& localToWorld,
const Params& lightParms) override
{ }

void setUpLightingCalculation(const Vector3& worldLightOrigin,
const Matrix4& worldToLight,
const Vector3& viewer,
const Matrix4& objectTransform,
const Matrix4& inverseObjectTransform);
};

} // namespace render
Expand Down

0 comments on commit 2df6b65

Please sign in to comment.