| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
// written by Thorsten Renk, Oct 2015 |
| 5 |
|
| 6 |
varying vec4 diffuse_term; |
| 7 |
varying vec3 normal; |
| 8 |
varying vec3 relPos; |
| 9 |
varying vec4 ecPosition; |
| 10 |
|
| 11 |
|
| 12 |
uniform sampler2D texture; |
| 13 |
uniform samplerCube cube_texture; |
| 14 |
|
| 15 |
varying float yprime_alt; |
| 16 |
varying float mie_angle; |
| 17 |
|
| 18 |
|
| 19 |
uniform float visibility; |
| 20 |
uniform float avisibility; |
| 21 |
uniform float scattering; |
| 22 |
uniform float terminator; |
| 23 |
uniform float terrain_alt; |
| 24 |
uniform float hazeLayerAltitude; |
| 25 |
uniform float overcast; |
| 26 |
uniform float eye_alt; |
| 27 |
uniform float cloud_self_shading; |
| 28 |
uniform float angle; |
| 29 |
uniform float threshold_low; |
| 30 |
uniform float threshold_high; |
| 31 |
uniform float emit_intensity; |
| 32 |
uniform float light_radius; |
| 33 |
|
| 34 |
uniform vec3 offset_vec; |
| 35 |
uniform vec3 scale_vec; |
| 36 |
uniform vec3 tag_color; |
| 37 |
uniform vec3 emit_color; |
| 38 |
uniform vec3 light_filter_one; |
| 39 |
uniform vec3 light_filter_two; |
| 40 |
|
| 41 |
uniform int quality_level; |
| 42 |
uniform int tquality_level; |
| 43 |
uniform int use_searchlight; |
| 44 |
uniform int implicit_lightmap_enabled; |
| 45 |
uniform int use_flashlight; |
| 46 |
|
| 47 |
uniform bool shadow_mapping_enabled; |
| 48 |
|
| 49 |
|
| 50 |
const float EarthRadius = 5800000.0; |
| 51 |
const float terminator_width = 200000.0; |
| 52 |
|
| 53 |
float alt; |
| 54 |
float eShade; |
| 55 |
|
| 56 |
|
| 57 |
float fog_func (in float targ, in float alt); |
| 58 |
float alt_factor(in float eye_alt, in float vertex_alt); |
| 59 |
float light_distance_fading(in float dist); |
| 60 |
float fog_backscatter(in float avisibility); |
| 61 |
|
| 62 |
vec3 get_hazeColor(in float light_arg); |
| 63 |
vec3 flashlight(in vec3 color, in float radius); |
| 64 |
vec3 filter_combined (in vec3 color) ; |
| 65 |
|
| 66 |
float getShadowing(); |
| 67 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel); |
| 68 |
|
| 69 |
float luminance(vec3 color) |
| 70 |
{ |
| 71 |
return dot(vec3(0.212671, 0.715160, 0.072169), color); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 76 |
{ |
| 77 |
x = x - 0.5; |
| 78 |
|
| 79 |
// use the asymptotics to shorten computations |
| 80 |
if (x > 30.0) {return e;} |
| 81 |
if (x < -15.0) {return 0.0;} |
| 82 |
|
| 83 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 84 |
} |
| 85 |
|
| 86 |
// this determines how light is attenuated in the distance |
| 87 |
// physically this should be exp(-arg) but for technical reasons we use a sharper cutoff |
| 88 |
// for distance > visibility |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void main() |
| 94 |
{ |
| 95 |
|
| 96 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 97 |
// this is taken from default.frag |
| 98 |
vec3 n; |
| 99 |
float NdotL, NdotHV, fogFactor; |
| 100 |
vec4 color = gl_Color; |
| 101 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 102 |
vec3 halfVector = gl_LightSource[0].halfVector.xyz; |
| 103 |
vec4 texel; |
| 104 |
vec4 fragColor; |
| 105 |
vec4 specular = vec4(0.0); |
| 106 |
float intensity; |
| 107 |
|
| 108 |
float effective_scattering = min(scattering, cloud_self_shading); |
| 109 |
|
| 110 |
eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt); |
| 111 |
vec4 light_specular = gl_LightSource[0].specular * (eShade - 0.1); |
| 112 |
|
| 113 |
// If gl_Color.a == 0, this is a back-facing polygon and the |
| 114 |
// normal should be reversed. |
| 115 |
n = (2.0 * gl_Color.a - 1.0) * normal; |
| 116 |
n = normalize(n); |
| 117 |
|
| 118 |
// lookup on the opacity map |
| 119 |
vec3 light_vec = normalize((gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz); |
| 120 |
//vec3 light_vec = vec3 (-1.0,0.0,0.0); |
| 121 |
|
| 122 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 123 |
vec3 scaled_pos = relPos + ep.xyz; |
| 124 |
|
| 125 |
//vec3 lookup_vec = normalize(- normalize(light_vec) + relPos); |
| 126 |
scaled_pos -= offset_vec; |
| 127 |
float rangle = radians(angle); |
| 128 |
mat2 rotMat = mat2 (cos(rangle), -sin(rangle), sin(rangle), cos(rangle)); |
| 129 |
scaled_pos.xy *=rotMat; |
| 130 |
|
| 131 |
scaled_pos /= scale_vec; |
| 132 |
|
| 133 |
//vec3 lookup_pos = dot(base1,scaled_pos) * base1 + dot(base2,scaled_pos) * base2; |
| 134 |
vec3 lookup_pos = scaled_pos - light_vec * dot(light_vec, scaled_pos); |
| 135 |
|
| 136 |
vec3 lookup_vec = normalize(normalize(light_vec) + lookup_pos); |
| 137 |
vec4 opacity = textureCube(cube_texture, lookup_vec); |
| 138 |
|
| 139 |
|
| 140 |
vec4 diffuse = diffuse_term; |
| 141 |
NdotL = dot(n, lightDir); |
| 142 |
//NdotL = dot(n, (gl_ModelViewMatrix * vec4 (light_vec,0.0)).xyz); |
| 143 |
if (NdotL > 0.0) { |
| 144 |
float shadowmap = 1.0; |
| 145 |
if (shadow_mapping_enabled) { |
| 146 |
shadowmap = getShadowing(); |
| 147 |
} |
| 148 |
diffuse.rgb += 2.0 * diffuse.rgb * (1.0 - opacity.a); |
| 149 |
color += diffuse * NdotL * opacity * shadowmap; |
| 150 |
NdotHV = max(dot(n, halfVector), 0.0); |
| 151 |
if (gl_FrontMaterial.shininess > 0.0) |
| 152 |
specular.rgb = (gl_FrontMaterial.specular.rgb |
| 153 |
* light_specular.rgb |
| 154 |
* pow(NdotHV, gl_FrontMaterial.shininess) |
| 155 |
* shadowmap); |
| 156 |
} |
| 157 |
color.a = diffuse.a; |
| 158 |
// This shouldn't be necessary, but our lighting becomes very |
| 159 |
// saturated. Clamping the color before modulating by the texture |
| 160 |
// is closer to what the OpenGL fixed function pipeline does. |
| 161 |
//color = clamp(color, 0.0, 1.0); |
| 162 |
|
| 163 |
vec3 secondary_light = vec3 (0.0,0.0,0.0); |
| 164 |
|
| 165 |
if (use_flashlight == 1) |
| 166 |
{ |
| 167 |
secondary_light.rgb += flashlight(light_filter_one, light_radius); |
| 168 |
} |
| 169 |
if (use_flashlight == 2) |
| 170 |
{ |
| 171 |
secondary_light.rgb += flashlight(light_filter_two, light_radius); |
| 172 |
} |
| 173 |
float dist = length(relPos); |
| 174 |
color.rgb += secondary_light * light_distance_fading(dist); |
| 175 |
|
| 176 |
texel = texture2D(texture, gl_TexCoord[0].st); |
| 177 |
fragColor = color * texel + specular; |
| 178 |
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb); |
| 179 |
|
| 180 |
// implicit lightmap - the user gets to select |
| 181 |
|
| 182 |
if (implicit_lightmap_enabled == 1) |
| 183 |
{ |
| 184 |
float cdiff = (length(texel.rgb - tag_color)); |
| 185 |
float enhance = 1.0 - smoothstep(threshold_low, threshold_high, cdiff); |
| 186 |
fragColor.rgb = fragColor.rgb + enhance * emit_color * emit_intensity; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 191 |
|
| 192 |
gl_FragColor = fragColor; |
| 193 |
|
| 194 |
} |
| 195 |
|