grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_water.h
1 #ifndef SHADER_scene_water_H
2 #define SHADER_scene_water_H
3 static void shader_scene_water_link(void);
4 static void shader_scene_water_register(void);
5 static struct vg_shader _shader_scene_water = {
6 .name = "scene_water",
7 .link = shader_scene_water_link,
8 .vs =
9 {
10 .orig_file = "shaders/scene.vs",
11 .static_src =
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec4 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "layout (location=3) in ivec4 a_lights;\n"
16 "\n"
17 "#line 1 1 \n"
18 "const float k_motion_lerp_amount = 0.01;\n"
19 "\n"
20 "#line 2 0 \n"
21 "\n"
22 "out vec3 aMotionVec0;\n"
23 "out vec3 aMotionVec1;\n"
24 "\n"
25 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
26 "{\n"
27 " // This magically solves some artifacting errors!\n"
28 " //\n"
29 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
30 "\n"
31 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
32 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
33 "}\n"
34 "\n"
35 "#line 7 0 \n"
36 "\n"
37 "uniform mat4x3 uMdl;\n"
38 "uniform mat4 uPv;\n"
39 "uniform mat4 uPvmPrev;\n"
40 "\n"
41 "out vec2 aUv;\n"
42 "out vec4 aNorm;\n"
43 "out vec3 aCo;\n"
44 "out vec3 aWorldCo;\n"
45 "\n"
46 "flat out ivec4 light_indices;\n"
47 "\n"
48 "void main()\n"
49 "{\n"
50 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
51 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
52 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
53 "\n"
54 " vs_motion_out( vproj0, vproj1 );\n"
55 "\n"
56 " gl_Position = vproj0;\n"
57 "\n"
58 " aUv = a_uv;\n"
59 " aNorm = vec4( mat3(uMdl) * a_norm.xyz, a_norm.w );\n"
60 " aCo = a_co;\n"
61 " aWorldCo = world_pos0;\n"
62 "\n"
63 " light_indices = a_lights;\n"
64 "}\n"
65 ""},
66 .fs =
67 {
68 .orig_file = "shaders/scene_water.fs",
69 .static_src =
70 "uniform sampler2D uTexMain;\n"
71 "uniform sampler2D uTexDudv;\n"
72 "uniform sampler2D uTexBack;\n"
73 "\n"
74 "uniform vec2 uInvRes;\n"
75 "uniform float uTime;\n"
76 "uniform vec3 uCamera;\n"
77 "uniform float uSurfaceY;\n"
78 "uniform vec3 uBoard0;\n"
79 "uniform vec3 uBoard1;\n"
80 "\n"
81 "uniform vec3 uShoreColour;\n"
82 "uniform vec3 uOceanColour;\n"
83 "\n"
84 "#line 1 1 \n"
85 "// :D\n"
86 "\n"
87 "in vec2 aUv;\n"
88 "in vec4 aNorm;\n"
89 "in vec3 aCo;\n"
90 "in vec3 aWorldCo;\n"
91 "flat in ivec4 light_indices;\n"
92 "\n"
93 "uniform samplerBuffer uLightsArray;\n"
94 "uniform usampler3D uLightsIndex;\n"
95 "\n"
96 "#line 1 1 \n"
97 "layout (location = 0) out vec4 oColour;\n"
98 "\n"
99 "layout (std140) uniform ub_world_lighting\n"
100 "{\n"
101 " vec4 g_cube_min;\n"
102 " vec4 g_cube_inv_range;\n"
103 "\n"
104 " vec4 g_light_colours[3];\n"
105 " vec4 g_light_directions[3];\n"
106 " vec4 g_ambient_colour;\n"
107 "\n"
108 " vec4 g_water_plane;\n"
109 " vec4 g_depth_bounds;\n"
110 " float g_water_fog;\n"
111 " float g_time;\n"
112 " int g_light_count;\n"
113 " int g_light_preview;\n"
114 " int g_shadow_samples;\n"
115 "\n"
116 " int g_debug_indices;\n"
117 " int g_debug_complexity;\n"
118 "\n"
119 " // g_time ?\n"
120 "\n"
121 " //vec4 g_point_light_positions[32];\n"
122 " //vec4 g_point_light_colours[32];\n"
123 "};\n"
124 "\n"
125 "uniform sampler2D g_world_depth;\n"
126 "\n"
127 "float world_depth_sample( vec3 pos )\n"
128 "{\n"
129 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
130 " return texture( g_world_depth, depth_coord ).r;\n"
131 "}\n"
132 "\n"
133 "float world_water_depth( vec3 pos )\n"
134 "{\n"
135 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
136 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
137 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
138 "}\n"
139 "\n"
140 "float shadow_sample( vec3 vdir )\n"
141 "{\n"
142 " vec3 sample_pos = aWorldCo + vdir;\n"
143 " float height_sample = world_depth_sample( sample_pos );\n"
144 "\n"
145 " float fdelta = height_sample - sample_pos.y;\n"
146 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
147 "}\n"
148 "\n"
149 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
150 "{\n"
151 " float dist = pow(fdist*0.0008,1.2);\n"
152 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
153 "}\n"
154 "\n"
155 "\n"
156 "// New lighting model\n"
157 "\n"
158 "vec3 newlight_compute_ambient()\n"
159 "{\n"
160 " return g_ambient_colour.rgb;\n"
161 "}\n"
162 "\n"
163 "float newlight_compute_sun_shadow( vec3 dir )\n"
164 "{\n"
165 " if( g_shadow_samples == 0 )\n"
166 " {\n"
167 " return 1.0;\n"
168 " }\n"
169 "\n"
170 " float fspread = g_light_colours[0].w;\n"
171 " vec3 vdir = dir;\n"
172 " float flength = g_light_directions[0].w;\n"
173 "\n"
174 " float famt = 0.0;\n"
175 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
176 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
177 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
178 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
179 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
180 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
181 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
182 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
183 "\n"
184 " return 1.0 - famt;\n"
185 "}\n"
186 "\n"
187 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
188 "{\n"
189 " vec3 vtotal = g_ambient_colour.rgb;\n"
190 "\n"
191 " for( int i=0; i<g_light_count; i++ )\n"
192 " {\n"
193 " vec3 vcolour = g_light_colours[i].rgb;\n"
194 " vec3 vdir = g_light_directions[i].xyz;\n"
195 "\n"
196 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
197 " vtotal += vcolour*flight;\n"
198 " }\n"
199 "\n"
200 " return vtotal;\n"
201 "}\n"
202 "\n"
203 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
204 "{\n"
205 " vec3 vcolour = g_light_colours[0].rgb;\n"
206 " vec3 vdir = g_light_directions[0].xyz;\n"
207 "\n"
208 " vec3 specdir = reflect( -vdir, wnormal );\n"
209 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
210 " return vcolour*spec*fintensity;\n"
211 "}\n"
212 "\n"
213 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
214 "{\n"
215 " vec3 specdir = reflect( -dir, wnormal );\n"
216 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
217 "}\n"
218 "\n"
219 "vec3 newlight_compute_quadratic( vec3 wnormal, float max_dist,\n"
220 " vec3 light_colour, vec3 light_pos )\n"
221 "{\n"
222 " vec3 light_delta = light_pos-aWorldCo;\n"
223 "\n"
224 " float dist2 = dot(light_delta,light_delta);\n"
225 "\n"
226 " float quadratic = dist2*100.0;\n"
227 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
228 " attenuation *= max( dot( normalize(light_delta), wnormal ), 0.0 );\n"
229 "\n"
230 " float falloff = max( 0.0, 1.0-(dist2*max_dist) );\n"
231 " return light_colour * attenuation * falloff;\n"
232 "}\n"
233 "\n"
234 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
235 " vec3 light_colour, vec3 light_pos,\n"
236 " vec4 light_dir )\n"
237 "{\n"
238 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
239 "\n"
240 " float quadratic = dot(light_delta,light_delta);\n"
241 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
242 "\n"
243 " light_delta = normalize( light_delta );\n"
244 " attenuation *= max( 0.0, dot( light_delta, wnormal ) );\n"
245 "\n"
246 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),\n"
247 " falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );\n"
248 "\n"
249 " return light_colour*attenuation*falloff;\n"
250 "}\n"
251 "\n"
252 "#line 13 0 \n"
253 "#line 1 2 \n"
254 "const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );\n"
255 "const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n"
256 "const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 );\n"
257 "const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 );\n"
258 "const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 );\n"
259 "const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 ) * 1.125;\n"
260 "\n"
261 "const float SUN_ANGLE = 0.0001;\n"
262 "const float TIME_RATE = 0.025;\n"
263 "\n"
264 "const float PI = 3.14159265;\n"
265 "\n"
266 "struct world_info\n"
267 "{\n"
268 " float time,\n"
269 " time_of_day,\n"
270 " day_phase,\n"
271 " sunset_phase;\n"
272 " \n"
273 " vec3 sun_dir;\n"
274 "};\n"
275 "\n"
276 "float luminance( vec3 v )\n"
277 "{\n"
278 " return dot( v, vec3(0.2126, 0.7152, 0.0722) );\n"
279 "}\n"
280 "\n"
281 "vec3 scene_ambient( vec3 dir, const world_info w )\n"
282 "{\n"
283 " float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6;\n"
284 " float sky_gradient = dir.y;\n"
285 " \n"
286 " /* Blend phase colours */\n"
287 " vec3 ambient = DAYSKY_COLOUR * (w.day_phase-w.sunset_phase*0.1);\n"
288 " ambient += SUNSET_COLOUR * (1.0-dir.y*0.5) * w.sunset_phase * sun_azimuth;\n"
289 " ambient += NIGHTSKY_COLOUR * (1.0-w.day_phase);\n"
290 " \n"
291 " /* Add gradient */\n"
292 " ambient -= sky_gradient * luminance(ambient);\n"
293 " \n"
294 " return ambient;\n"
295 "}\n"
296 "\n"
297 "vec3 scene_sky( vec3 ray_dir, const world_info w )\n"
298 "{\n"
299 " ray_dir.y = abs( ray_dir.y );\n"
300 " vec3 sky_colour = scene_ambient( ray_dir, w );\n"
301 " \n"
302 " /* Sun */\n"
303 " float sun_theta = dot( ray_dir, w.sun_dir );\n"
304 " float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );\n"
305 " float sun_shape = pow( sun_size, 2000.0 );\n"
306 " sun_shape += sun_size * max(w.sun_dir.y,0.0) * 0.5;\n"
307 " \n"
308 " vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 );\n"
309 " sun_colour *= sun_shape;\n"
310 " \n"
311 " vec3 composite = sky_colour + sun_colour;\n"
312 " return composite;\n"
313 "}\n"
314 "\n"
315 "vec3 scene_compute_ambient( vec3 normal, const world_info w )\n"
316 "{\n"
317 " return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w );\n"
318 "}\n"
319 "\n"
320 "vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour )\n"
321 "{\n"
322 " vec3 dir3 = vec3\n"
323 " (\n"
324 " cos(dir.y) * cos(dir.x),\n"
325 " sin(dir.x),\n"
326 " sin(dir.y) * cos(dir.x)\n"
327 " );\n"
328 "\n"
329 " float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 );\n"
330 " \n"
331 " return flight * colour;\n"
332 "}\n"
333 "\n"
334 "vec3 scene_lighting_old( vec3 normal, const world_info w )\n"
335 "{\n"
336 " vec3 SR_COLOUR_SUN = vec3( 1.36, 1.35, 1.01 );\n"
337 " vec3 SR_COLOUR_FILL = vec3( 0.33, 0.56, 0.64 );\n"
338 " vec3 SR_COLOUR_RIM = vec3( 0.05, 0.05, 0.23 );\n"
339 " \n"
340 " return SR_LIGHT( normal, vec2( 0.63, -0.08 ), SR_COLOUR_SUN ) +\n"
341 " SR_LIGHT( normal, vec2( -2.60, -0.13 ), SR_COLOUR_FILL ) + \n"
342 " SR_LIGHT( normal, vec2( 2.60, -0.84 ), SR_COLOUR_RIM ) ;\n"
343 "}\n"
344 "\n"
345 "vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w )\n"
346 "{\n"
347 " float fresnel = 1.0 - abs(dot(normal,halfview));\n"
348 "\n"
349 " vec3 sky_reflection = 0.5 * fresnel * mix( DAYSKY_COLOUR, SUNSET_COLOUR, w.sunset_phase );\n"
350 " vec3 light_sun = max(0.0,dot(normal,w.sun_dir)*0.75+0.25) * SUN_COLOUR \n"
351 " * w.day_phase;\n"
352 "\n"
353 " float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) );\n"
354 "\n"
355 " vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
356 "\n"
357 " return ambient + (light_sun + sky_reflection) * shadow;\n"
358 "\n"
359 "\n"
360 "\n"
361 "\n"
362 "\n"
363 "\n"
364 " float sun_theta = dot( normal, w.sun_dir );\n"
365 "\n"
366 " float softness_min = 0.5;\n"
367 " float softness = softness_min + w.sunset_phase * (1.0-softness_min);\n"
368 " float light_min = 0.0 * w.day_phase;\n"
369 " float light_direct = light_min + smoothstep( -softness, softness, sun_theta ) * (1.0-light_min);\n"
370 " light_direct *= clamp(w.sun_dir.y * 4.0 + 1.0,0.0,1.0) * shadow;\n"
371 " \n"
372 " float light_bounce = 0.5 + 0.5 * dot( -normal, w.sun_dir );\n"
373 " light_bounce *= light_bounce * max( w.sun_dir.y, 0.0 );\n"
374 " \n"
375 " vec3 light_colour = SUN_COLOUR*w.day_phase + (SUNSET_COLOUR*w.sunset_phase + 0.1);\n"
376 " vec3 dark_colour = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
377 " \n"
378 " float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 ) \n"
379 " * 0.2 * shadow * w.day_phase;\n"
380 " \n"
381 " return mix(dark_colour, light_colour, light_direct) + \n"
382 " spec +\n"
383 " dark_colour * light_bounce;\n"
384 "}\n"
385 "\n"
386 "void scene_state( float world_time, out world_info w )\n"
387 "{\n"
388 " w.time = world_time;\n"
389 " w.time_of_day = fract( w.time );\n"
390 " w.day_phase = cos( w.time_of_day * PI * 2.0 ) * 0.5 + 0.5;\n"
391 " w.sunset_phase = cos( w.time_of_day * PI * 4.0 + PI ) * 0.5 + 0.5;\n"
392 " w.sunset_phase = pow( w.sunset_phase, 6.0 );\n"
393 " \n"
394 " float a = w.time_of_day * PI * 2.0;\n"
395 " w.sun_dir = normalize( vec3( sin( a ), cos( a ), 0.2) );\n"
396 "}\n"
397 "\n"
398 "\n"
399 "#line 14 0 \n"
400 "\n"
401 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
402 "{\n"
403 " vec3 pa = p - a;\n"
404 " vec3 ba = b - a;\n"
405 "\n"
406 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
407 " return length( pa - ba*h );\n"
408 "}\n"
409 "\n"
410 "float compute_board_shadow()\n"
411 "{\n"
412 " // player shadow\n"
413 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
414 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
415 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
416 "\n"
417 " return 1.0 - player_shadow*0.8;\n"
418 "}\n"
419 "\n"
420 "vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )\n"
421 "{\n"
422 " float dist = pow(fdist*0.0010,0.78);\n"
423 " return mix( vfrag, colour, min( 1.0, dist ) );\n"
424 "}\n"
425 "\n"
426 "vec3 rand33(vec3 p3)\n"
427 "{\n"
428 " p3 = fract(p3 * vec3(.1031, .1030, .0973));\n"
429 " p3 += dot(p3, p3.yxz+33.33);\n"
430 " return fract((p3.xxy + p3.yxx)*p3.zyx);\n"
431 "}\n"
432 "\n"
433 "vec3 scene_calculate_light( int light_index, \n"
434 " vec3 halfview, vec3 co, vec3 normal )\n"
435 "{\n"
436 " vec4 light_colour = texelFetch( uLightsArray, light_index+0 );\n"
437 " vec4 light_co = texelFetch( uLightsArray, light_index+1 );\n"
438 " vec4 light_dir = texelFetch( uLightsArray, light_index+2 );\n"
439 "\n"
440 " vec3 light_delta = light_co.xyz-co;\n"
441 " float dist2 = dot(light_delta,light_delta);\n"
442 "\n"
443 " light_delta = normalize( light_delta );\n"
444 "\n"
445 " float quadratic = dist2*100.0;\n"
446 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
447 " attenuation *= max( dot( light_delta, normal ), 0.0 );\n"
448 "\n"
449 " float falloff = max( 0.0, 1.0-(dist2*light_co.w) );\n"
450 "\n"
451 " if( light_dir.w < 0.999999 )\n"
452 " {\n"
453 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );\n"
454 " falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );\n"
455 " }\n"
456 "\n"
457 " return light_colour.rgb * attenuation * falloff;\n"
458 "}\n"
459 "\n"
460 "vec3 scene_calculate_packed_light_patch( uint packed_index, \n"
461 " vec3 halfview, vec3 co, vec3 normal )\n"
462 "{\n"
463 " uint light_count = packed_index & 0x3u;\n"
464 "\n"
465 " vec3 l = vec3(0.0);\n"
466 "\n"
467 " if( light_count >= 1u )\n"
468 " {\n"
469 " int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );\n"
470 " int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );\n"
471 " int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );\n"
472 "\n"
473 " l += scene_calculate_light( index_0, halfview, co, normal );\n"
474 "\n"
475 " if( light_count >= 2u )\n"
476 " {\n"
477 " l += scene_calculate_light( index_1, halfview, co, normal );\n"
478 "\n"
479 " if( light_count >= 3u )\n"
480 " {\n"
481 " l += scene_calculate_light( index_2, halfview, co, normal );\n"
482 " }\n"
483 " }\n"
484 " }\n"
485 "\n"
486 " return l;\n"
487 "}\n"
488 "\n"
489 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
490 "{\n"
491 " world_info world;\n"
492 " scene_state( g_time, world );\n"
493 "\n"
494 " // Lighting\n"
495 " vec3 halfview = uCamera - aWorldCo;\n"
496 " float fdist = length(halfview);\n"
497 " halfview /= fdist;\n"
498 "\n"
499 " vec3 total_light = vec3(0.0);\n"
500 " float world_shadow = newlight_compute_sun_shadow( world.sun_dir \n"
501 " * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );\n"
502 " float board_shadow = compute_board_shadow();\n"
503 "\n"
504 " total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ), \n"
505 " halfview, world );\n"
506 "\n"
507 " vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;\n"
508 " cube_coord = floor( cube_coord );\n"
509 "\n"
510 " if( g_debug_indices == 1 )\n"
511 " {\n"
512 " return rand33(cube_coord);\n"
513 " }\n"
514 "\n"
515 " if( g_debug_complexity == 1 )\n"
516 " {\n"
517 " ivec3 coord = ivec3( cube_coord );\n"
518 " uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
519 "\n"
520 " uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);\n"
521 " return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );\n"
522 " }\n"
523 "\n"
524 " // FIXME: this should absolutely must be clamped!\n"
525 " \n"
526 " ivec3 coord = ivec3( cube_coord );\n"
527 " uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
528 "\n"
529 " total_light += \n"
530 " scene_calculate_packed_light_patch( index_sample.x,\n"
531 " halfview, aWorldCo, wnormal ) \n"
532 " * board_shadow;\n"
533 " total_light += \n"
534 " scene_calculate_packed_light_patch( index_sample.y,\n"
535 " halfview, aWorldCo, wnormal )\n"
536 " * board_shadow;\n"
537 "\n"
538 " vec3 fog_colour = scene_sky( -halfview, world );\n"
539 " return scene_apply_fog( diffuse * total_light, fog_colour, fdist );\n"
540 "}\n"
541 "\n"
542 "#line 16 0 \n"
543 "#line 1 2 \n"
544 "const float k_motion_lerp_amount = 0.01;\n"
545 "\n"
546 "#line 2 0 \n"
547 "\n"
548 "layout (location = 1) out vec2 oMotionVec;\n"
549 "\n"
550 "in vec3 aMotionVec0;\n"
551 "in vec3 aMotionVec1;\n"
552 "\n"
553 "void compute_motion_vectors()\n"
554 "{\n"
555 " // Write motion vectors\n"
556 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
557 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
558 "\n"
559 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
560 "}\n"
561 "\n"
562 "#line 17 0 \n"
563 "\n"
564 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
565 " vec4 beneath, vec4 above )\n"
566 "{\n"
567 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
568 "\n"
569 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
570 "\n"
571 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
572 " vec3 specdir = reflect( -lightdir, vnorm );\n"
573 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
574 " \n"
575 " // Depth \n"
576 " float depthblend = pow( beneath.r, 0.8 );\n"
577 "\n"
578 " // Composite\n"
579 " vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
580 " //vsurface += spec;\n"
581 "\n"
582 " return vec4( vsurface,depthblend );\n"
583 "}\n"
584 "\n"
585 "void main()\n"
586 "{\n"
587 " compute_motion_vectors();\n"
588 "\n"
589 " // Create texture coords\n"
590 " vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
591 " \n"
592 " // Surface colour composite\n"
593 " float depthvalue = clamp( -world_water_depth( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
594 "\n"
595 " vec2 world_coord = aCo.xz * 0.008;\n"
596 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
597 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
598 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
599 "\n"
600 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
601 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
602 " \n"
603 " // Foam\n"
604 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
605 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
606 "\n"
607 " // Lighting\n"
608 " vec3 halfview = -normalize( aCo-uCamera );\n"
609 "\n"
610 " // Sample textures\n"
611 " vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
612 " vec4 beneath = texture( uTexBack, ssuv );\n"
613 "\n"
614 " // Fog\n"
615 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
616 "\n"
617 " // Composite\n"
618 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
619 " vsurface.a -= fdist;\n"
620 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
621 " oColour.rgb = scene_do_lighting( oColour.rgb, aNorm.xyz );\n"
622 "}\n"
623 ""},
624 };
625
626 static GLuint _uniform_scene_water_uMdl;
627 static GLuint _uniform_scene_water_uPv;
628 static GLuint _uniform_scene_water_uPvmPrev;
629 static GLuint _uniform_scene_water_uTexMain;
630 static GLuint _uniform_scene_water_uTexDudv;
631 static GLuint _uniform_scene_water_uTexBack;
632 static GLuint _uniform_scene_water_uInvRes;
633 static GLuint _uniform_scene_water_uTime;
634 static GLuint _uniform_scene_water_uCamera;
635 static GLuint _uniform_scene_water_uSurfaceY;
636 static GLuint _uniform_scene_water_uBoard0;
637 static GLuint _uniform_scene_water_uBoard1;
638 static GLuint _uniform_scene_water_uShoreColour;
639 static GLuint _uniform_scene_water_uOceanColour;
640 static GLuint _uniform_scene_water_uLightsArray;
641 static GLuint _uniform_scene_water_uLightsIndex;
642 static GLuint _uniform_scene_water_g_world_depth;
643 static void shader_scene_water_uMdl(m4x3f m){
644 glUniformMatrix4x3fv(_uniform_scene_water_uMdl,1,GL_FALSE,(float*)m);
645 }
646 static void shader_scene_water_uPv(m4x4f m){
647 glUniformMatrix4fv(_uniform_scene_water_uPv,1,GL_FALSE,(float*)m);
648 }
649 static void shader_scene_water_uPvmPrev(m4x4f m){
650 glUniformMatrix4fv(_uniform_scene_water_uPvmPrev,1,GL_FALSE,(float*)m);
651 }
652 static void shader_scene_water_uTexMain(int i){
653 glUniform1i(_uniform_scene_water_uTexMain,i);
654 }
655 static void shader_scene_water_uTexDudv(int i){
656 glUniform1i(_uniform_scene_water_uTexDudv,i);
657 }
658 static void shader_scene_water_uTexBack(int i){
659 glUniform1i(_uniform_scene_water_uTexBack,i);
660 }
661 static void shader_scene_water_uInvRes(v2f v){
662 glUniform2fv(_uniform_scene_water_uInvRes,1,v);
663 }
664 static void shader_scene_water_uTime(float f){
665 glUniform1f(_uniform_scene_water_uTime,f);
666 }
667 static void shader_scene_water_uCamera(v3f v){
668 glUniform3fv(_uniform_scene_water_uCamera,1,v);
669 }
670 static void shader_scene_water_uSurfaceY(float f){
671 glUniform1f(_uniform_scene_water_uSurfaceY,f);
672 }
673 static void shader_scene_water_uBoard0(v3f v){
674 glUniform3fv(_uniform_scene_water_uBoard0,1,v);
675 }
676 static void shader_scene_water_uBoard1(v3f v){
677 glUniform3fv(_uniform_scene_water_uBoard1,1,v);
678 }
679 static void shader_scene_water_uShoreColour(v3f v){
680 glUniform3fv(_uniform_scene_water_uShoreColour,1,v);
681 }
682 static void shader_scene_water_uOceanColour(v3f v){
683 glUniform3fv(_uniform_scene_water_uOceanColour,1,v);
684 }
685 static void shader_scene_water_g_world_depth(int i){
686 glUniform1i(_uniform_scene_water_g_world_depth,i);
687 }
688 static void shader_scene_water_register(void){
689 vg_shader_register( &_shader_scene_water );
690 }
691 static void shader_scene_water_use(void){ glUseProgram(_shader_scene_water.id); }
692 static void shader_scene_water_link(void){
693 _uniform_scene_water_uMdl = glGetUniformLocation( _shader_scene_water.id, "uMdl" );
694 _uniform_scene_water_uPv = glGetUniformLocation( _shader_scene_water.id, "uPv" );
695 _uniform_scene_water_uPvmPrev = glGetUniformLocation( _shader_scene_water.id, "uPvmPrev" );
696 _uniform_scene_water_uTexMain = glGetUniformLocation( _shader_scene_water.id, "uTexMain" );
697 _uniform_scene_water_uTexDudv = glGetUniformLocation( _shader_scene_water.id, "uTexDudv" );
698 _uniform_scene_water_uTexBack = glGetUniformLocation( _shader_scene_water.id, "uTexBack" );
699 _uniform_scene_water_uInvRes = glGetUniformLocation( _shader_scene_water.id, "uInvRes" );
700 _uniform_scene_water_uTime = glGetUniformLocation( _shader_scene_water.id, "uTime" );
701 _uniform_scene_water_uCamera = glGetUniformLocation( _shader_scene_water.id, "uCamera" );
702 _uniform_scene_water_uSurfaceY = glGetUniformLocation( _shader_scene_water.id, "uSurfaceY" );
703 _uniform_scene_water_uBoard0 = glGetUniformLocation( _shader_scene_water.id, "uBoard0" );
704 _uniform_scene_water_uBoard1 = glGetUniformLocation( _shader_scene_water.id, "uBoard1" );
705 _uniform_scene_water_uShoreColour = glGetUniformLocation( _shader_scene_water.id, "uShoreColour" );
706 _uniform_scene_water_uOceanColour = glGetUniformLocation( _shader_scene_water.id, "uOceanColour" );
707 _uniform_scene_water_uLightsArray = glGetUniformLocation( _shader_scene_water.id, "uLightsArray" );
708 _uniform_scene_water_uLightsIndex = glGetUniformLocation( _shader_scene_water.id, "uLightsIndex" );
709 _uniform_scene_water_g_world_depth = glGetUniformLocation( _shader_scene_water.id, "g_world_depth" );
710 }
711 #endif /* SHADER_scene_water_H */