Skip to content

Commit

Permalink
#108: ambient lights now render correctly
Browse files Browse the repository at this point in the history
Ambient lights had in fact never worked since the switch to GLSL, since the
ambient light code was only ever present in the old Cg shaders. The GLSL shader
is now updated to support ambient lights through a new bool uniform, which
works for both models and brushes.
  • Loading branch information
Matthew Mott committed Mar 8, 2021
1 parent 8cd54cd commit 07a10bb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
14 changes: 5 additions & 9 deletions include/iglprogram.h
Expand Up @@ -48,28 +48,24 @@ class GLProgram
/// Transformation from world space into light space
Matrix4 world2Light;

/// Amount of directionality, 0.0 for normal light, 1.0 for ambient
float ambientFactor;
/// True if this is an ambient light, false for a directional light
bool isAmbientLight = false;

/// Whether vertex colours should be inverted
bool invertVertexColour;
bool invertVertexColour = false;

Params(const Vector3& lightOrigin_,
const Colour4& lightColour_,
const Matrix4& world2Light_)
: lightOrigin(lightOrigin_),
lightColour(lightColour_),
world2Light(world2Light_),
ambientFactor(0.0),
invertVertexColour(false)
world2Light(world2Light_)
{

}
};

/**
* \brief
* Apply render parameters used by this program to OpenGL.
* \brief Apply render parameters used by this program to OpenGL.
*
* This method is invoked shortly before the renderable geometry is
* submitted for rendering; the GLProgram must apply to the GL state any
Expand Down
6 changes: 5 additions & 1 deletion install/gl/interaction_fp.glsl
Expand Up @@ -33,6 +33,9 @@ uniform float u_light_scale;
// Invert vertex colour
uniform bool uInvertVCol;

// Activate ambbient light mode (brightness unaffected by direction)
uniform bool uAmbientLight;

varying vec3 var_vertex;
varying vec4 var_tex_diffuse_bump;
varying vec2 var_tex_specular;
Expand All @@ -56,7 +59,8 @@ void main()

// compute the diffuse term
vec4 diffuse = texture2D(u_diffusemap, var_tex_diffuse_bump.st);
diffuse.rgb *= u_light_color * u_light_scale * clamp(dot(N, L), 0.0, 1.0);
float lightBrightness = uAmbientLight ? 1.0 : clamp(dot(N, L), 0.0, 1.0);
diffuse.rgb *= u_light_color * u_light_scale * lightBrightness;

// compute the specular term
float specIntensity = clamp(dot(N, H), 0.0, 1.0);
Expand Down
2 changes: 1 addition & 1 deletion radiantcore/rendersystem/backend/OpenGLShaderPass.cpp
Expand Up @@ -691,7 +691,7 @@ void OpenGLShaderPass::setUpLightingCalculation(OpenGLState& current,
GLProgram::Params parms(
light->getLightOrigin(), layer->getColour(), world2light
);
parms.ambientFactor = lightMat->isAmbientLight() ? 1.0f : 0.0f;
parms.isAmbientLight = lightMat->isAmbientLight();
parms.invertVertexColour = _glState.isColourInverted();

assert(current.glProgram);
Expand Down
Expand Up @@ -53,9 +53,10 @@ void GLSLBumpProgram::create()
// Set the uniform locations to the correct bound values
_locLightOrigin = glGetUniformLocation(_programObj, "u_light_origin");
_locLightColour = glGetUniformLocation(_programObj, "u_light_color");
_locViewOrigin = glGetUniformLocation(_programObj, "u_view_origin");
_locLightScale = glGetUniformLocation(_programObj, "u_light_scale");
_locInvertVCol = glGetUniformLocation(_programObj, "uInvertVCol");
_locViewOrigin = glGetUniformLocation(_programObj, "u_view_origin");
_locLightScale = glGetUniformLocation(_programObj, "u_light_scale");
_locInvertVCol = glGetUniformLocation(_programObj, "uInvertVCol");
_locAmbientLight = glGetUniformLocation(_programObj, "uAmbientLight");

// Set up the texture uniforms. The renderer uses fixed texture units for
// particular textures, so make sure they are correct here.
Expand Down Expand Up @@ -155,6 +156,7 @@ void GLSLBumpProgram::applyRenderParams(const Vector3& viewer,
static_cast<float>(parms.lightColour.z())
);
glUniform1f(_locLightScale, _lightScale);
glUniform1i(_locAmbientLight, parms.isAmbientLight);

// Set vertex colour parameters
glUniform1i(_locInvertVCol, parms.invertVertexColour);
Expand Down
Expand Up @@ -18,7 +18,7 @@ class GLSLBumpProgram
int _locLightColour;
int _locViewOrigin;
int _locLightScale;
int _locAmbientFactor;
int _locAmbientLight;
int _locInvertVCol;

// Program object identifier
Expand Down

0 comments on commit 07a10bb

Please sign in to comment.