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