UFO: Alien Invasion
world_med_fs.glsl
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Medium quality battlescape fragment shader.
4  */
5 
6 #if r_postprocess
7  /*
8  * Indicates that gl_FragData is written to, not gl_FragColor.
9  * #extension needs to be placed before all non preprocessor code.
10  */
11  #extension GL_ARB_draw_buffers : enable
12 #endif
13 
14 uniform int BUMPMAP;
15 uniform int SPECULARMAP;
16 /*uniform float SPECULAR; specular exponent divided by 512 */
17 /*uniform float HARDNESS; specular component brightness (0 - no specular) */
18 uniform vec3 AMBIENT;
19 
20 /** Diffuse texture.*/
21 uniform sampler2D SAMPLER_DIFFUSE;
22 /** Specularmap.*/
23 uniform sampler2D SAMPLER_SPECULAR;
24 /** Lightmap.*/
25 uniform sampler2D SAMPLER_LIGHTMAP;
26 /** Deluxemap.*/
27 uniform sampler2D SAMPLER_DELUXEMAP;
28 /** Normalmap.*/
29 uniform sampler2D SAMPLER_NORMALMAP;
30 
31 #define R_DYNAMIC_LIGHTS #replace r_dynamic_lights
32 #if r_dynamic_lights
33 in_qualifier vec3 lightDirs[R_DYNAMIC_LIGHTS];
34 uniform vec4 LIGHTPARAMS[R_DYNAMIC_LIGHTS];
35 #endif
36 
37 #include "light_fs.glsl"
38 #include "bump_fs.glsl"
39 #include "fog_fs.glsl"
40 #include "world_devtools_fs.glsl"
41 #include "write_fragment_fs.glsl"
42 
43 in_qualifier vec4 blendColor;
44 
45 /**
46  * @brief main
47  */
48 void main(void) {
49  vec4 finalColor = vec4(0.0);
50  vec3 light = vec3(0.0);
51  vec3 specular;
52  vec3 deluxemap;
53  /* normalmap should be declared in this scope for developer tools to work */
54  vec4 normalmap = vec4(0.0, 0.0, 1.0, 0.5);
55  vec4 specularmap = vec4(HARDNESS, HARDNESS, HARDNESS, SPECULAR);
56 
57  vec2 offset = vec2(0.0);
58 
59  /* lightmap contains pre-computed incoming light color */
60  light = texture2D(SAMPLER_LIGHTMAP, gl_TexCoord[1].st).rgb;
61 
62  /* deluxemap contains pre-computed incoming light vectors in object tangent space */
63  deluxemap = texture2D(SAMPLER_DELUXEMAP, gl_TexCoord[1].st).rgb;
64  deluxemap = normalize(deluxemap * 2.0 - 1.0);
65 
66 #if r_bumpmap
67  if (BUMPMAP > 0) {
68  /* Sample normalmap.*/
69  normalmap = texture2D(SAMPLER_NORMALMAP, gl_TexCoord[0].st);
70  normalmap.rgb = normalize(normalmap.rgb * 2.0 - 1.0);
71 
72  /* Resolve parallax offset */
73  offset = BumpTexcoord(normalmap.a);
74  }
75 #endif
76 
77  /* Modulate incoming light by cos(angle_of_incidence); should be done even bump mapping is disabled, to avoid having flat shading as a result.*/
78  light *= clamp(dot(deluxemap, vec3(normalmap.x * BUMP, normalmap.y * BUMP, normalmap.z)), 0.0, 1.0);
79 
80  /* Sample the diffuse texture, honoring the parallax offset.*/
81  vec4 diffusemap = texture2D(SAMPLER_DIFFUSE, gl_TexCoord[0].st + offset);
82 
83  /* Sample specularity map, if any */
84  if (SPECULARMAP > 0) {
85  specularmap = texture2D(SAMPLER_SPECULAR, gl_TexCoord[0].st + offset);
86  }
87 
88  /* Calculate specular component via Phong model */
89  vec3 V = reflect(-normalize(eyedir), normalmap.rgb);
90  float LdotV = dot(V, deluxemap);
91  specular = clamp(light - AMBIENT, 0.0, 1.0) * specularmap.rgb * pow(max(LdotV, 0.0), specularmap.a * 512.0);
92 
93  /* Calculate dynamic lights (if any) */
94  light = clamp(light + LightFragment(normalmap.rgb), 0.0, 2.0);
95 
96  finalColor.rgb = diffusemap.rgb * light + specular;
97  finalColor.a = diffusemap.a;
98 
99  finalColor *= blendColor;
100 
101 #if r_fog
102  /* Add fog.*/
103  finalColor = FogFragment(finalColor);
104 #endif
105 
106  /* Developer tools, if enabled */
107  finalColor = ApplyDeveloperTools(finalColor, normalmap.rgb, light, deluxemap);
108 
109  writeFragment(finalColor);
110 }