| 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 vec3 rawpos; |
| 10 |
varying vec4 ecPosition; |
| 11 |
|
| 12 |
|
| 13 |
uniform sampler2D texture; |
| 14 |
uniform sampler2D lightmap_texture; |
| 15 |
uniform sampler2D grain_texture; |
| 16 |
uniform samplerCube cube_texture; |
| 17 |
|
| 18 |
|
| 19 |
varying float yprime_alt; |
| 20 |
varying float mie_angle; |
| 21 |
|
| 22 |
|
| 23 |
uniform float visibility; |
| 24 |
uniform float avisibility; |
| 25 |
uniform float scattering; |
| 26 |
uniform float terminator; |
| 27 |
uniform float terrain_alt; |
| 28 |
uniform float hazeLayerAltitude; |
| 29 |
uniform float overcast; |
| 30 |
uniform float eye_alt; |
| 31 |
uniform float cloud_self_shading; |
| 32 |
uniform float angle; |
| 33 |
uniform float threshold_low; |
| 34 |
uniform float threshold_high; |
| 35 |
uniform float emit_intensity; |
| 36 |
uniform float light_radius; |
| 37 |
uniform float lightmap_r_factor; |
| 38 |
uniform float lightmap_g_factor; |
| 39 |
uniform float lightmap_b_factor; |
| 40 |
uniform float lightmap_a_factor; |
| 41 |
uniform float grain_magnification; |
| 42 |
|
| 43 |
uniform vec3 offset_vec; |
| 44 |
uniform vec3 scale_vec; |
| 45 |
uniform vec3 tag_color; |
| 46 |
uniform vec3 emit_color; |
| 47 |
uniform vec3 light_filter_one; |
| 48 |
uniform vec3 light_filter_two; |
| 49 |
uniform vec3 lightmap_r_color; |
| 50 |
uniform vec3 lightmap_g_color; |
| 51 |
uniform vec3 lightmap_b_color; |
| 52 |
uniform vec3 lightmap_a_color; |
| 53 |
|
| 54 |
uniform int quality_level; |
| 55 |
uniform int tquality_level; |
| 56 |
uniform int use_searchlight; |
| 57 |
uniform int implicit_lightmap_enabled; |
| 58 |
uniform int use_flashlight; |
| 59 |
uniform int lightmap_enabled; |
| 60 |
uniform int lightmap_multi; |
| 61 |
uniform int grain_texture_enabled; |
| 62 |
|
| 63 |
uniform bool shadow_mapping_enabled; |
| 64 |
|
| 65 |
|
| 66 |
const float EarthRadius = 5800000.0; |
| 67 |
const float terminator_width = 200000.0; |
| 68 |
|
| 69 |
float alt; |
| 70 |
float eShade; |
| 71 |
|
| 72 |
|
| 73 |
float fog_func (in float targ, in float alt); |
| 74 |
float alt_factor(in float eye_alt, in float vertex_alt); |
| 75 |
float light_distance_fading(in float dist); |
| 76 |
float fog_backscatter(in float avisibility); |
| 77 |
|
| 78 |
vec3 addLights(in vec3 color1, in vec3 color2); |
| 79 |
vec3 flashlight(in vec3 color, in float radius); |
| 80 |
vec3 filter_combined (in vec3 color) ; |
| 81 |
|
| 82 |
float getShadowing(); |
| 83 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel); |
| 84 |
|
| 85 |
float luminance(vec3 color) |
| 86 |
{ |
| 87 |
return dot(vec3(0.212671, 0.715160, 0.072169), color); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
float light_func (in float x, in float a, in float b, in float c, in float d, in float e) |
| 92 |
{ |
| 93 |
x = x - 0.5; |
| 94 |
|
| 95 |
// use the asymptotics to shorten computations |
| 96 |
if (x > 30.0) {return e;} |
| 97 |
if (x < -15.0) {return 0.0;} |
| 98 |
|
| 99 |
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d)); |
| 100 |
} |
| 101 |
|
| 102 |
// this determines how light is attenuated in the distance |
| 103 |
// physically this should be exp(-arg) but for technical reasons we use a sharper cutoff |
| 104 |
// for distance > visibility |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
void main() |
| 110 |
{ |
| 111 |
|
| 112 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 113 |
// this is taken from default.frag |
| 114 |
vec3 n; |
| 115 |
float NdotL, NdotHV, fogFactor; |
| 116 |
vec4 color = gl_Color; |
| 117 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 118 |
vec3 halfVector = gl_LightSource[0].halfVector.xyz; |
| 119 |
vec4 texel; |
| 120 |
vec4 fragColor; |
| 121 |
vec3 specular = vec3(0.0); |
| 122 |
float intensity; |
| 123 |
|
| 124 |
float effective_scattering = min(scattering, cloud_self_shading); |
| 125 |
|
| 126 |
eShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt); |
| 127 |
vec4 light_specular = gl_LightSource[0].specular * (eShade - 0.1); |
| 128 |
|
| 129 |
// If gl_Color.a == 0, this is a back-facing polygon and the |
| 130 |
// normal should be reversed. |
| 131 |
n = (2.0 * gl_Color.a - 1.0) * normal; |
| 132 |
n = normalize(n); |
| 133 |
|
| 134 |
// lookup on the opacity map |
| 135 |
vec3 light_vec = normalize((gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz); |
| 136 |
//vec3 light_vec = vec3 (-1.0,0.0,0.0); |
| 137 |
|
| 138 |
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 139 |
vec3 scaled_pos = relPos + ep.xyz; |
| 140 |
|
| 141 |
//vec3 lookup_vec = normalize(- normalize(light_vec) + relPos); |
| 142 |
scaled_pos -= offset_vec; |
| 143 |
float rangle = radians(angle); |
| 144 |
mat2 rotMat = mat2 (cos(rangle), -sin(rangle), sin(rangle), cos(rangle)); |
| 145 |
scaled_pos.xy *=rotMat; |
| 146 |
|
| 147 |
scaled_pos /= scale_vec; |
| 148 |
|
| 149 |
//vec3 lookup_pos = dot(base1,scaled_pos) * base1 + dot(base2,scaled_pos) * base2; |
| 150 |
vec3 lookup_pos = scaled_pos - light_vec * dot(light_vec, scaled_pos); |
| 151 |
|
| 152 |
vec3 lookup_vec = normalize(normalize(light_vec) + lookup_pos); |
| 153 |
vec4 opacity = textureCube(cube_texture, lookup_vec); |
| 154 |
|
| 155 |
|
| 156 |
vec4 diffuse = diffuse_term; |
| 157 |
NdotL = dot(n, lightDir); |
| 158 |
//NdotL = dot(n, (gl_ModelViewMatrix * vec4 (light_vec,0.0)).xyz); |
| 159 |
if (NdotL > 0.0) { |
| 160 |
float shadowmap = 1.0; |
| 161 |
if (shadow_mapping_enabled) { |
| 162 |
shadowmap = getShadowing(); |
| 163 |
} |
| 164 |
diffuse.rgb += 2.0 * diffuse.rgb * (1.0 - opacity.a); |
| 165 |
color += diffuse * NdotL * opacity * shadowmap; |
| 166 |
|
| 167 |
//NdotHV = max(dot(n, halfVector), 0.0); |
| 168 |
if (gl_FrontMaterial.shininess > 0.0) { |
| 169 |
vec3 ecViewDir = (gl_ModelViewMatrix * (ep - vec4(rawpos, 1.0))).xyz; |
| 170 |
vec3 HV = normalize(normalize(gl_LightSource[0].position.xyz) + normalize(ecViewDir)); |
| 171 |
NdotHV = max(0.0, dot(n,HV)); |
| 172 |
specular = (gl_FrontMaterial.specular.rgb |
| 173 |
* (light_specular.rgb+2.0*light_specular.rgb*(1.0-opacity.a)) |
| 174 |
* pow(NdotHV, gl_FrontMaterial.shininess) |
| 175 |
* opacity.rgb |
| 176 |
* shadowmap); |
| 177 |
} |
| 178 |
} |
| 179 |
color.a = diffuse.a; |
| 180 |
// This shouldn't be necessary, but our lighting becomes very |
| 181 |
// saturated. Clamping the color before modulating by the texture |
| 182 |
// is closer to what the OpenGL fixed function pipeline does. |
| 183 |
//color = clamp(color, 0.0, 1.0); |
| 184 |
|
| 185 |
vec3 secondary_light = vec3 (0.0,0.0,0.0); |
| 186 |
|
| 187 |
if (use_flashlight == 1) |
| 188 |
{ |
| 189 |
secondary_light.rgb += flashlight(light_filter_one, light_radius); |
| 190 |
} |
| 191 |
if (use_flashlight == 2) |
| 192 |
{ |
| 193 |
secondary_light.rgb += flashlight(light_filter_two, light_radius); |
| 194 |
} |
| 195 |
float dist = length(relPos); |
| 196 |
color.rgb += secondary_light * light_distance_fading(dist); |
| 197 |
|
| 198 |
texel = texture2D(texture, gl_TexCoord[0].st); |
| 199 |
|
| 200 |
if (grain_texture_enabled ==1) |
| 201 |
{ |
| 202 |
vec4 grainTexel = texture2D(grain_texture, gl_TexCoord[0].st * grain_magnification); |
| 203 |
texel.rgb = mix(texel.rgb, grainTexel.rgb, grainTexel.a ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
fragColor = color * texel; |
| 208 |
fragColor.rgb += specular.rgb; |
| 209 |
|
| 210 |
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb); |
| 211 |
|
| 212 |
// implicit lightmap - the user gets to select a color which is then made emissive |
| 213 |
|
| 214 |
if (implicit_lightmap_enabled == 1) |
| 215 |
{ |
| 216 |
float cdiff = (length(texel.rgb - tag_color)); |
| 217 |
float enhance = 1.0 - smoothstep(threshold_low, threshold_high, cdiff); |
| 218 |
fragColor.rgb = fragColor.rgb + enhance * emit_color * emit_intensity; |
| 219 |
} |
| 220 |
|
| 221 |
// explicit lightmap |
| 222 |
|
| 223 |
vec3 lightmapcolor = vec3(0.0, 0.0, 0.0); |
| 224 |
|
| 225 |
|
| 226 |
if (lightmap_enabled == 1) |
| 227 |
{ |
| 228 |
vec4 lightmapTexel = texture2D(lightmap_texture, gl_TexCoord[0].st); |
| 229 |
vec4 lightmapFactor = vec4(lightmap_r_factor, lightmap_g_factor, lightmap_b_factor, lightmap_a_factor); |
| 230 |
|
| 231 |
if (lightmap_multi > 0 ) |
| 232 |
{ |
| 233 |
lightmapFactor = lightmapFactor * lightmapTexel; |
| 234 |
//lightmapcolor = lightmap_r_color * lightmapFactor.r + |
| 235 |
//lightmap_g_color * lightmapFactor.g + |
| 236 |
// lightmap_b_color * lightmapFactor.b + |
| 237 |
//lightmap_a_color * lightmapFactor.a ; |
| 238 |
|
| 239 |
lightmapcolor = lightmap_r_color * lightmapFactor.r; |
| 240 |
lightmapcolor = addLights(lightmapcolor, lightmap_g_color * lightmapFactor.g); |
| 241 |
lightmapcolor = addLights(lightmapcolor, lightmap_b_color * lightmapFactor.b); |
| 242 |
lightmapcolor = addLights(lightmapcolor, lightmap_a_color * lightmapFactor.a); |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
lightmapcolor = lightmapTexel.rgb * lightmap_r_color * lightmapFactor.r; |
| 247 |
} |
| 248 |
fragColor.rgb = max(fragColor.rgb, lightmapcolor.rgb * gl_FrontMaterial.diffuse.rgb * smoothstep(0.0, 1.0, texel.rgb*.5 + lightmapcolor.rgb*.5)); |
| 249 |
} |
| 250 |
|
| 251 |
fragColor.rgb = filter_combined(fragColor.rgb); |
| 252 |
|
| 253 |
gl_FragColor = fragColor; |
| 254 |
|
| 255 |
|
| 256 |
} |
| 257 |
|