Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify vertex colour handling in interaction shader
Modern shaders can work fine with boolean uniforms, so we don't need the
awkward "scale and offset" approach to vertex colour handling. Instead we just
examine a boolean to determine if the vertex colour should be inverted, and
perform the calculation accordingly.
  • Loading branch information
Matthew Mott committed Mar 8, 2021
1 parent 4d19f81 commit 0b00b9f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
10 changes: 3 additions & 7 deletions install/gl/interaction_fp.glsl
Expand Up @@ -30,12 +30,8 @@ uniform vec3 u_light_origin;
uniform vec3 u_light_color;
uniform float u_light_scale;

// Vertex colour parameters. Vertex colour is calculated as
// (colour * scale + offset), so for normal vertex colouring scale should be 1.0
// and offset 0.0, and for inverse colouring scale should be -1.0 and offset
// 1.0.
uniform float u_vcol_scale;
uniform float u_vcol_offset;
// Invert vertex colour
uniform bool uInvertVCol;

varying vec3 var_vertex;
varying vec4 var_tex_diffuse_bump;
Expand Down Expand Up @@ -82,7 +78,7 @@ void main()
).rgb;

// compute final color
gl_FragColor = diffuse * (gl_Color * u_vcol_scale + u_vcol_offset);
gl_FragColor = diffuse * (uInvertVCol ? vec4(1.0, 1.0, 1.0, 1.0) - gl_Color : gl_Color);
gl_FragColor.rgb *= 2.0; // replacement for RENDER_SCREEN light scaling
gl_FragColor.rgb += specular;
gl_FragColor.rgb *= attenuation_xy;
Expand Down
30 changes: 10 additions & 20 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp
Expand Up @@ -55,8 +55,7 @@ void GLSLBumpProgram::create()
_locLightColour = glGetUniformLocation(_programObj, "u_light_color");
_locViewOrigin = glGetUniformLocation(_programObj, "u_view_origin");
_locLightScale = glGetUniformLocation(_programObj, "u_light_scale");
_locVColScale = glGetUniformLocation(_programObj, "u_vcol_scale");
_locVColOffset = glGetUniformLocation(_programObj, "u_vcol_offset");
_locInvertVCol = glGetUniformLocation(_programObj, "uInvertVCol");

// 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 @@ -139,35 +138,26 @@ void GLSLBumpProgram::applyRenderParams(const Vector3& viewer,
local2light.multiplyBy(objectToWorld); // local->world->light

// Set lighting parameters in the shader
glUniform3f(_locViewOrigin,
static_cast<float>(viewer.x()),
static_cast<float>(viewer.y()),
glUniform3f(_locViewOrigin,
static_cast<float>(viewer.x()),
static_cast<float>(viewer.y()),
static_cast<float>(viewer.z())
);
glUniform3f(_locLightOrigin,
static_cast<float>(localLight.x()),
static_cast<float>(localLight.y()),
glUniform3f(_locLightOrigin,
static_cast<float>(localLight.x()),
static_cast<float>(localLight.y()),
static_cast<float>(localLight.z())
);
glUniform3f(
_locLightColour,
static_cast<float>(parms.lightColour.x()),
static_cast<float>(parms.lightColour.y()),
static_cast<float>(parms.lightColour.x()),
static_cast<float>(parms.lightColour.y()),
static_cast<float>(parms.lightColour.z())
);
glUniform1f(_locLightScale, _lightScale);

// Set vertex colour parameters
if (parms.invertVertexColour)
{
glUniform1f(_locVColScale, -1.0f);
glUniform1f(_locVColOffset, 1.0f);
}
else
{
glUniform1f(_locVColScale, 1.0f);
glUniform1f(_locVColOffset, 0.0f);
}
glUniform1i(_locInvertVCol, parms.invertVertexColour);

glActiveTexture(GL_TEXTURE3);
glClientActiveTexture(GL_TEXTURE3);
Expand Down
3 changes: 1 addition & 2 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h
Expand Up @@ -19,8 +19,7 @@ class GLSLBumpProgram
int _locViewOrigin;
int _locLightScale;
int _locAmbientFactor;
int _locVColScale;
int _locVColOffset;
int _locInvertVCol;

// Program object identifier
GLuint _programObj;
Expand Down

0 comments on commit 0b00b9f

Please sign in to comment.