1
0
Fork 0
fgdata/Shaders/ws30.frag

103 lines
3 KiB
GLSL
Raw Normal View History

// WS30 FRAGMENT SHADER
// -*-C++-*-
#version 130
#extension GL_EXT_texture_array : enable
varying vec3 normal;
varying vec4 ecPosition;
uniform sampler2D landclass;
uniform sampler2DArray atlas;
// Passed from VPBTechnique, not the Effect
uniform float tile_width;
uniform float tile_height;
2021-11-09 20:27:23 +00:00
uniform bool photoScenery;
uniform vec4 dimensionsArray[128];
uniform vec4 ambientArray[128];
uniform vec4 diffuseArray[128];
uniform vec4 specularArray[128];
2021-01-23 17:15:12 +00:00
// See include_fog.frag
uniform int fogType;
vec3 fog_Func(vec3 color, int type);
// See Shaders/shadows-include.frag
float getShadowing();
// See Shaders/clustered-include.frag
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
void main()
{
float NdotL, NdotHV, fogFactor;
2021-01-23 17:15:12 +00:00
vec3 lightDir = gl_LightSource[0].position.xyz;
vec3 halfVector = gl_LightSource[0].halfVector.xyz;
vec4 texel;
vec4 fragColor;
vec4 specular = vec4(0.0);
// Material properties.
vec4 mat_diffuse, mat_ambient, mat_specular;
float mat_shininess;
2021-11-09 20:27:23 +00:00
if (photoScenery) {
mat_ambient = vec4(1.0,1.0,1.0,1.0);
mat_diffuse = vec4(1.0,1.0,1.0,1.0);
mat_specular = vec4(0.1, 0.1, 0.1, 1.0);
mat_shininess = 1.2;
2021-11-09 20:27:23 +00:00
texel = texture(landclass, vec2(gl_TexCoord[0].s, 1.0 - gl_TexCoord[0].t));
} else {
// 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);
// Color Mode is always AMBIENT_AND_DIFFUSE, which means
// using a base colour of white for ambient/diffuse,
// rather than the material color from ambientArray/diffuseArray.
mat_ambient = vec4(1.0,1.0,1.0,1.0);
mat_diffuse = vec4(1.0,1.0,1.0,1.0);
mat_specular = specularArray[lc];
mat_shininess = dimensionsArray[lc].z;
2021-11-09 20:27:23 +00:00
// Different textures have different have different dimensions.
vec2 atlas_dimensions = dimensionsArray[lc].st;
2021-11-09 20:27:23 +00:00
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));
}
vec4 color = mat_ambient * (gl_LightModel.ambient + gl_LightSource[0].ambient);
// 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);
NdotL = dot(n, lightDir);
if (NdotL > 0.0) {
float shadowmap = getShadowing();
color += mat_diffuse * NdotL * shadowmap;
NdotHV = max(dot(n, halfVector), 0.0);
if (mat_shininess > 0.0)
specular.rgb = (mat_specular.rgb
* gl_LightSource[0].specular.rgb
* pow(NdotHV, mat_shininess)
* shadowmap);
}
color.a = mat_diffuse.a;
// This shouldn't be necessary, but our lighting becomes very
// saturated. Clamping the color before modulating by the texture
// is closer to what the OpenGL fixed function pipeline does.
color = clamp(color, 0.0, 1.0);
fragColor = color * texel + specular;
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
2021-01-23 17:15:12 +00:00
fragColor.rgb = fog_Func(fragColor.rgb, fogType);
gl_FragColor = fragColor;
}