openclonk/planet/Graphics.ocg/LightShader.glsl

100 lines
2.9 KiB
GLSL

// Base light calculations
#ifdef HAVE_LIGHT
uniform sampler2D lightTex;
#endif
uniform float cullMode; // 0 if backface culling is enabled, 1 if it is disabled
// uncomment the following lines for debugging light directions:
// yellow: light up, blue: light down, turqoise: light right, pink: light left
// brightness: light strength
//#define LIGHT_DEBUG
// uncomment the following lines for debugging light color:
// the light will always come from the front and have a uniform brightness.
//#define LIGHT_DEBUG_COLOR
// uncomment the following lines to set the light color to pink for all lights for debugging:
//#define LIGHT_DEBUG_PINK
// At what point of light intensity we set the "darkness" point. This
// is to compensate for the fact that the engine "smoothes" the light
// and therefore will often never arrive at 0 light intensity.
const float lightDarknessLevel = 8.0 / 256.0;
slice(texture+5)
{
#ifdef HAVE_LIGHT
// Query light texture
vec2 lightDirCoord = lightCoord.st;
vec4 lightPx = texture2D(lightTex, lightDirCoord);
float lightBright = max(0.0, lightPx.a-lightDarknessLevel);
vec3 lightDir = extend_normal(vec2(1.0, 1.0) - lightPx.yz * 3.0);
// Query light color texture (part of the light texture)
vec2 lightColorCoord = lightCoord.st - vec2(0.0, 0.5); // subtract offset for the color texture
vec4 lightColor = texture2D(lightTex, lightColorCoord.st);
#ifdef LIGHT_DEBUG_COLOR
lightBright = 0.5;
lightDir = vec3(0.0, 0.0, 1.0);
#endif
#else
// When lighting is disabled, put a light source coming from the camera.
// Note that in most cases this does not actually matter, since in the
// case with lighting disabled, ambient lighting takes fully over.
float lightBright = 0.5;
vec3 lightDir = vec3(0.0, 0.0, 1.0);
vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0);
#endif
}
slice(light)
{
float light = 2.0 * lightBright * max(max(dot(normal, lightDir), 0.0), cullMode * max(dot(-normal, lightDir), 0.0));
#ifdef HAVE_2PX
float light2 = 2.0 * lightBright * max(max(dot(normal2, lightDir), 0.0), cullMode * max(dot(-normal2, lightDir), 0.0));
#endif
}
slice(color+5)
{
// pink shade for debugging!
#ifdef LIGHT_DEBUG_PINK
lightColor = vec4(1.0, 0.0, 1.0, 1.0);
#endif
lightColor.rgb = sqrt(3.0) * normalize(lightColor.rgb);
// Add light
color.rgb = light * color.rgb * lightColor.rgb;
#ifdef HAVE_2PX
color2.rgb = light2 * color2.rgb * lightColor.rgb;
#endif
}
slice(finish+5)
{
#ifdef LIGHT_DEBUG
#ifdef HAVE_LIGHT
float lightYDir = lightPx.b - 1.0/3.0;
float lightXDir = lightPx.g - 1.0/3.0;
float lightStrength = lightPx.a;
color =
vec4(lightStrength * vec3(1.0-1.5*(max(0.0, lightYDir) + max(0.0,lightXDir)),
1.0-1.5*(max(0.0, lightYDir) + max(0.0,-lightXDir)),
1.0-1.5*max(0.0, -lightYDir)),
1.0);
#else
color = vec4(0.0, 0.0, 0.0, 0.0); // invisible
#endif
#endif
}