Skip to content

Commit

Permalink
Merge pull request #8 from Tokeiburu/global-light-fix
Browse files Browse the repository at this point in the history
Fix for sun lighting on maps
  • Loading branch information
Borf authored Nov 30, 2023
2 parents a5d067a + 58aaa12 commit f0f598c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
9 changes: 5 additions & 4 deletions browedit/components/GndRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ void GndRenderer::render()
shader->setUniform(GndShader::Uniforms::ModelViewMatrix, dynamic_cast<GndRenderContext*>(renderContext)->viewMatrix);


glm::vec3 lightDirection;
lightDirection[0] = -glm::cos(glm::radians((float)rsw->light.longitude)) * glm::sin(glm::radians((float)rsw->light.latitude));
lightDirection[1] = glm::cos(glm::radians((float)rsw->light.latitude));
lightDirection[2] = glm::sin(glm::radians((float)rsw->light.longitude)) * glm::sin(glm::radians((float)rsw->light.latitude));
glm::vec3 lightDirection(0.0f, 1.0f, 0.0f);
glm::mat4 rot = glm::mat4(1.0f);
rot = glm::rotate(rot, glm::radians(-(float)rsw->light.latitude), glm::vec3(1, 0, 0));
rot = glm::rotate(rot, glm::radians((float)rsw->light.longitude), glm::vec3(0, 1, 0));
lightDirection = lightDirection * glm::mat3(rot);
shader->setUniform(GndShader::Uniforms::lightAmbient, rsw->light.ambient);
shader->setUniform(GndShader::Uniforms::lightDiffuse, rsw->light.diffuse);
shader->setUniform(GndShader::Uniforms::lightDirection, lightDirection);
Expand Down
7 changes: 4 additions & 3 deletions browedit/components/RsmRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ void RsmRenderer::render()
glm::vec3 lightDirection(1,1,1);
if (rsw)
{
lightDirection[0] = -glm::cos(glm::radians((float)rsw->light.longitude)) * glm::sin(glm::radians((float)rsw->light.latitude));
lightDirection[1] = glm::cos(glm::radians((float)rsw->light.latitude));
lightDirection[2] = glm::sin(glm::radians((float)rsw->light.longitude)) * glm::sin(glm::radians((float)rsw->light.latitude));
glm::mat4 rot = glm::mat4(1.0f);
rot = glm::rotate(rot, glm::radians(-(float)rsw->light.latitude), glm::vec3(1, 0, 0));
rot = glm::rotate(rot, glm::radians((float)rsw->light.longitude), glm::vec3(0, 1, 0));
lightDirection = glm::vec3(0.0f, 1.0f, 0.0f) * glm::mat3(rot);
shader->setUniform(RsmShader::Uniforms::lightAmbient, rsw->light.ambient);
shader->setUniform(RsmShader::Uniforms::lightDiffuse, rsw->light.diffuse);
shader->setUniform(RsmShader::Uniforms::lightIntensity, rsw->light.intensity);
Expand Down
8 changes: 3 additions & 5 deletions data/shaders/gnd.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ void main()
texture = mix(vec4(1,1,1,texColor.a), texColor, viewTextures);
if(texture.a < 0.1)
discard;


texture.rgb *= max((max(0.0, dot(normal, vec3(1,-1,1)*lightDirection)) * lightDiffuse + lightIntensity * lightAmbient), lightToggle);
texture.rgb *= max(color, colorToggle).rgb;
texture.rgb *= max(texture2D(s_lighting, texCoord2).a, shadowMapToggle);


texture.rgb *= max((max(0.0, dot(normal, vec3(-1,-1,1)*lightDirection)) * lightDiffuse + lightIntensity * lightAmbient), lightToggle);
texture += clamp(vec4(texture2D(s_lighting, texCoord2).rgb,1.0), 0.0, 1.0) * lightColorToggle;
texture.rgb += clamp(texture2D(s_lighting, texCoord2).rgb, 0.0, 1.0) * lightColorToggle;

if(fogEnabled)
{
Expand Down
16 changes: 10 additions & 6 deletions data/shaders/rsm.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ void main()
if(color.a < 0.1)
discard;

if(shadeType == 1 || shadeType == 2 && lightToggle)
color.rgb *= lightIntensity * clamp(dot(normalize(normal), lightDirection),0.0,1.0) * lightDiffuse + lightAmbient;

if(shadeType == 4 && lightToggle) // only for editor
color.rgb *= lightDiffuse;

if (lightToggle) {
// lightIntensity is ignored by the client for whatever reason
if (shadeType == 0)
color.rgb *= min(lightDiffuse, 1.0 - lightAmbient) * lightDiffuse + lightAmbient;
else if (shadeType == 1 || shadeType == 2)
color.rgb *= clamp(dot(normalize(normal), lightDirection),0.0,1.0) * lightDiffuse + lightAmbient;
else if (shadeType == 4) // only for editor
color.rgb *= lightDiffuse;
}

if(fogEnabled)
{
float depth = gl_FragCoord.z / gl_FragCoord.w;
Expand Down

0 comments on commit f0f598c

Please sign in to comment.