Debian Codegraph @

Shaders/ws30.frag

raw
1 // WS30 FRAGMENT SHADER
2
3 // -*-C++-*-
4 #version 130
5 #extension GL_EXT_texture_array : enable
6
7 varying vec3 normal;
8 varying vec4 ecPosition;
9
10 uniform sampler2D landclass;
11 uniform sampler2DArray atlas;
12
13 // Passed from VPBTechnique, not the Effect
14 uniform float fg_tileWidth;
15 uniform float fg_tileHeight;
16 uniform bool fg_photoScenery;
17 uniform vec4 fg_dimensionsArray[128];
18 uniform vec4 fg_ambientArray[128];
19 uniform vec4 fg_diffuseArray[128];
20 uniform vec4 fg_specularArray[128];
21 uniform vec4 fg_textureLookup1[128];
22 uniform vec4 fg_textureLookup2[128];
23 #define MAX_TEXTURES 8
24 uniform mat4 fg_zUpTransform;
25 uniform vec3 fg_modelOffset;
26
27 // See include_fog.frag
28 uniform int fogType;
29 vec3 fog_Func(vec3 color, int type);
30
31 // See Shaders/shadows-include.frag
32 float getShadowing();
33
34 // See Shaders/clustered-include.frag
35 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
36
37 void main()
38 {
39 float NdotL, NdotHV, fogFactor;
40 vec3 lightDir = gl_LightSource[0].position.xyz;
41 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
42 vec4 texel;
43 vec4 fragColor;
44 vec4 specular = vec4(0.0);
45
46 // Material properties.
47 vec4 mat_diffuse, mat_ambient, mat_specular;
48 float mat_shininess;
49
50 if (fg_photoScenery) {
51 mat_ambient = vec4(1.0,1.0,1.0,1.0);
52 mat_diffuse = vec4(1.0,1.0,1.0,1.0);
53 mat_specular = vec4(0.1, 0.1, 0.1, 1.0);
54 mat_shininess = 1.2;
55
56 texel = texture(landclass, vec2(gl_TexCoord[0].s, 1.0 - gl_TexCoord[0].t));
57 } else {
58 // The Landclass for this particular fragment. This can be used to
59 // index into the atlas textures.
60 int lc = int(texture2D(landclass, gl_TexCoord[0].st).g * 255.0 + 0.5);
61
62 // Color Mode is always AMBIENT_AND_DIFFUSE, which means
63 // using a base colour of white for ambient/diffuse,
64 // rather than the material color from ambientArray/diffuseArray.
65 mat_ambient = vec4(1.0,1.0,1.0,1.0);
66 mat_diffuse = vec4(1.0,1.0,1.0,1.0);
67 mat_specular = fg_specularArray[lc];
68 mat_shininess = fg_dimensionsArray[lc].z;
69
70 // Different textures have different have different dimensions.
71 vec2 atlas_dimensions = fg_dimensionsArray[lc].st;
72 vec2 atlas_scale = vec2(fg_tileWidth / atlas_dimensions.s, fg_tileHeight / atlas_dimensions.t );
73
74 texel = texture(atlas, vec3(atlas_scale * gl_TexCoord[0].st, int(texture2D(landclass, gl_TexCoord[0].st).g * 255.0 + 0.5)));
75 }
76
77 vec4 color = mat_ambient * (gl_LightModel.ambient + gl_LightSource[0].ambient);
78
79 // If gl_Color.a == 0, this is a back-facing polygon and the
80 // normal should be reversed.
81 vec3 n = (2.0 * gl_Color.a - 1.0) * normal;
82 n = normalize(n);
83 NdotL = dot(n, lightDir);
84
85 if (NdotL > 0.0) {
86 float shadowmap = getShadowing();
87 color += mat_diffuse * NdotL * shadowmap;
88 NdotHV = max(dot(n, halfVector), 0.0);
89 if (mat_shininess > 0.0)
90 specular.rgb = (mat_specular.rgb
91 * gl_LightSource[0].specular.rgb
92 * pow(NdotHV, mat_shininess)
93 * shadowmap);
94 }
95 color.a = mat_diffuse.a;
96
97 // This shouldn't be necessary, but our lighting becomes very
98 // saturated. Clamping the color before modulating by the texture
99 // is closer to what the OpenGL fixed function pipeline does.
100 color = clamp(color, 0.0, 1.0);
101
102 fragColor = color * texel + specular;
103 fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
104
105 fragColor.rgb = fog_Func(fragColor.rgb, fogType);
106 gl_FragColor = fragColor;
107 }