3 * @brief Atmosphere fragment shader.
7 * Indicates that gl_FragData is written to, not gl_FragColor.
8 * #extension needs to be placed before all non preprocessor code.
12 * Indicates that gl_FragData is written to, not gl_FragColor.
13 * #extension needs to be placed before all non preprocessor code.
15 #extension GL_ARB_draw_buffers : enable
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];
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;
31in_qualifier vec4 ambientLight;
32in_qualifier vec4 diffuseLight;
33in_qualifier vec4 specularLight;
35in_qualifier vec3 lightVec;
36in_qualifier vec3 eyeVec;
39uniform sampler2D SAMPLER_DIFFUSE;
41uniform sampler2D SAMPLER_NORMALMAP;
43uniform float GLOWSCALE;
44uniform vec4 DEFAULTCOLOR;
46const float specularExp = 32.0;
49 * @brief Fresnel's equations for reflection and refraction between different density media.
51void fresnelRefract(vec3 L, vec3 N, float n1, float n2,
52 out vec3 reflection, out vec3 refraction,
53 out float reflectance, out float transmittance) {
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;
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 */
76 fresnelRefract(L, N, 1.0, 1.133, Rvec, Tvec, R, T);
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);
85 vec3 ambient = vec3(0.05, 0.05, 0.05);
87 /* calculate reflections */
88 vec3 reflectColor = diffuseColor * (ambient + pow(NdotL, 4.0) * (TdotV + MTdotV) + 0.2 * pow(VdotL, 16.0));
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;
93 vec3 hdrColor = GLOWSCALE * (0.4 * reflectColor + 1.0 * specularColor);
95 /* calculate final color */
97 gl_FragData[0] = DEFAULTCOLOR;
98 gl_FragData[1].rgb = hdrColor;
99 gl_FragData[1].a = 1.0;
101 gl_FragColor.rgb = DEFAULTCOLOR.rgb + hdrColor;
102 gl_FragColor.a = DEFAULTCOLOR.a;