UFO: Alien Invasion
Doxygen documentation generating
model_med_fs.glsl
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Medium quality battlescape model 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 vec3 AMBIENT;
17 uniform vec3 SUNCOLOR;
18 
19 in_qualifier vec3 sunDir; /** < Direction towards the sun */
20 
21 /** Diffuse texture.*/
22 uniform sampler2D SAMPLER_DIFFUSE;
23 /** Specularmap.*/
24 uniform sampler2D SAMPLER_SPECULAR;
25 /** Normalmap.*/
26 uniform sampler2D SAMPLER_NORMALMAP;
27 
28 #define R_DYNAMIC_LIGHTS #replace r_dynamic_lights
29 #if r_dynamic_lights
30 in_qualifier vec3 lightDirs[R_DYNAMIC_LIGHTS];
31 uniform vec4 LIGHTPARAMS[R_DYNAMIC_LIGHTS];
32 #endif
33 
34 #include "light_fs.glsl"
35 #include "bump_fs.glsl"
36 #include "fog_fs.glsl"
37 #include "model_devtools_fs.glsl"
38 #include "write_fragment_fs.glsl"
39 
40 /**
41  * @brief main
42  */
43 void main(void) {
44  vec4 finalColor;
45  vec3 specular;
46  vec4 specularmap = vec4(HARDNESS, HARDNESS, HARDNESS, SPECULAR);
47 
48  vec3 sun = normalize(sunDir);
49  vec3 normal = vec3(0.0, 0.0, 1.0);
50  vec2 offset = vec2(0.0);
51 
52  if (BUMPMAP > 0) {
53  vec4 normalmap = texture2D(SAMPLER_NORMALMAP, gl_TexCoord[0].st);
54  normal = normalmap.rgb * 2.0 - 1.0; /** < Expanded normal */
55  normal.xy *= BUMP;
56  normal = normalize(normal);
57 
58  /* Resolve parallax offset */
59  offset = BumpTexcoord(normalmap.a);
60  }
61 
62  /* Sample the diffuse texture, honoring the parallax offset.*/
63  vec4 diffusemap = texture2D(SAMPLER_DIFFUSE, gl_TexCoord[0].st + offset);
64 
65  /* Sample specularity map, if any */
66  if (SPECULARMAP > 0) {
67  specularmap = texture2D(SAMPLER_SPECULAR, gl_TexCoord[0].st + offset);
68  }
69 
70  /* Lambert illumination model */
71  vec3 light = AMBIENT + SUNCOLOR * clamp(dot(sun, normal), 0.0, 1.0);
72 
73  /* Calculate specular component via Phong model */
74  vec3 V = reflect(-normalize(eyedir), normal);
75  float LdotV = dot(V, sun);
76  specular = SUNCOLOR * specularmap.rgb * pow(max(LdotV, 0.0), specularmap.a * 512.0);
77 
78  /* Calculate dynamic lights (if any) */
79  light = clamp(light + LightFragment(normal), 0.0, 2.0);
80 
81  finalColor.rgb = diffusemap.rgb * light + specular;
82  finalColor.a = diffusemap.a;
83 
84 #if r_fog
85  /* Add fog.*/
86  finalColor = FogFragment(finalColor);
87 #endif
88 
89  /* Developer tools, if enabled */
90  finalColor = ApplyDeveloperTools(finalColor, sunDir, normal);
91 
92  writeFragment(finalColor);
93 }