1 #ifndef SHADER_model_sky_H
2 #define SHADER_model_sky_H
3 static void shader_model_sky_link(void);
4 static void shader_model_sky_register(void);
5 static struct vg_shader _shader_model_sky
= {
7 .link
= shader_model_sky_link
,
10 .orig_file
= "shaders/model.vs",
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec3 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "layout (location=3) in vec4 a_colour;\n"
16 "layout (location=4) in vec4 a_weights;\n"
17 "layout (location=5) in ivec4 a_groups;\n"
20 "const float k_motion_lerp_amount = 0.01;\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
29 " // This magically solves some artifacting errors!\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
39 "uniform mat4x3 uMdl;\n"
41 "uniform mat4 uPvmPrev;\n"
47 "out vec3 aWorldCo;\n"
51 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
52 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
53 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
55 " vs_motion_out( vproj0, vproj1 );\n"
57 " gl_Position = vproj0;\n"
58 " aWorldCo = world_pos0;\n"
59 " aColour = a_colour;\n"
61 " aNorm = mat3(uMdl) * a_norm;\n"
67 .orig_file
= "shaders/model_sky.fs",
69 "uniform sampler2D uTexGarbage;\n"
78 "layout (location = 0) out vec4 oColour;\n"
80 "layout (std140) uniform ub_world_lighting\n"
82 " vec4 g_light_colours[3];\n"
83 " vec4 g_light_directions[3];\n"
84 " vec4 g_ambient_colour;\n"
86 " vec4 g_water_plane;\n"
87 " vec4 g_depth_bounds;\n"
88 " float g_water_fog;\n"
90 " int g_light_count;\n"
91 " int g_light_preview;\n"
92 " int g_shadow_samples;\n"
96 " //vec4 g_point_light_positions[32];\n"
97 " //vec4 g_point_light_colours[32];\n"
100 "uniform sampler2D g_world_depth;\n"
102 "float world_depth_sample( vec3 pos )\n"
104 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
105 " return texture( g_world_depth, depth_coord ).r;\n"
108 "float world_water_depth( vec3 pos )\n"
110 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
111 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
112 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
115 "float shadow_sample( vec3 vdir )\n"
117 " vec3 sample_pos = aWorldCo + vdir;\n"
118 " float height_sample = world_depth_sample( sample_pos );\n"
120 " float fdelta = height_sample - sample_pos.y;\n"
121 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
124 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
126 " float dist = pow(fdist*0.0008,1.2);\n"
127 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
131 "// New lighting model\n"
133 "vec3 newlight_compute_ambient()\n"
135 " return g_ambient_colour.rgb;\n"
138 "float newlight_compute_sun_shadow( vec3 dir )\n"
140 " if( g_shadow_samples == 0 )\n"
145 " float fspread = g_light_colours[0].w;\n"
146 " vec3 vdir = dir;\n"
147 " float flength = g_light_directions[0].w;\n"
149 " float famt = 0.0;\n"
150 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
151 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
152 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
153 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
154 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
155 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
156 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
157 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
159 " return 1.0 - famt;\n"
162 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
164 " vec3 vtotal = g_ambient_colour.rgb;\n"
166 " for( int i=0; i<g_light_count; i++ )\n"
168 " vec3 vcolour = g_light_colours[i].rgb;\n"
169 " vec3 vdir = g_light_directions[i].xyz;\n"
171 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
172 " vtotal += vcolour*flight;\n"
178 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
180 " vec3 vcolour = g_light_colours[0].rgb;\n"
181 " vec3 vdir = g_light_directions[0].xyz;\n"
183 " vec3 specdir = reflect( -vdir, wnormal );\n"
184 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
185 " return vcolour*spec*fintensity;\n"
188 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
190 " vec3 specdir = reflect( -dir, wnormal );\n"
191 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
194 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
195 " vec3 light_colour, vec3 light_pos )\n"
197 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
199 " float quadratic = dot(light_delta,light_delta);\n"
200 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
201 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
203 " return light_colour*attenuation;\n"
206 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
207 " vec3 light_colour, vec3 light_pos,\n"
208 " vec4 light_dir )\n"
210 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
212 " float quadratic = dot(light_delta,light_delta);\n"
213 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
215 " light_delta = normalize( light_delta );\n"
216 " attenuation *= max( 0.0, dot( light_delta, wnormal ) );\n"
218 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),\n"
219 " falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );\n"
221 " return light_colour*attenuation*falloff;\n"
226 "const float k_motion_lerp_amount = 0.01;\n"
230 "layout (location = 1) out vec2 oMotionVec;\n"
232 "in vec3 aMotionVec0;\n"
233 "in vec3 aMotionVec1;\n"
235 "void compute_motion_vectors()\n"
237 " // Write motion vectors\n"
238 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
239 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
241 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
246 "const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );\n"
247 "const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n"
248 "const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 );\n"
249 "const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 );\n"
250 "const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 );\n"
251 "const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 ) * 1.125;\n"
253 "const float SUN_ANGLE = 0.0001;\n"
254 "const float TIME_RATE = 0.025;\n"
256 "const float PI = 3.14159265;\n"
258 "struct world_info\n"
268 "float luminance( vec3 v )\n"
270 " return dot( v, vec3(0.2126, 0.7152, 0.0722) );\n"
273 "vec3 scene_ambient( vec3 dir, const world_info w )\n"
275 " float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6;\n"
276 " float sky_gradient = dir.y;\n"
278 " /* Blend phase colours */\n"
279 " vec3 ambient = DAYSKY_COLOUR * (w.day_phase-w.sunset_phase*0.1);\n"
280 " ambient += SUNSET_COLOUR * (1.0-dir.y*0.5) * w.sunset_phase * sun_azimuth;\n"
281 " ambient += NIGHTSKY_COLOUR * (1.0-w.day_phase);\n"
283 " /* Add gradient */\n"
284 " ambient -= sky_gradient * luminance(ambient);\n"
289 "vec3 scene_sky( vec3 ray_dir, const world_info w )\n"
291 " ray_dir.y = abs( ray_dir.y );\n"
292 " vec3 sky_colour = scene_ambient( ray_dir, w );\n"
295 " float sun_theta = dot( ray_dir, w.sun_dir );\n"
296 " float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );\n"
297 " float sun_shape = pow( sun_size, 2000.0 );\n"
298 " sun_shape += sun_size * max(w.sun_dir.y,0.0) * 0.5;\n"
300 " vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 );\n"
301 " sun_colour *= sun_shape;\n"
303 " vec3 composite = sky_colour + sun_colour;\n"
304 " return composite;\n"
307 "vec3 scene_compute_ambient( vec3 normal, const world_info w )\n"
309 " return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w );\n"
312 "vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour )\n"
314 " vec3 dir3 = vec3\n"
316 " cos(dir.y) * cos(dir.x),\n"
318 " sin(dir.y) * cos(dir.x)\n"
321 " float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 );\n"
323 " return flight * colour;\n"
326 "vec3 scene_lighting_old( vec3 normal, const world_info w )\n"
328 " vec3 SR_COLOUR_SUN = vec3( 1.36, 1.35, 1.01 );\n"
329 " vec3 SR_COLOUR_FILL = vec3( 0.33, 0.56, 0.64 );\n"
330 " vec3 SR_COLOUR_RIM = vec3( 0.05, 0.05, 0.23 );\n"
332 " return SR_LIGHT( normal, vec2( 0.63, -0.08 ), SR_COLOUR_SUN ) +\n"
333 " SR_LIGHT( normal, vec2( -2.60, -0.13 ), SR_COLOUR_FILL ) + \n"
334 " SR_LIGHT( normal, vec2( 2.60, -0.84 ), SR_COLOUR_RIM ) ;\n"
337 "vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w )\n"
339 " float fresnel = 1.0 - abs(dot(normal,halfview));\n"
341 " vec3 sky_reflection = 0.5 * fresnel * mix( DAYSKY_COLOUR, SUNSET_COLOUR, w.sunset_phase );\n"
342 " vec3 light_sun = max(0.0,dot(normal,w.sun_dir)*0.75+0.25) * SUN_COLOUR \n"
345 " float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) );\n"
347 " vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
349 " return ambient + (light_sun + sky_reflection) * shadow;\n"
356 " float sun_theta = dot( normal, w.sun_dir );\n"
358 " float softness_min = 0.5;\n"
359 " float softness = softness_min + w.sunset_phase * (1.0-softness_min);\n"
360 " float light_min = 0.0 * w.day_phase;\n"
361 " float light_direct = light_min + smoothstep( -softness, softness, sun_theta ) * (1.0-light_min);\n"
362 " light_direct *= clamp(w.sun_dir.y * 4.0 + 1.0,0.0,1.0) * shadow;\n"
364 " float light_bounce = 0.5 + 0.5 * dot( -normal, w.sun_dir );\n"
365 " light_bounce *= light_bounce * max( w.sun_dir.y, 0.0 );\n"
367 " vec3 light_colour = SUN_COLOUR*w.day_phase + (SUNSET_COLOUR*w.sunset_phase + 0.1);\n"
368 " vec3 dark_colour = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
370 " float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 ) \n"
371 " * 0.2 * shadow * w.day_phase;\n"
373 " return mix(dark_colour, light_colour, light_direct) + \n"
375 " dark_colour * light_bounce;\n"
378 "void scene_state( float world_time, out world_info w )\n"
380 " w.time = world_time;\n"
381 " w.time_of_day = fract( w.time );\n"
382 " w.day_phase = cos( w.time_of_day * PI * 2.0 ) * 0.5 + 0.5;\n"
383 " w.sunset_phase = cos( w.time_of_day * PI * 4.0 + PI ) * 0.5 + 0.5;\n"
384 " w.sunset_phase = pow( w.sunset_phase, 6.0 );\n"
386 " float a = w.time_of_day * PI * 2.0;\n"
387 " w.sun_dir = normalize( vec3( sin( a ), cos( a ), 0.2) );\n"
395 " compute_motion_vectors();\n"
397 " world_info world;\n"
398 " scene_state( g_time, world );\n"
400 " vec3 rd = normalize(aNorm);\n"
402 " float fmove = g_time * 0.004;\n"
403 " vec2 cloudplane = (rd.xz / (rd.y*sign(rd.y))) * 0.025;\n"
404 " vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 );\n"
405 " vec4 clouds2 = texture( uTexGarbage, cloudplane*2.0 + vec2(0.3,0.1)*fmove );\n"
407 " float cloud_d = max(clouds1.b*clouds2.r -0.2 - clouds2.g*0.4,0.0);\n"
408 " float cloud_e = pow(cloud_d,1.5)*pow(abs(rd.y),0.3)*2.0;\n"
410 " oColour = vec4( scene_sky( -rd, world ) ,1.0);\n"
412 " vec3 cloud_colour = mix( mix(NIGHTSKY_COLOUR,vec3(1.0),world.day_phase), \n"
413 " SUNSET_COLOUR, world.sunset_phase );\n"
414 " oColour.rgb = mix( oColour.rgb, cloud_colour, cloud_e );\n"
419 static GLuint _uniform_model_sky_uMdl
;
420 static GLuint _uniform_model_sky_uPv
;
421 static GLuint _uniform_model_sky_uPvmPrev
;
422 static GLuint _uniform_model_sky_uTexGarbage
;
423 static GLuint _uniform_model_sky_g_world_depth
;
424 static void shader_model_sky_uMdl(m4x3f m
){
425 glUniformMatrix4x3fv(_uniform_model_sky_uMdl
,1,GL_FALSE
,(float*)m
);
427 static void shader_model_sky_uPv(m4x4f m
){
428 glUniformMatrix4fv(_uniform_model_sky_uPv
,1,GL_FALSE
,(float*)m
);
430 static void shader_model_sky_uPvmPrev(m4x4f m
){
431 glUniformMatrix4fv(_uniform_model_sky_uPvmPrev
,1,GL_FALSE
,(float*)m
);
433 static void shader_model_sky_uTexGarbage(int i
){
434 glUniform1i(_uniform_model_sky_uTexGarbage
,i
);
436 static void shader_model_sky_g_world_depth(int i
){
437 glUniform1i(_uniform_model_sky_g_world_depth
,i
);
439 static void shader_model_sky_register(void){
440 vg_shader_register( &_shader_model_sky
);
442 static void shader_model_sky_use(void){ glUseProgram(_shader_model_sky
.id
); }
443 static void shader_model_sky_link(void){
444 _uniform_model_sky_uMdl
= glGetUniformLocation( _shader_model_sky
.id
, "uMdl" );
445 _uniform_model_sky_uPv
= glGetUniformLocation( _shader_model_sky
.id
, "uPv" );
446 _uniform_model_sky_uPvmPrev
= glGetUniformLocation( _shader_model_sky
.id
, "uPvmPrev" );
447 _uniform_model_sky_uTexGarbage
= glGetUniformLocation( _shader_model_sky
.id
, "uTexGarbage" );
448 _uniform_model_sky_g_world_depth
= glGetUniformLocation( _shader_model_sky
.id
, "g_world_depth" );
450 #endif /* SHADER_model_sky_H */