grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_vertex_blend.h
1 #ifndef SHADER_scene_vertex_blend_H
2 #define SHADER_scene_vertex_blend_H
3 static void shader_scene_vertex_blend_link(void);
4 static void shader_scene_vertex_blend_register(void);
5 static struct vg_shader _shader_scene_vertex_blend = {
6 .name = "scene_vertex_blend",
7 .link = shader_scene_vertex_blend_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_vertex_blend.fs",
69 .static_src =
70 "uniform sampler2D uTexGarbage;\n"
71 "uniform sampler2D uTexGradients;\n"
72 "uniform vec3 uCamera;\n"
73 "uniform vec3 uBoard0;\n"
74 "uniform vec3 uBoard1;\n"
75 "\n"
76 "#line 1 1 \n"
77 "// :D\n"
78 "\n"
79 "in vec2 aUv;\n"
80 "in vec4 aNorm;\n"
81 "in vec3 aCo;\n"
82 "in vec3 aWorldCo;\n"
83 "flat in ivec4 light_indices;\n"
84 "\n"
85 "uniform samplerBuffer uLightsArray;\n"
86 "uniform usampler3D uLightsIndex;\n"
87 "\n"
88 "#line 1 1 \n"
89 "layout (location = 0) out vec4 oColour;\n"
90 "\n"
91 "layout (std140) uniform ub_world_lighting\n"
92 "{\n"
93 " vec4 g_cube_min;\n"
94 " vec4 g_cube_inv_range;\n"
95 "\n"
96 " vec4 g_light_colours[3];\n"
97 " vec4 g_light_directions[3];\n"
98 " vec4 g_ambient_colour;\n"
99 "\n"
100 " vec4 g_water_plane;\n"
101 " vec4 g_depth_bounds;\n"
102 " float g_water_fog;\n"
103 " float g_time;\n"
104 " int g_light_count;\n"
105 " int g_light_preview;\n"
106 " int g_shadow_samples;\n"
107 "\n"
108 " int g_debug_indices;\n"
109 " int g_debug_complexity;\n"
110 "\n"
111 " // g_time ?\n"
112 "\n"
113 " //vec4 g_point_light_positions[32];\n"
114 " //vec4 g_point_light_colours[32];\n"
115 "};\n"
116 "\n"
117 "uniform sampler2D g_world_depth;\n"
118 "\n"
119 "float world_depth_sample( vec3 pos )\n"
120 "{\n"
121 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
122 " return texture( g_world_depth, depth_coord ).r;\n"
123 "}\n"
124 "\n"
125 "float world_water_depth( vec3 pos )\n"
126 "{\n"
127 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
128 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
129 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
130 "}\n"
131 "\n"
132 "float shadow_sample( vec3 vdir )\n"
133 "{\n"
134 " vec3 sample_pos = aWorldCo + vdir;\n"
135 " float height_sample = world_depth_sample( sample_pos );\n"
136 "\n"
137 " float fdelta = height_sample - sample_pos.y;\n"
138 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
139 "}\n"
140 "\n"
141 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
142 "{\n"
143 " float dist = pow(fdist*0.0008,1.2);\n"
144 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
145 "}\n"
146 "\n"
147 "\n"
148 "// New lighting model\n"
149 "\n"
150 "vec3 newlight_compute_ambient()\n"
151 "{\n"
152 " return g_ambient_colour.rgb;\n"
153 "}\n"
154 "\n"
155 "float newlight_compute_sun_shadow( vec3 dir )\n"
156 "{\n"
157 " if( g_shadow_samples == 0 )\n"
158 " {\n"
159 " return 1.0;\n"
160 " }\n"
161 "\n"
162 " float fspread = g_light_colours[0].w;\n"
163 " vec3 vdir = dir;\n"
164 " float flength = g_light_directions[0].w;\n"
165 "\n"
166 " float famt = 0.0;\n"
167 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
168 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
170 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
171 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
172 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
173 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
174 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
175 "\n"
176 " return 1.0 - famt;\n"
177 "}\n"
178 "\n"
179 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
180 "{\n"
181 " vec3 vtotal = g_ambient_colour.rgb;\n"
182 "\n"
183 " for( int i=0; i<g_light_count; i++ )\n"
184 " {\n"
185 " vec3 vcolour = g_light_colours[i].rgb;\n"
186 " vec3 vdir = g_light_directions[i].xyz;\n"
187 "\n"
188 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
189 " vtotal += vcolour*flight;\n"
190 " }\n"
191 "\n"
192 " return vtotal;\n"
193 "}\n"
194 "\n"
195 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
196 "{\n"
197 " vec3 vcolour = g_light_colours[0].rgb;\n"
198 " vec3 vdir = g_light_directions[0].xyz;\n"
199 "\n"
200 " vec3 specdir = reflect( -vdir, wnormal );\n"
201 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
202 " return vcolour*spec*fintensity;\n"
203 "}\n"
204 "\n"
205 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
206 "{\n"
207 " vec3 specdir = reflect( -dir, wnormal );\n"
208 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
209 "}\n"
210 "\n"
211 "vec3 newlight_compute_quadratic( vec3 wnormal, float max_dist,\n"
212 " vec3 light_colour, vec3 light_pos )\n"
213 "{\n"
214 " vec3 light_delta = light_pos-aWorldCo;\n"
215 "\n"
216 " float dist2 = dot(light_delta,light_delta);\n"
217 "\n"
218 " float quadratic = dist2*100.0;\n"
219 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
220 " attenuation *= max( dot( normalize(light_delta), wnormal ), 0.0 );\n"
221 "\n"
222 " float falloff = max( 0.0, 1.0-(dist2*max_dist) );\n"
223 " return light_colour * attenuation * falloff;\n"
224 "}\n"
225 "\n"
226 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
227 " vec3 light_colour, vec3 light_pos,\n"
228 " vec4 light_dir )\n"
229 "{\n"
230 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
231 "\n"
232 " float quadratic = dot(light_delta,light_delta);\n"
233 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
234 "\n"
235 " light_delta = normalize( light_delta );\n"
236 " attenuation *= max( 0.0, dot( light_delta, wnormal ) );\n"
237 "\n"
238 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),\n"
239 " falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );\n"
240 "\n"
241 " return light_colour*attenuation*falloff;\n"
242 "}\n"
243 "\n"
244 "#line 13 0 \n"
245 "#line 1 2 \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"
252 "\n"
253 "const float SUN_ANGLE = 0.0001;\n"
254 "const float TIME_RATE = 0.025;\n"
255 "\n"
256 "const float PI = 3.14159265;\n"
257 "\n"
258 "struct world_info\n"
259 "{\n"
260 " float time,\n"
261 " time_of_day,\n"
262 " day_phase,\n"
263 " sunset_phase;\n"
264 " \n"
265 " vec3 sun_dir;\n"
266 "};\n"
267 "\n"
268 "float luminance( vec3 v )\n"
269 "{\n"
270 " return dot( v, vec3(0.2126, 0.7152, 0.0722) );\n"
271 "}\n"
272 "\n"
273 "vec3 scene_ambient( vec3 dir, const world_info w )\n"
274 "{\n"
275 " float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6;\n"
276 " float sky_gradient = dir.y;\n"
277 " \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"
282 " \n"
283 " /* Add gradient */\n"
284 " ambient -= sky_gradient * luminance(ambient);\n"
285 " \n"
286 " return ambient;\n"
287 "}\n"
288 "\n"
289 "vec3 scene_sky( vec3 ray_dir, const world_info w )\n"
290 "{\n"
291 " ray_dir.y = abs( ray_dir.y );\n"
292 " vec3 sky_colour = scene_ambient( ray_dir, w );\n"
293 " \n"
294 " /* Sun */\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"
299 " \n"
300 " vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 );\n"
301 " sun_colour *= sun_shape;\n"
302 " \n"
303 " vec3 composite = sky_colour + sun_colour;\n"
304 " return composite;\n"
305 "}\n"
306 "\n"
307 "vec3 scene_compute_ambient( vec3 normal, const world_info w )\n"
308 "{\n"
309 " return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w );\n"
310 "}\n"
311 "\n"
312 "vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour )\n"
313 "{\n"
314 " vec3 dir3 = vec3\n"
315 " (\n"
316 " cos(dir.y) * cos(dir.x),\n"
317 " sin(dir.x),\n"
318 " sin(dir.y) * cos(dir.x)\n"
319 " );\n"
320 "\n"
321 " float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 );\n"
322 " \n"
323 " return flight * colour;\n"
324 "}\n"
325 "\n"
326 "vec3 scene_lighting_old( vec3 normal, const world_info w )\n"
327 "{\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"
331 " \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"
335 "}\n"
336 "\n"
337 "vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w )\n"
338 "{\n"
339 " float fresnel = 1.0 - abs(dot(normal,halfview));\n"
340 "\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"
343 " * w.day_phase;\n"
344 "\n"
345 " float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) );\n"
346 "\n"
347 " vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
348 "\n"
349 " return ambient + (light_sun + sky_reflection) * shadow;\n"
350 "\n"
351 "\n"
352 "\n"
353 "\n"
354 "\n"
355 "\n"
356 " float sun_theta = dot( normal, w.sun_dir );\n"
357 "\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"
363 " \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"
366 " \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"
369 " \n"
370 " float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 ) \n"
371 " * 0.2 * shadow * w.day_phase;\n"
372 " \n"
373 " return mix(dark_colour, light_colour, light_direct) + \n"
374 " spec +\n"
375 " dark_colour * light_bounce;\n"
376 "}\n"
377 "\n"
378 "void scene_state( float world_time, out world_info w )\n"
379 "{\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"
385 " \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"
388 "}\n"
389 "\n"
390 "\n"
391 "#line 14 0 \n"
392 "\n"
393 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
394 "{\n"
395 " vec3 pa = p - a;\n"
396 " vec3 ba = b - a;\n"
397 "\n"
398 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
399 " return length( pa - ba*h );\n"
400 "}\n"
401 "\n"
402 "float compute_board_shadow()\n"
403 "{\n"
404 " // player shadow\n"
405 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
406 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
407 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
408 "\n"
409 " return 1.0 - player_shadow*0.8;\n"
410 "}\n"
411 "\n"
412 "vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )\n"
413 "{\n"
414 " float dist = pow(fdist*0.0010,0.78);\n"
415 " return mix( vfrag, colour, min( 1.0, dist ) );\n"
416 "}\n"
417 "\n"
418 "vec3 rand33(vec3 p3)\n"
419 "{\n"
420 " p3 = fract(p3 * vec3(.1031, .1030, .0973));\n"
421 " p3 += dot(p3, p3.yxz+33.33);\n"
422 " return fract((p3.xxy + p3.yxx)*p3.zyx);\n"
423 "}\n"
424 "\n"
425 "vec3 scene_calculate_light( int light_index, \n"
426 " vec3 halfview, vec3 co, vec3 normal )\n"
427 "{\n"
428 " vec4 light_colour = texelFetch( uLightsArray, light_index+0 );\n"
429 " vec4 light_co = texelFetch( uLightsArray, light_index+1 );\n"
430 " vec4 light_dir = texelFetch( uLightsArray, light_index+2 );\n"
431 "\n"
432 " vec3 light_delta = light_co.xyz-co;\n"
433 " float dist2 = dot(light_delta,light_delta);\n"
434 "\n"
435 " light_delta = normalize( light_delta );\n"
436 "\n"
437 " float quadratic = dist2*100.0;\n"
438 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
439 " attenuation *= max( dot( light_delta, normal ), 0.0 );\n"
440 "\n"
441 " float falloff = max( 0.0, 1.0-(dist2*light_co.w) );\n"
442 "\n"
443 " if( light_dir.w < 0.999999 )\n"
444 " {\n"
445 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );\n"
446 " falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );\n"
447 " }\n"
448 "\n"
449 " return light_colour.rgb * attenuation * falloff;\n"
450 "}\n"
451 "\n"
452 "vec3 scene_calculate_packed_light_patch( uint packed_index, \n"
453 " vec3 halfview, vec3 co, vec3 normal )\n"
454 "{\n"
455 " uint light_count = packed_index & 0x3u;\n"
456 "\n"
457 " vec3 l = vec3(0.0);\n"
458 "\n"
459 " if( light_count >= 1u )\n"
460 " {\n"
461 " int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );\n"
462 " int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );\n"
463 " int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );\n"
464 "\n"
465 " l += scene_calculate_light( index_0, halfview, co, normal );\n"
466 "\n"
467 " if( light_count >= 2u )\n"
468 " {\n"
469 " l += scene_calculate_light( index_1, halfview, co, normal );\n"
470 "\n"
471 " if( light_count >= 3u )\n"
472 " {\n"
473 " l += scene_calculate_light( index_2, halfview, co, normal );\n"
474 " }\n"
475 " }\n"
476 " }\n"
477 "\n"
478 " return l;\n"
479 "}\n"
480 "\n"
481 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
482 "{\n"
483 " world_info world;\n"
484 " scene_state( g_time, world );\n"
485 "\n"
486 " // Lighting\n"
487 " vec3 halfview = uCamera - aWorldCo;\n"
488 " float fdist = length(halfview);\n"
489 " halfview /= fdist;\n"
490 "\n"
491 " vec3 total_light = vec3(0.0);\n"
492 " float world_shadow = newlight_compute_sun_shadow( world.sun_dir \n"
493 " * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );\n"
494 " float board_shadow = compute_board_shadow();\n"
495 "\n"
496 " total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ), \n"
497 " halfview, world );\n"
498 "\n"
499 " vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;\n"
500 " cube_coord = floor( cube_coord );\n"
501 "\n"
502 " if( g_debug_indices == 1 )\n"
503 " {\n"
504 " return rand33(cube_coord);\n"
505 " }\n"
506 "\n"
507 " if( g_debug_complexity == 1 )\n"
508 " {\n"
509 " ivec3 coord = ivec3( cube_coord );\n"
510 " uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
511 "\n"
512 " uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);\n"
513 " return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );\n"
514 " }\n"
515 "\n"
516 " // FIXME: this should absolutely must be clamped!\n"
517 " \n"
518 " ivec3 coord = ivec3( cube_coord );\n"
519 " uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
520 "\n"
521 " total_light += \n"
522 " scene_calculate_packed_light_patch( index_sample.x,\n"
523 " halfview, aWorldCo, wnormal ) \n"
524 " * board_shadow;\n"
525 " total_light += \n"
526 " scene_calculate_packed_light_patch( index_sample.y,\n"
527 " halfview, aWorldCo, wnormal )\n"
528 " * board_shadow;\n"
529 "\n"
530 " vec3 fog_colour = scene_sky( -halfview, world );\n"
531 " return scene_apply_fog( diffuse * total_light, fog_colour, fdist );\n"
532 "}\n"
533 "\n"
534 "#line 8 0 \n"
535 "#line 1 2 \n"
536 "const float k_motion_lerp_amount = 0.01;\n"
537 "\n"
538 "#line 2 0 \n"
539 "\n"
540 "layout (location = 1) out vec2 oMotionVec;\n"
541 "\n"
542 "in vec3 aMotionVec0;\n"
543 "in vec3 aMotionVec1;\n"
544 "\n"
545 "void compute_motion_vectors()\n"
546 "{\n"
547 " // Write motion vectors\n"
548 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
549 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
550 "\n"
551 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
552 "}\n"
553 "\n"
554 "#line 9 0 \n"
555 "\n"
556 "void main()\n"
557 "{\n"
558 " compute_motion_vectors();\n"
559 "\n"
560 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
561 "\n"
562 " // ws modulation\n"
563 " vec4 wgarbage = vec4(0.5,0.5,0.5,1.0);\n"
564 " \n"
565 " // Creating normal patches\n"
566 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
567 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
568 " qnorm += vec3(0.001,0.0,0.0);\n"
569 "\n"
570 " vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
571 " vec3 tangent1 = cross(qnorm,tangent0);\n"
572 " vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.160;\n"
573 " \n"
574 " // Patch local noise\n"
575 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
576 "\n"
577 " // Colour blending\n"
578 " float fblendclip = step(0.380,aNorm.w + (rgarbage.r-0.5)*-1.740)*0.320;\n"
579 " vec2 uvgradients = aUv + vec2( fblendclip, 0.0 );\n"
580 "\n"
581 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
582 " vfrag -= rgarbage.a*0.04;\n"
583 "\n"
584 " if( g_light_preview == 1 )\n"
585 " {\n"
586 " vfrag = vec3(0.5);\n"
587 " }\n"
588 "\n"
589 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
590 " oColour = vec4(vfrag, 1.0);\n"
591 "}\n"
592 ""},
593 };
594
595 static GLuint _uniform_scene_vertex_blend_uMdl;
596 static GLuint _uniform_scene_vertex_blend_uPv;
597 static GLuint _uniform_scene_vertex_blend_uPvmPrev;
598 static GLuint _uniform_scene_vertex_blend_uTexGarbage;
599 static GLuint _uniform_scene_vertex_blend_uTexGradients;
600 static GLuint _uniform_scene_vertex_blend_uCamera;
601 static GLuint _uniform_scene_vertex_blend_uBoard0;
602 static GLuint _uniform_scene_vertex_blend_uBoard1;
603 static GLuint _uniform_scene_vertex_blend_uLightsArray;
604 static GLuint _uniform_scene_vertex_blend_uLightsIndex;
605 static GLuint _uniform_scene_vertex_blend_g_world_depth;
606 static void shader_scene_vertex_blend_uMdl(m4x3f m){
607 glUniformMatrix4x3fv(_uniform_scene_vertex_blend_uMdl,1,GL_FALSE,(float*)m);
608 }
609 static void shader_scene_vertex_blend_uPv(m4x4f m){
610 glUniformMatrix4fv(_uniform_scene_vertex_blend_uPv,1,GL_FALSE,(float*)m);
611 }
612 static void shader_scene_vertex_blend_uPvmPrev(m4x4f m){
613 glUniformMatrix4fv(_uniform_scene_vertex_blend_uPvmPrev,1,GL_FALSE,(float*)m);
614 }
615 static void shader_scene_vertex_blend_uTexGarbage(int i){
616 glUniform1i(_uniform_scene_vertex_blend_uTexGarbage,i);
617 }
618 static void shader_scene_vertex_blend_uTexGradients(int i){
619 glUniform1i(_uniform_scene_vertex_blend_uTexGradients,i);
620 }
621 static void shader_scene_vertex_blend_uCamera(v3f v){
622 glUniform3fv(_uniform_scene_vertex_blend_uCamera,1,v);
623 }
624 static void shader_scene_vertex_blend_uBoard0(v3f v){
625 glUniform3fv(_uniform_scene_vertex_blend_uBoard0,1,v);
626 }
627 static void shader_scene_vertex_blend_uBoard1(v3f v){
628 glUniform3fv(_uniform_scene_vertex_blend_uBoard1,1,v);
629 }
630 static void shader_scene_vertex_blend_g_world_depth(int i){
631 glUniform1i(_uniform_scene_vertex_blend_g_world_depth,i);
632 }
633 static void shader_scene_vertex_blend_register(void){
634 vg_shader_register( &_shader_scene_vertex_blend );
635 }
636 static void shader_scene_vertex_blend_use(void){ glUseProgram(_shader_scene_vertex_blend.id); }
637 static void shader_scene_vertex_blend_link(void){
638 _uniform_scene_vertex_blend_uMdl = glGetUniformLocation( _shader_scene_vertex_blend.id, "uMdl" );
639 _uniform_scene_vertex_blend_uPv = glGetUniformLocation( _shader_scene_vertex_blend.id, "uPv" );
640 _uniform_scene_vertex_blend_uPvmPrev = glGetUniformLocation( _shader_scene_vertex_blend.id, "uPvmPrev" );
641 _uniform_scene_vertex_blend_uTexGarbage = glGetUniformLocation( _shader_scene_vertex_blend.id, "uTexGarbage" );
642 _uniform_scene_vertex_blend_uTexGradients = glGetUniformLocation( _shader_scene_vertex_blend.id, "uTexGradients" );
643 _uniform_scene_vertex_blend_uCamera = glGetUniformLocation( _shader_scene_vertex_blend.id, "uCamera" );
644 _uniform_scene_vertex_blend_uBoard0 = glGetUniformLocation( _shader_scene_vertex_blend.id, "uBoard0" );
645 _uniform_scene_vertex_blend_uBoard1 = glGetUniformLocation( _shader_scene_vertex_blend.id, "uBoard1" );
646 _uniform_scene_vertex_blend_uLightsArray = glGetUniformLocation( _shader_scene_vertex_blend.id, "uLightsArray" );
647 _uniform_scene_vertex_blend_uLightsIndex = glGetUniformLocation( _shader_scene_vertex_blend.id, "uLightsIndex" );
648 _uniform_scene_vertex_blend_g_world_depth = glGetUniformLocation( _shader_scene_vertex_blend.id, "g_world_depth" );
649 }
650 #endif /* SHADER_scene_vertex_blend_H */