chaos pt 1
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_position.h
1 #ifndef SHADER_scene_position_H
2 #define SHADER_scene_position_H
3 static void shader_scene_position_link(void);
4 static void shader_scene_position_register(void);
5 static struct vg_shader _shader_scene_position = {
6 .name = "scene_position",
7 .link = shader_scene_position_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 "\n"
16 "#line 1 1 \n"
17 "const float k_motion_lerp_amount = 0.01;\n"
18 "\n"
19 "#line 2 0 \n"
20 "\n"
21 "out vec3 aMotionVec0;\n"
22 "out vec3 aMotionVec1;\n"
23 "\n"
24 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
25 "{\n"
26 " // This magically solves some artifacting errors!\n"
27 " //\n"
28 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
29 "\n"
30 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
31 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
32 "}\n"
33 "\n"
34 "#line 6 0 \n"
35 "\n"
36 "uniform mat4x3 uMdl;\n"
37 "uniform mat4 uPv;\n"
38 "uniform mat4 uPvmPrev;\n"
39 "\n"
40 "out vec2 aUv;\n"
41 "out vec4 aNorm;\n"
42 "out vec3 aCo;\n"
43 "out vec3 aWorldCo;\n"
44 "\n"
45 "void main()\n"
46 "{\n"
47 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
48 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
49 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
50 "\n"
51 " vs_motion_out( vproj0, vproj1 );\n"
52 "\n"
53 " gl_Position = vproj0;\n"
54 "\n"
55 " aUv = a_uv;\n"
56 " aNorm = vec4( mat3(uMdl) * a_norm.xyz, a_norm.w );\n"
57 " aCo = a_co;\n"
58 " aWorldCo = world_pos0;\n"
59 "}\n"
60 ""},
61 .fs =
62 {
63 .orig_file = "shaders/scene_position.fs",
64 .static_src =
65 "out vec4 FragColor;\n"
66 "\n"
67 "uniform vec3 uCamera;\n"
68 "uniform vec3 uBoard0;\n"
69 "uniform vec3 uBoard1;\n"
70 "\n"
71 "#line 1 1 \n"
72 "// :D\n"
73 "\n"
74 "in vec2 aUv;\n"
75 "in vec4 aNorm;\n"
76 "in vec3 aCo;\n"
77 "in vec3 aWorldCo;\n"
78 "\n"
79 "#line 1 1 \n"
80 "layout (location = 0) out vec4 oColour;\n"
81 "\n"
82 "// OpenGL wiki: Recommends do not use vec3 because of drivers. hence the v4s...\n"
83 "layout (std140) uniform ub_world_lighting\n"
84 "{\n"
85 " vec4 g_cube_min;\n"
86 " vec4 g_cube_inv_range;\n"
87 "\n"
88 " vec4 g_water_plane;\n"
89 " vec4 g_depth_bounds;\n"
90 "\n"
91 " vec4 g_daysky_colour;\n"
92 " vec4 g_nightsky_colour;\n"
93 " vec4 g_sunset_colour;\n"
94 " vec4 g_ambient_colour;\n"
95 " vec4 g_sunset_ambient;\n"
96 " vec4 g_sun_colour;\n"
97 " vec4 g_sun_dir;\n"
98 " vec4 g_board_0;\n"
99 " vec4 g_board_1;\n"
100 "\n"
101 " float g_water_fog;\n"
102 " float g_time;\n"
103 " float g_realtime;\n"
104 " float g_shadow_length;\n"
105 " float g_shadow_spread;\n"
106 "\n"
107 " float g_time_of_day;\n"
108 " float g_day_phase;\n"
109 " float g_sunset_phase;\n"
110 "\n"
111 " int g_light_preview;\n"
112 " int g_shadow_samples;\n"
113 "\n"
114 " int g_debug_indices;\n"
115 " int g_debug_complexity;\n"
116 "};\n"
117 "\n"
118 "uniform sampler2D g_world_depth;\n"
119 "uniform samplerBuffer uLightsArray;\n"
120 "uniform usampler3D uLightsIndex;\n"
121 "\n"
122 "#line 1 1 \n"
123 "//const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );\n"
124 "//const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n"
125 "//const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 );\n"
126 "//const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 );\n"
127 "//const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 );\n"
128 "//const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 );\n"
129 "\n"
130 "const float SUN_ANGLE = 0.0001;\n"
131 "const float PI = 3.14159265;\n"
132 "\n"
133 "//struct world_info\n"
134 "//{\n"
135 "// float time,\n"
136 "// time_of_day,\n"
137 "// day_phase,\n"
138 "// sunset_phase;\n"
139 "// \n"
140 "// vec3 sun_dir;\n"
141 "//};\n"
142 "\n"
143 "float luminance( vec3 v )\n"
144 "{\n"
145 " return dot( v, vec3(0.2126, 0.7152, 0.0722) );\n"
146 "}\n"
147 "\n"
148 "vec3 clearskies_ambient( vec3 dir )\n"
149 "{\n"
150 " float sun_azimuth = g_sunset_phase * (dot( dir.xz, g_sun_dir.xz )*0.4+0.6);\n"
151 " float sky_gradient = dir.y;\n"
152 " \n"
153 " /* Blend phase colours */\n"
154 " vec3 ambient = g_daysky_colour.rgb * (g_day_phase-g_sunset_phase*0.1);\n"
155 " ambient += g_sunset_colour.rgb * (1.0-dir.y*0.5)*sun_azimuth;\n"
156 " ambient += g_nightsky_colour.rgb * (1.0-g_day_phase);\n"
157 " \n"
158 " /* Add gradient */\n"
159 " ambient -= sky_gradient * luminance(ambient);\n"
160 " \n"
161 " return ambient;\n"
162 "}\n"
163 "\n"
164 "vec3 clearskies_sky( vec3 ray_dir )\n"
165 "{\n"
166 " ray_dir.y = abs( ray_dir.y );\n"
167 " vec3 sky_colour = clearskies_ambient( ray_dir );\n"
168 " \n"
169 " /* Sun */\n"
170 " float sun_theta = dot( ray_dir, g_sun_dir.xyz );\n"
171 " float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );\n"
172 " float sun_shape = pow( sun_size, 2000.0 );\n"
173 " sun_shape += sun_size * max(g_sun_dir.y,0.0) * 0.5;\n"
174 " \n"
175 " vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );\n"
176 " sun_colour *= sun_shape;\n"
177 " \n"
178 " vec3 composite = sky_colour + sun_colour;\n"
179 " return composite;\n"
180 "}\n"
181 "\n"
182 "vec3 clearskies_lighting( vec3 normal, float shadow, vec3 halfview )\n"
183 "{\n"
184 " float fresnel = 1.0 - abs(dot(normal,halfview));\n"
185 "\n"
186 " vec3 reflect_colour = mix( g_daysky_colour.rgb, g_sunset_colour.rgb, \n"
187 " g_sunset_phase );\n"
188 "\n"
189 " vec3 sky_reflection = 0.5 * fresnel * reflect_colour;\n"
190 " vec3 light_sun = max(0.0,dot(normal,g_sun_dir.xyz)*0.75+0.25) \n"
191 " * g_sun_colour.rgb * g_day_phase;\n"
192 "\n"
193 " float scaled_shadow = max( shadow, 1.0 - max(g_sun_dir.y,0.0) );\n"
194 " vec3 ambient = mix( g_ambient_colour.rgb, g_sunset_ambient.rgb, \n"
195 " g_sunset_phase );\n"
196 "\n"
197 " return ambient + (light_sun + sky_reflection) * shadow;\n"
198 "}\n"
199 "\n"
200 "#line 44 0 \n"
201 "\n"
202 "float world_depth_sample( vec3 pos )\n"
203 "{\n"
204 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
205 " return texture( g_world_depth, depth_coord ).r;\n"
206 "}\n"
207 "\n"
208 "float world_water_depth( vec3 pos )\n"
209 "{\n"
210 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
211 " return world_depth_sample( pos ) - ref_depth;\n"
212 "}\n"
213 "\n"
214 "float shadow_sample( vec3 co ){\n"
215 " float height_sample = world_depth_sample( co );\n"
216 "\n"
217 " float fdelta = height_sample - co.y;\n"
218 " return clamp( fdelta, 0.2, 0.4 )-0.2;\n"
219 "}\n"
220 "\n"
221 "float newlight_compute_sun_shadow( vec3 co, vec3 dir ){\n"
222 " if( g_shadow_samples == 0 ){\n"
223 " return 1.0;\n"
224 " }\n"
225 "\n"
226 " float fspread = g_shadow_spread;\n"
227 " float flength = g_shadow_length;\n"
228 "\n"
229 " float famt = 0.0;\n"
230 " famt += shadow_sample(co+(dir+vec3(-0.56,0.55, 0.30)*fspread)*flength*0.1);\n"
231 " famt += shadow_sample(co+(dir+vec3( 0.80,0.68, 0.34)*fspread)*flength*0.2);\n"
232 " famt += shadow_sample(co+(dir+vec3( 0.78,0.07,-0.06)*fspread)*flength*0.3);\n"
233 " famt += shadow_sample(co+(dir+vec3(-0.59,0.07,-0.42)*fspread)*flength*0.4);\n"
234 "\n"
235 " //famt+=shadow_sample(co+(dir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
236 " //famt+=shadow_sample(co+(dir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
237 " //famt+=shadow_sample(co+(dir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
238 " //famt+=shadow_sample(co+(dir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
239 "\n"
240 " return 1.0 - famt;\n"
241 "}\n"
242 "\n"
243 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
244 "{\n"
245 " vec3 specdir = reflect( -dir, wnormal );\n"
246 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
247 "}\n"
248 "\n"
249 "vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )\n"
250 "{\n"
251 " float dist = pow(fdist*0.0010,0.78);\n"
252 " return mix( vfrag, colour, min( 1.0, dist ) );\n"
253 "}\n"
254 "\n"
255 "vec3 rand33(vec3 p3)\n"
256 "{\n"
257 " p3 = fract(p3 * vec3(.1031, .1030, .0973));\n"
258 " p3 += dot(p3, p3.yxz+33.33);\n"
259 " return fract((p3.xxy + p3.yxx)*p3.zyx);\n"
260 "}\n"
261 "\n"
262 "vec3 scene_calculate_light( int light_index, \n"
263 " vec3 halfview, vec3 co, vec3 normal )\n"
264 "{\n"
265 " vec4 light_colour = texelFetch( uLightsArray, light_index+0 );\n"
266 " vec4 light_co = texelFetch( uLightsArray, light_index+1 );\n"
267 " vec4 light_dir = texelFetch( uLightsArray, light_index+2 );\n"
268 "\n"
269 " vec3 light_delta = light_co.xyz-co;\n"
270 " float dist2 = dot(light_delta,light_delta);\n"
271 "\n"
272 " light_delta = normalize( light_delta );\n"
273 "\n"
274 " float quadratic = dist2*100.0;\n"
275 " float attenuation = 1.0/( 1.0 + quadratic );\n"
276 " attenuation *= max( dot( light_delta, normal ), 0.0 );\n"
277 "\n"
278 " float falloff = max( 0.0, 1.0-(dist2*light_co.w) );\n"
279 "\n"
280 " if( light_dir.w < 0.999999 ){\n"
281 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );\n"
282 " falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );\n"
283 " }\n"
284 "\n"
285 " return light_colour.rgb * attenuation * falloff \n"
286 " * step( g_day_phase, light_colour.w );\n"
287 "}\n"
288 "\n"
289 "vec3 scene_calculate_packed_light_patch( uint packed_index, \n"
290 " vec3 halfview, vec3 co, vec3 normal )\n"
291 "{\n"
292 " uint light_count = packed_index & 0x3u;\n"
293 "\n"
294 " vec3 l = vec3(0.0);\n"
295 "\n"
296 " if( light_count >= 1u ){\n"
297 " int index_0 = int( ((packed_index >> 2u) & 0x3ffu) * 3u );\n"
298 " int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );\n"
299 " int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );\n"
300 "\n"
301 " l += scene_calculate_light( index_0, halfview, co, normal );\n"
302 "\n"
303 " if( light_count >= 2u ){\n"
304 " l += scene_calculate_light( index_1, halfview, co, normal );\n"
305 "\n"
306 " if( light_count >= 3u ){\n"
307 " l += scene_calculate_light( index_2, halfview, co, normal );\n"
308 " }\n"
309 " }\n"
310 " }\n"
311 "\n"
312 " return l;\n"
313 "}\n"
314 "\n"
315 "vec3 world_compute_lighting( vec3 diffuse, vec3 normal, vec3 co,\n"
316 " float light_mask )\n"
317 "{\n"
318 " if( g_light_preview == 1 )\n"
319 " diffuse = vec3(0.75);\n"
320 "\n"
321 " // Lighting\n"
322 " vec3 halfview = uCamera - co;\n"
323 " float fdist = length(halfview);\n"
324 " halfview /= fdist;\n"
325 "\n"
326 " float world_shadow = newlight_compute_sun_shadow( \n"
327 " co, g_sun_dir.xyz * (1.0/(max(g_sun_dir.y,0.0)+0.2)) );\n"
328 "\n"
329 " vec3 total_light = clearskies_lighting( \n"
330 " normal, min( light_mask, world_shadow ), halfview );\n"
331 "\n"
332 " vec3 cube_coord = (co - g_cube_min.xyz) * g_cube_inv_range.xyz;\n"
333 " cube_coord = floor( cube_coord );\n"
334 "\n"
335 " if( g_debug_indices == 1 )\n"
336 " {\n"
337 " return rand33(cube_coord);\n"
338 " }\n"
339 "\n"
340 " if( g_debug_complexity == 1 )\n"
341 " {\n"
342 " ivec3 coord = ivec3( cube_coord );\n"
343 " uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
344 "\n"
345 " uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);\n"
346 " return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );\n"
347 " }\n"
348 "\n"
349 " // FIXME: this coord should absolutely must be clamped!\n"
350 " \n"
351 " ivec3 coord = ivec3( cube_coord );\n"
352 " uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );\n"
353 "\n"
354 " total_light += \n"
355 " scene_calculate_packed_light_patch( index_sample.x,\n"
356 " halfview, co, normal ) \n"
357 " * light_mask;\n"
358 " total_light += \n"
359 " scene_calculate_packed_light_patch( index_sample.y,\n"
360 " halfview, co, normal )\n"
361 " * light_mask;\n"
362 "\n"
363 " // Take a section of the sky function to give us a matching fog colour\n"
364 "\n"
365 " vec3 fog_colour = clearskies_ambient( -halfview );\n"
366 " float sun_theta = dot( -halfview, g_sun_dir.xyz );\n"
367 " float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 );\n"
368 " float sun_shape = sun_size * max(g_sun_dir.y,0.0) * 0.5;\n"
369 " \n"
370 " vec3 sun_colour = mix( vec3(1.0), g_sunset_colour.rgb, g_sunset_phase*0.5 );\n"
371 " sun_colour *= sun_shape;\n"
372 "\n"
373 " fog_colour += sun_colour;\n"
374 " return scene_apply_fog( diffuse * total_light, fog_colour, fdist );\n"
375 "}\n"
376 "\n"
377 "#line 9 0 \n"
378 "\n"
379 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
380 "{\n"
381 " vec3 pa = p - a;\n"
382 " vec3 ba = b - a;\n"
383 "\n"
384 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
385 " return length( pa - ba*h );\n"
386 "}\n"
387 "\n"
388 "float compute_board_shadow()\n"
389 "{\n"
390 " // player shadow\n"
391 " float dist_to_player = max( 0.0, sdLine( aWorldCo, g_board_0.xyz,\n"
392 " g_board_1.xyz )-0.1 );\n"
393 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
394 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
395 "\n"
396 " return 1.0 - player_shadow*0.8;\n"
397 "}\n"
398 "\n"
399 "vec3 scene_compute_lighting( vec3 diffuse, vec3 normal, vec3 co )\n"
400 "{\n"
401 " return world_compute_lighting( diffuse, normal, co, compute_board_shadow() );\n"
402 "}\n"
403 "\n"
404 "#line 8 0 \n"
405 "\n"
406 "void main()\n"
407 "{\n"
408 " float height_full = aWorldCo.y;\n"
409 " float height_water = height_full;\n"
410 "\n"
411 " if( height_water > (g_water_plane.y * g_water_plane.w) + 2.0 )\n"
412 " height_water = -99999.9;\n"
413 "\n"
414 " FragColor = vec4( height_full, height_water, 0.0, 0.0 );\n"
415 "}\n"
416 ""},
417 };
418
419 static GLuint _uniform_scene_position_uMdl;
420 static GLuint _uniform_scene_position_uPv;
421 static GLuint _uniform_scene_position_uPvmPrev;
422 static GLuint _uniform_scene_position_uCamera;
423 static GLuint _uniform_scene_position_uBoard0;
424 static GLuint _uniform_scene_position_uBoard1;
425 static GLuint _uniform_scene_position_g_world_depth;
426 static GLuint _uniform_scene_position_uLightsArray;
427 static GLuint _uniform_scene_position_uLightsIndex;
428 static void shader_scene_position_uMdl(m4x3f m){
429 glUniformMatrix4x3fv(_uniform_scene_position_uMdl,1,GL_FALSE,(float*)m);
430 }
431 static void shader_scene_position_uPv(m4x4f m){
432 glUniformMatrix4fv(_uniform_scene_position_uPv,1,GL_FALSE,(float*)m);
433 }
434 static void shader_scene_position_uPvmPrev(m4x4f m){
435 glUniformMatrix4fv(_uniform_scene_position_uPvmPrev,1,GL_FALSE,(float*)m);
436 }
437 static void shader_scene_position_uCamera(v3f v){
438 glUniform3fv(_uniform_scene_position_uCamera,1,v);
439 }
440 static void shader_scene_position_uBoard0(v3f v){
441 glUniform3fv(_uniform_scene_position_uBoard0,1,v);
442 }
443 static void shader_scene_position_uBoard1(v3f v){
444 glUniform3fv(_uniform_scene_position_uBoard1,1,v);
445 }
446 static void shader_scene_position_g_world_depth(int i){
447 glUniform1i(_uniform_scene_position_g_world_depth,i);
448 }
449 static void shader_scene_position_register(void){
450 vg_shader_register( &_shader_scene_position );
451 }
452 static void shader_scene_position_use(void){ glUseProgram(_shader_scene_position.id); }
453 static void shader_scene_position_link(void){
454 _uniform_scene_position_uMdl = glGetUniformLocation( _shader_scene_position.id, "uMdl" );
455 _uniform_scene_position_uPv = glGetUniformLocation( _shader_scene_position.id, "uPv" );
456 _uniform_scene_position_uPvmPrev = glGetUniformLocation( _shader_scene_position.id, "uPvmPrev" );
457 _uniform_scene_position_uCamera = glGetUniformLocation( _shader_scene_position.id, "uCamera" );
458 _uniform_scene_position_uBoard0 = glGetUniformLocation( _shader_scene_position.id, "uBoard0" );
459 _uniform_scene_position_uBoard1 = glGetUniformLocation( _shader_scene_position.id, "uBoard1" );
460 _uniform_scene_position_g_world_depth = glGetUniformLocation( _shader_scene_position.id, "g_world_depth" );
461 _uniform_scene_position_uLightsArray = glGetUniformLocation( _shader_scene_position.id, "uLightsArray" );
462 _uniform_scene_position_uLightsIndex = glGetUniformLocation( _shader_scene_position.id, "uLightsIndex" );
463 }
464 #endif /* SHADER_scene_position_H */