UFO: Alien Invasion
Doxygen documentation generating
atmosphere_fs.glsl
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Atmosphere fragment shader.
4  */
5 
6 /*
7  * Indicates that gl_FragData is written to, not gl_FragColor.
8  * #extension needs to be placed before all non preprocessor code.
9  */
10 #if r_postprocess
11  /*
12  * Indicates that gl_FragData is written to, not gl_FragColor.
13  * #extension needs to be placed before all non preprocessor code.
14  */
15  #extension GL_ARB_draw_buffers : enable
16 #endif
17 
18 #ifndef glsl110
19  #if r_postprocess
20  /** After glsl1110 this need to be explicitly declared; used by fixed functionality at the end of the OpenGL pipeline.*/
21  out vec4 gl_FragData[2];
22  #else
23  /** After glsl1110 this need to be explicitly declared; used by fixed functionality at the end of the OpenGL pipeline.*/
24  out vec4 gl_FragColor;
25  #endif
26 #endif
27 
28 
29 in_qualifier vec2 tex;
30 
31 in_qualifier vec4 ambientLight;
32 in_qualifier vec4 diffuseLight;
33 in_qualifier vec4 specularLight;
34 
35 in_qualifier vec3 lightVec;
36 in_qualifier vec3 eyeVec;
37 
38 /** Diffuse.*/
39 uniform sampler2D SAMPLER_DIFFUSE;
40 /** Normalmap.*/
41 uniform sampler2D SAMPLER_NORMALMAP;
42 
43 uniform float GLOWSCALE;
44 uniform vec4 DEFAULTCOLOR;
45 
46 const float specularExp = 32.0;
47 
48 /**
49  * @brief Fresnel's equations for reflection and refraction between different density media.
50  */
51 void fresnelRefract(vec3 L, vec3 N, float n1, float n2,
52  out vec3 reflection, out vec3 refraction,
53  out float reflectance, out float transmittance) {
54 
55  float eta = n1/n2;
56  float cos_theta1 = dot(L, N);
57  float cos_theta2 = sqrt(1.0 - ((eta * eta) * ( 1.0 - (cos_theta1 * cos_theta1))));
58  reflection = L - 2.0 * cos_theta1 * N;
59  refraction = (eta * L) + (cos_theta2 - eta * cos_theta1) * N;
60  float rs = (n1 * cos_theta1 - n2 * cos_theta2 ) / (n1 * cos_theta1 + n2 * cos_theta2);
61  float rp = (n1 * cos_theta2 - n2 * cos_theta1 ) / (n1 * cos_theta2 + n2 * cos_theta1);
62  reflectance = (rs * rs + rp * rp) / 2.0;
63  transmittance = ((1.0-rs) * (1.0-rs) + (1.0-rp) * (1.0-rp)) / 2.0;
64 }
65 
66 void main() {
67  vec3 diffuseColor = texture2D(SAMPLER_DIFFUSE, tex).rgb;
68  vec3 V = normalize(eyeVec).rgb;
69  vec3 L = normalize(lightVec).rgb;
70  vec3 N = normalize(texture2D(SAMPLER_NORMALMAP, tex).rgb * 2.0 - 1.0);
71  /* calculate reflections/refractions */
72  vec3 Rvec;
73  vec3 Tvec;
74  float R;
75  float T;
76  fresnelRefract(L, N, 1.0, 1.133, Rvec, Tvec, R, T);
77 
78  float RdotV = clamp(R * dot(Rvec, -V), 0.0, 1.0);
79  float TdotV = clamp(T * ((dot(Tvec, -V) + 1.0) / 2.0), 0.0, 1.0);
80  float MTdotV = clamp(T * ((dot(-Tvec, -V) + 1.0) / 2.0), 0.0, 1.0);
81  float NdotL = clamp(((dot(N, L) + 1.0) / 2.0), 0.0, 1.0);
82  float LNdotV = clamp(dot(reflect(-L, N), V), 0.0, 1.0);
83  float VdotL = clamp(((dot(L, -V) + 1.0) / 2.0), 0.0, 1.0);
84 
85  vec3 ambient = vec3(0.05, 0.05, 0.05);
86 
87  /* calculate reflections */
88  vec3 reflectColor = diffuseColor * (ambient + pow(NdotL, 4.0) * (TdotV + MTdotV) + 0.2 * pow(VdotL, 16.0));
89 
90  float d = clamp(pow(1.0 + dot(V, L), 0.4), 0.0, 1.0);
91  vec3 specularColor = d * RdotV * pow(LNdotV, specularExp) * specularLight.rgb;
92 
93  vec3 hdrColor = GLOWSCALE * (0.4 * reflectColor + 1.0 * specularColor);
94 
95  /* calculate final color */
96 #if r_postprocess
97  gl_FragData[0] = DEFAULTCOLOR;
98  gl_FragData[1].rgb = hdrColor;
99  gl_FragData[1].a = 1.0;
100 #else
101  gl_FragColor.rgb = DEFAULTCOLOR.rgb + hdrColor;
102  gl_FragColor.a = DEFAULTCOLOR.a;
103 #endif
104 }