grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / model_sky.h
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 = {
6 .name = "model_sky",
7 .link = shader_model_sky_link,
8 .vs =
9 {
10 .orig_file = "shaders/model.vs",
11 .static_src =
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"
18 "\n"
19 "#line 1 1 \n"
20 "const float k_motion_lerp_amount = 0.01;\n"
21 "\n"
22 "#line 2 0 \n"
23 "\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
26 "\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
28 "{\n"
29 " // This magically solves some artifacting errors!\n"
30 " //\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
32 "\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
35 "}\n"
36 "\n"
37 "#line 9 0 \n"
38 "\n"
39 "uniform mat4x3 uMdl;\n"
40 "uniform mat4 uPv;\n"
41 "uniform mat4 uPvmPrev;\n"
42 "\n"
43 "out vec4 aColour;\n"
44 "out vec2 aUv;\n"
45 "out vec3 aNorm;\n"
46 "out vec3 aCo;\n"
47 "out vec3 aWorldCo;\n"
48 "\n"
49 "void main()\n"
50 "{\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"
54 "\n"
55 " vs_motion_out( vproj0, vproj1 );\n"
56 "\n"
57 " gl_Position = vproj0;\n"
58 " aWorldCo = world_pos0;\n"
59 " aColour = a_colour;\n"
60 " aUv = a_uv;\n"
61 " aNorm = mat3(uMdl) * a_norm;\n"
62 " aCo = a_co;\n"
63 "}\n"
64 ""},
65 .fs =
66 {
67 .orig_file = "shaders/model_sky.fs",
68 .static_src =
69 "uniform sampler2D uTexGarbage;\n"
70 "\n"
71 "in vec4 aColour;\n"
72 "in vec2 aUv;\n"
73 "in vec3 aNorm;\n"
74 "in vec3 aCo;\n"
75 "in vec3 aWorldCo;\n"
76 "\n"
77 "#line 1 1 \n"
78 "layout (location = 0) out vec4 oColour;\n"
79 "\n"
80 "layout (std140) uniform ub_world_lighting\n"
81 "{\n"
82 " vec4 g_cube_min;\n"
83 " vec4 g_cube_inv_range;\n"
84 "\n"
85 " vec4 g_light_colours[3];\n"
86 " vec4 g_light_directions[3];\n"
87 " vec4 g_ambient_colour;\n"
88 "\n"
89 " vec4 g_water_plane;\n"
90 " vec4 g_depth_bounds;\n"
91 " float g_water_fog;\n"
92 " float g_time;\n"
93 " int g_light_count;\n"
94 " int g_light_preview;\n"
95 " int g_shadow_samples;\n"
96 "\n"
97 " int g_debug_indices;\n"
98 " int g_debug_complexity;\n"
99 "\n"
100 " // g_time ?\n"
101 "\n"
102 " //vec4 g_point_light_positions[32];\n"
103 " //vec4 g_point_light_colours[32];\n"
104 "};\n"
105 "\n"
106 "uniform sampler2D g_world_depth;\n"
107 "\n"
108 "float world_depth_sample( vec3 pos )\n"
109 "{\n"
110 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
111 " return texture( g_world_depth, depth_coord ).r;\n"
112 "}\n"
113 "\n"
114 "float world_water_depth( vec3 pos )\n"
115 "{\n"
116 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
117 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
118 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
119 "}\n"
120 "\n"
121 "float shadow_sample( vec3 vdir )\n"
122 "{\n"
123 " vec3 sample_pos = aWorldCo + vdir;\n"
124 " float height_sample = world_depth_sample( sample_pos );\n"
125 "\n"
126 " float fdelta = height_sample - sample_pos.y;\n"
127 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
128 "}\n"
129 "\n"
130 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
131 "{\n"
132 " float dist = pow(fdist*0.0008,1.2);\n"
133 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
134 "}\n"
135 "\n"
136 "\n"
137 "// New lighting model\n"
138 "\n"
139 "vec3 newlight_compute_ambient()\n"
140 "{\n"
141 " return g_ambient_colour.rgb;\n"
142 "}\n"
143 "\n"
144 "float newlight_compute_sun_shadow( vec3 dir )\n"
145 "{\n"
146 " if( g_shadow_samples == 0 )\n"
147 " {\n"
148 " return 1.0;\n"
149 " }\n"
150 "\n"
151 " float fspread = g_light_colours[0].w;\n"
152 " vec3 vdir = dir;\n"
153 " float flength = g_light_directions[0].w;\n"
154 "\n"
155 " float famt = 0.0;\n"
156 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
157 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
158 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
159 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
160 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
161 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
162 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
163 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
164 "\n"
165 " return 1.0 - famt;\n"
166 "}\n"
167 "\n"
168 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
169 "{\n"
170 " vec3 vtotal = g_ambient_colour.rgb;\n"
171 "\n"
172 " for( int i=0; i<g_light_count; i++ )\n"
173 " {\n"
174 " vec3 vcolour = g_light_colours[i].rgb;\n"
175 " vec3 vdir = g_light_directions[i].xyz;\n"
176 "\n"
177 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
178 " vtotal += vcolour*flight;\n"
179 " }\n"
180 "\n"
181 " return vtotal;\n"
182 "}\n"
183 "\n"
184 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
185 "{\n"
186 " vec3 vcolour = g_light_colours[0].rgb;\n"
187 " vec3 vdir = g_light_directions[0].xyz;\n"
188 "\n"
189 " vec3 specdir = reflect( -vdir, wnormal );\n"
190 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
191 " return vcolour*spec*fintensity;\n"
192 "}\n"
193 "\n"
194 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
195 "{\n"
196 " vec3 specdir = reflect( -dir, wnormal );\n"
197 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
198 "}\n"
199 "\n"
200 "vec3 newlight_compute_quadratic( vec3 wnormal, float max_dist,\n"
201 " vec3 light_colour, vec3 light_pos )\n"
202 "{\n"
203 " vec3 light_delta = light_pos-aWorldCo;\n"
204 "\n"
205 " float dist2 = dot(light_delta,light_delta);\n"
206 "\n"
207 " float quadratic = dist2*100.0;\n"
208 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
209 " attenuation *= max( dot( normalize(light_delta), wnormal ), 0.0 );\n"
210 "\n"
211 " float falloff = max( 0.0, 1.0-(dist2*max_dist) );\n"
212 " return light_colour * attenuation * falloff;\n"
213 "}\n"
214 "\n"
215 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
216 " vec3 light_colour, vec3 light_pos,\n"
217 " vec4 light_dir )\n"
218 "{\n"
219 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
220 "\n"
221 " float quadratic = dot(light_delta,light_delta);\n"
222 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
223 "\n"
224 " light_delta = normalize( light_delta );\n"
225 " attenuation *= max( 0.0, dot( light_delta, wnormal ) );\n"
226 "\n"
227 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),\n"
228 " falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );\n"
229 "\n"
230 " return light_colour*attenuation*falloff;\n"
231 "}\n"
232 "\n"
233 "#line 10 0 \n"
234 "#line 1 2 \n"
235 "const float k_motion_lerp_amount = 0.01;\n"
236 "\n"
237 "#line 2 0 \n"
238 "\n"
239 "layout (location = 1) out vec2 oMotionVec;\n"
240 "\n"
241 "in vec3 aMotionVec0;\n"
242 "in vec3 aMotionVec1;\n"
243 "\n"
244 "void compute_motion_vectors()\n"
245 "{\n"
246 " // Write motion vectors\n"
247 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
248 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
249 "\n"
250 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
251 "}\n"
252 "\n"
253 "#line 11 0 \n"
254 "#line 1 3 \n"
255 "const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );\n"
256 "const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n"
257 "const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 );\n"
258 "const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 );\n"
259 "const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 );\n"
260 "const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 ) * 1.125;\n"
261 "\n"
262 "const float SUN_ANGLE = 0.0001;\n"
263 "const float TIME_RATE = 0.025;\n"
264 "\n"
265 "const float PI = 3.14159265;\n"
266 "\n"
267 "struct world_info\n"
268 "{\n"
269 " float time,\n"
270 " time_of_day,\n"
271 " day_phase,\n"
272 " sunset_phase;\n"
273 " \n"
274 " vec3 sun_dir;\n"
275 "};\n"
276 "\n"
277 "float luminance( vec3 v )\n"
278 "{\n"
279 " return dot( v, vec3(0.2126, 0.7152, 0.0722) );\n"
280 "}\n"
281 "\n"
282 "vec3 scene_ambient( vec3 dir, const world_info w )\n"
283 "{\n"
284 " float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6;\n"
285 " float sky_gradient = dir.y;\n"
286 " \n"
287 " /* Blend phase colours */\n"
288 " vec3 ambient = DAYSKY_COLOUR * (w.day_phase-w.sunset_phase*0.1);\n"
289 " ambient += SUNSET_COLOUR * (1.0-dir.y*0.5) * w.sunset_phase * sun_azimuth;\n"
290 " ambient += NIGHTSKY_COLOUR * (1.0-w.day_phase);\n"
291 " \n"
292 " /* Add gradient */\n"
293 " ambient -= sky_gradient * luminance(ambient);\n"
294 " \n"
295 " return ambient;\n"
296 "}\n"
297 "\n"
298 "vec3 scene_sky( vec3 ray_dir, const world_info w )\n"
299 "{\n"
300 " ray_dir.y = abs( ray_dir.y );\n"
301 " vec3 sky_colour = scene_ambient( ray_dir, w );\n"
302 " \n"
303 " /* Sun */\n"
304 " float sun_theta = dot( ray_dir, w.sun_dir );\n"
305 " float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );\n"
306 " float sun_shape = pow( sun_size, 2000.0 );\n"
307 " sun_shape += sun_size * max(w.sun_dir.y,0.0) * 0.5;\n"
308 " \n"
309 " vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 );\n"
310 " sun_colour *= sun_shape;\n"
311 " \n"
312 " vec3 composite = sky_colour + sun_colour;\n"
313 " return composite;\n"
314 "}\n"
315 "\n"
316 "vec3 scene_compute_ambient( vec3 normal, const world_info w )\n"
317 "{\n"
318 " return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w );\n"
319 "}\n"
320 "\n"
321 "vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour )\n"
322 "{\n"
323 " vec3 dir3 = vec3\n"
324 " (\n"
325 " cos(dir.y) * cos(dir.x),\n"
326 " sin(dir.x),\n"
327 " sin(dir.y) * cos(dir.x)\n"
328 " );\n"
329 "\n"
330 " float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 );\n"
331 " \n"
332 " return flight * colour;\n"
333 "}\n"
334 "\n"
335 "vec3 scene_lighting_old( vec3 normal, const world_info w )\n"
336 "{\n"
337 " vec3 SR_COLOUR_SUN = vec3( 1.36, 1.35, 1.01 );\n"
338 " vec3 SR_COLOUR_FILL = vec3( 0.33, 0.56, 0.64 );\n"
339 " vec3 SR_COLOUR_RIM = vec3( 0.05, 0.05, 0.23 );\n"
340 " \n"
341 " return SR_LIGHT( normal, vec2( 0.63, -0.08 ), SR_COLOUR_SUN ) +\n"
342 " SR_LIGHT( normal, vec2( -2.60, -0.13 ), SR_COLOUR_FILL ) + \n"
343 " SR_LIGHT( normal, vec2( 2.60, -0.84 ), SR_COLOUR_RIM ) ;\n"
344 "}\n"
345 "\n"
346 "vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w )\n"
347 "{\n"
348 " float fresnel = 1.0 - abs(dot(normal,halfview));\n"
349 "\n"
350 " vec3 sky_reflection = 0.5 * fresnel * mix( DAYSKY_COLOUR, SUNSET_COLOUR, w.sunset_phase );\n"
351 " vec3 light_sun = max(0.0,dot(normal,w.sun_dir)*0.75+0.25) * SUN_COLOUR \n"
352 " * w.day_phase;\n"
353 "\n"
354 " float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) );\n"
355 "\n"
356 " vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
357 "\n"
358 " return ambient + (light_sun + sky_reflection) * shadow;\n"
359 "\n"
360 "\n"
361 "\n"
362 "\n"
363 "\n"
364 "\n"
365 " float sun_theta = dot( normal, w.sun_dir );\n"
366 "\n"
367 " float softness_min = 0.5;\n"
368 " float softness = softness_min + w.sunset_phase * (1.0-softness_min);\n"
369 " float light_min = 0.0 * w.day_phase;\n"
370 " float light_direct = light_min + smoothstep( -softness, softness, sun_theta ) * (1.0-light_min);\n"
371 " light_direct *= clamp(w.sun_dir.y * 4.0 + 1.0,0.0,1.0) * shadow;\n"
372 " \n"
373 " float light_bounce = 0.5 + 0.5 * dot( -normal, w.sun_dir );\n"
374 " light_bounce *= light_bounce * max( w.sun_dir.y, 0.0 );\n"
375 " \n"
376 " vec3 light_colour = SUN_COLOUR*w.day_phase + (SUNSET_COLOUR*w.sunset_phase + 0.1);\n"
377 " vec3 dark_colour = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
378 " \n"
379 " float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 ) \n"
380 " * 0.2 * shadow * w.day_phase;\n"
381 " \n"
382 " return mix(dark_colour, light_colour, light_direct) + \n"
383 " spec +\n"
384 " dark_colour * light_bounce;\n"
385 "}\n"
386 "\n"
387 "void scene_state( float world_time, out world_info w )\n"
388 "{\n"
389 " w.time = world_time;\n"
390 " w.time_of_day = fract( w.time );\n"
391 " w.day_phase = cos( w.time_of_day * PI * 2.0 ) * 0.5 + 0.5;\n"
392 " w.sunset_phase = cos( w.time_of_day * PI * 4.0 + PI ) * 0.5 + 0.5;\n"
393 " w.sunset_phase = pow( w.sunset_phase, 6.0 );\n"
394 " \n"
395 " float a = w.time_of_day * PI * 2.0;\n"
396 " w.sun_dir = normalize( vec3( sin( a ), cos( a ), 0.2) );\n"
397 "}\n"
398 "\n"
399 "\n"
400 "#line 12 0 \n"
401 "\n"
402 "void main()\n"
403 "{\n"
404 " compute_motion_vectors();\n"
405 "\n"
406 " world_info world;\n"
407 " scene_state( g_time, world );\n"
408 "\n"
409 " vec3 rd = normalize(aNorm);\n"
410 "\n"
411 " float fmove = g_time * 0.004;\n"
412 " vec2 cloudplane = (rd.xz / (rd.y*sign(rd.y))) * 0.025;\n"
413 " vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 );\n"
414 " vec4 clouds2 = texture( uTexGarbage, cloudplane*2.0 + vec2(0.3,0.1)*fmove );\n"
415 "\n"
416 " float cloud_d = max(clouds1.b*clouds2.r -0.2 - clouds2.g*0.4,0.0);\n"
417 " float cloud_e = pow(cloud_d,1.5)*pow(abs(rd.y),0.3)*2.0;\n"
418 "\n"
419 " oColour = vec4( scene_sky( -rd, world ) ,1.0);\n"
420 "\n"
421 " vec3 cloud_colour = mix( mix(NIGHTSKY_COLOUR,vec3(1.0),world.day_phase), \n"
422 " SUNSET_COLOUR, world.sunset_phase );\n"
423 " oColour.rgb = mix( oColour.rgb, cloud_colour, cloud_e );\n"
424 "}\n"
425 ""},
426 };
427
428 static GLuint _uniform_model_sky_uMdl;
429 static GLuint _uniform_model_sky_uPv;
430 static GLuint _uniform_model_sky_uPvmPrev;
431 static GLuint _uniform_model_sky_uTexGarbage;
432 static GLuint _uniform_model_sky_g_world_depth;
433 static void shader_model_sky_uMdl(m4x3f m){
434 glUniformMatrix4x3fv(_uniform_model_sky_uMdl,1,GL_FALSE,(float*)m);
435 }
436 static void shader_model_sky_uPv(m4x4f m){
437 glUniformMatrix4fv(_uniform_model_sky_uPv,1,GL_FALSE,(float*)m);
438 }
439 static void shader_model_sky_uPvmPrev(m4x4f m){
440 glUniformMatrix4fv(_uniform_model_sky_uPvmPrev,1,GL_FALSE,(float*)m);
441 }
442 static void shader_model_sky_uTexGarbage(int i){
443 glUniform1i(_uniform_model_sky_uTexGarbage,i);
444 }
445 static void shader_model_sky_g_world_depth(int i){
446 glUniform1i(_uniform_model_sky_g_world_depth,i);
447 }
448 static void shader_model_sky_register(void){
449 vg_shader_register( &_shader_model_sky );
450 }
451 static void shader_model_sky_use(void){ glUseProgram(_shader_model_sky.id); }
452 static void shader_model_sky_link(void){
453 _uniform_model_sky_uMdl = glGetUniformLocation( _shader_model_sky.id, "uMdl" );
454 _uniform_model_sky_uPv = glGetUniformLocation( _shader_model_sky.id, "uPv" );
455 _uniform_model_sky_uPvmPrev = glGetUniformLocation( _shader_model_sky.id, "uPvmPrev" );
456 _uniform_model_sky_uTexGarbage = glGetUniformLocation( _shader_model_sky.id, "uTexGarbage" );
457 _uniform_model_sky_g_world_depth = glGetUniformLocation( _shader_model_sky.id, "g_world_depth" );
458 }
459 #endif /* SHADER_model_sky_H */