UFO: Alien Invasion
Loading...
Searching...
No Matches
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
29in_qualifier vec2 tex;
30
31in_qualifier vec4 ambientLight;
32in_qualifier vec4 diffuseLight;
33in_qualifier vec4 specularLight;
34
35in_qualifier vec3 lightVec;
36in_qualifier vec3 eyeVec;
37
38/** Diffuse.*/
39uniform sampler2D SAMPLER_DIFFUSE;
40/** Normalmap.*/
41uniform sampler2D SAMPLER_NORMALMAP;
42
43uniform float GLOWSCALE;
44uniform vec4 DEFAULTCOLOR;
45
46const float specularExp = 32.0;
47
48/**
49 * @brief Fresnel's equations for reflection and refraction between different density media.
50 */
51void 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
66void 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}