1
0
Fork 0
fgdata/Shaders/ws30.frag
Stuart Buchanan bf55997a56 WS30: Improved ambient color
Previous we just applied gl_LightSource[0].ambient without
multiplying it by the texture itself, so it just lightened
everything.

This fixes that.  (ws30-ALS-detailed.frag still to do)
2021-11-05 19:55:05 +00:00

54 lines
1.8 KiB
C++

// WS30 FRAGMENT SHADER
// -*-C++-*-
#version 130
#extension GL_EXT_texture_array : enable
varying vec3 normal;
uniform sampler2D landclass;
uniform sampler2DArray atlas;
uniform sampler1D dimensionsArray;
uniform sampler1D diffuseArray;
uniform sampler1D specularArray;
// Passed from VPBTechnique, not the Effect
uniform float tile_width;
uniform float tile_height;
// See include_fog.frag
uniform int fogType;
vec3 fog_Func(vec3 color, int type);
void main()
{
vec3 lightDir = gl_LightSource[0].position.xyz;
vec3 halfVector = gl_LightSource[0].halfVector.xyz;
vec4 texel;
vec4 fragColor;
// The Landclass for this particular fragment. This can be used to
// index into the atlas textures.
int lc = int(texture2D(landclass, gl_TexCoord[0].st).g * 255.0 + 0.5);
// If gl_Color.a == 0, this is a back-facing polygon and the
// normal should be reversed.
vec3 n = (2.0 * gl_Color.a - 1.0) * normal;
n = normalize(n);
float NdotL = dot(n, lightDir);
float NdotHV = max(dot(n, halfVector), 0.0);
// Different textures have different have different dimensions.
// Dimensions array is scaled to fit in [0...1.0] in the texture1D, so has to be scaled back up here.
vec4 color = texture(diffuseArray, float(lc)/512.0) * (gl_LightSource[0].ambient + NdotL * gl_LightSource[0].diffuse);
vec4 specular = texture(specularArray, float(lc)/512.0);
vec2 atlas_dimensions = 10000.0 * texture(dimensionsArray, float(lc)/512.0).st;
vec2 atlas_scale = vec2(tile_width / atlas_dimensions.s, tile_height / atlas_dimensions.t );
texel = texture(atlas, vec3(atlas_scale * gl_TexCoord[0].st, lc));
fragColor = color * texel + pow(NdotHV, gl_FrontMaterial.shininess) * gl_LightSource[0].specular * specular;
fragColor.rgb = fog_Func(fragColor.rgb, fogType);
gl_FragColor = fragColor;
}