now we're doing a bunch of them
[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 .static_src =
11 "layout (location=0) in vec3 a_co;\n"
12 "layout (location=1) in vec4 a_norm;\n"
13 "layout (location=2) in vec2 a_uv;\n"
14 "layout (location=3) in ivec4 a_lights;\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 7 0 \n"
35 "\n"
36 "uniform mat4x3 uMdl;\n"
37 "uniform mat4 uPv;\n"
38 "uniform mat4 uPvmPrev;\n"
39 "uniform samplerBuffer uLightsArray;\n"
40 "\n"
41 "out vec2 aUv;\n"
42 "out vec4 aNorm;\n"
43 "out vec3 aCo;\n"
44 "out vec3 aWorldCo;\n"
45 "flat out vec4 light_colours[3];\n"
46 "flat out vec4 light_positions[3];\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 " // read lights\n"
64 " light_colours[0] = texelFetch( uLightsArray, a_lights.x*2+0 );\n"
65 " light_colours[1] = texelFetch( uLightsArray, a_lights.y*2+0 );\n"
66 " light_colours[2] = texelFetch( uLightsArray, a_lights.z*2+0 );\n"
67 " light_positions[0] = texelFetch( uLightsArray, a_lights.x*2+1 );\n"
68 " light_positions[1] = texelFetch( uLightsArray, a_lights.y*2+1 );\n"
69 " light_positions[2] = texelFetch( uLightsArray, a_lights.z*2+1 );\n"
70 "}\n"
71 ""},
72 .fs =
73 {
74 .static_src =
75 "uniform sampler2D uTexMain;\n"
76 "uniform sampler2D uTexDudv;\n"
77 "uniform sampler2D uTexBack;\n"
78 "\n"
79 "uniform vec2 uInvRes;\n"
80 "uniform float uTime;\n"
81 "uniform vec3 uCamera;\n"
82 "uniform float uSurfaceY;\n"
83 "uniform vec3 uBoard0;\n"
84 "uniform vec3 uBoard1;\n"
85 "\n"
86 "uniform vec3 uShoreColour;\n"
87 "uniform vec3 uOceanColour;\n"
88 "\n"
89 "#line 1 1 \n"
90 "// :D\n"
91 "\n"
92 "in vec2 aUv;\n"
93 "in vec4 aNorm;\n"
94 "in vec3 aCo;\n"
95 "in vec3 aWorldCo;\n"
96 "flat in vec4 light_colours[3];\n"
97 "flat in vec4 light_positions[3];\n"
98 "\n"
99 "#line 1 1 \n"
100 "layout (location = 0) out vec4 oColour;\n"
101 "\n"
102 "layout (std140) uniform ub_world_lighting\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 " int g_light_count;\n"
112 " int g_light_preview;\n"
113 " int g_shadow_samples;\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()\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 = g_light_directions[0].xyz;\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 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
210 " vec3 light_pos, vec3 light_colour )\n"
211 "{\n"
212 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
213 "\n"
214 " float quadratic = dot(light_delta,light_delta);\n"
215 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
216 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
217 "\n"
218 " return light_colour*attenuation;\n"
219 "}\n"
220 "\n"
221 "#line 11 0 \n"
222 "\n"
223 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
224 "{\n"
225 " vec3 pa = p - a;\n"
226 " vec3 ba = b - a;\n"
227 "\n"
228 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
229 " return length( pa - ba*h );\n"
230 "}\n"
231 "\n"
232 "float compute_board_shadow()\n"
233 "{\n"
234 " // player shadow\n"
235 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
236 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
237 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
238 "\n"
239 " return 1.0 - player_shadow*0.8;\n"
240 "}\n"
241 "\n"
242 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
243 "{\n"
244 " // Lighting\n"
245 " vec3 halfview = uCamera - aWorldCo;\n"
246 " float fdist = length(halfview);\n"
247 " halfview /= fdist;\n"
248 "\n"
249 " vec3 total_light = newlight_compute_ambient();\n"
250 " \n"
251 " // Compute world lighting contribution and apply it according to the\n"
252 " // shadow map\n"
253 " //\n"
254 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
255 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
256 "\n"
257 " float world_shadow = newlight_compute_sun_shadow();\n"
258 " float board_shadow = compute_board_shadow();\n"
259 "\n"
260 " total_light += world_light * min( board_shadow, world_shadow );\n"
261 "\n"
262 " // Compute the other lights that exist in the map, not effected by the sun\n"
263 " // shadow\n"
264 "\n"
265 " total_light += newlight_compute_quadratic\n"
266 " ( \n"
267 " wnormal, halfview,\n"
268 " light_positions[0].xyz,\n"
269 " light_colours[0].rgb \n"
270 " ) * board_shadow;\n"
271 " total_light += newlight_compute_quadratic\n"
272 " ( \n"
273 " wnormal, halfview,\n"
274 " light_positions[1].xyz,\n"
275 " light_colours[1].rgb \n"
276 " ) * board_shadow;\n"
277 " total_light += newlight_compute_quadratic\n"
278 " ( \n"
279 " wnormal, halfview,\n"
280 " light_positions[2].xyz,\n"
281 " light_colours[2].rgb \n"
282 " ) * board_shadow;\n"
283 "\n"
284 " return apply_fog( diffuse * total_light, fdist );\n"
285 "}\n"
286 "\n"
287 "#line 16 0 \n"
288 "#line 1 2 \n"
289 "const float k_motion_lerp_amount = 0.01;\n"
290 "\n"
291 "#line 2 0 \n"
292 "\n"
293 "layout (location = 1) out vec2 oMotionVec;\n"
294 "\n"
295 "in vec3 aMotionVec0;\n"
296 "in vec3 aMotionVec1;\n"
297 "\n"
298 "void compute_motion_vectors()\n"
299 "{\n"
300 " // Write motion vectors\n"
301 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
302 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
303 "\n"
304 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
305 "}\n"
306 "\n"
307 "#line 17 0 \n"
308 "\n"
309 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
310 " vec4 beneath, vec4 above )\n"
311 "{\n"
312 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
313 "\n"
314 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
315 "\n"
316 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
317 " vec3 specdir = reflect( -lightdir, vnorm );\n"
318 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
319 " \n"
320 " // Depth \n"
321 " float depthblend = pow( beneath.r, 0.8 );\n"
322 "\n"
323 " // Composite\n"
324 " vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
325 " //vsurface += spec;\n"
326 "\n"
327 " return vec4( vsurface,depthblend );\n"
328 "}\n"
329 "\n"
330 "void main()\n"
331 "{\n"
332 " compute_motion_vectors();\n"
333 "\n"
334 " // Create texture coords\n"
335 " vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
336 " \n"
337 " // Surface colour composite\n"
338 " float depthvalue = clamp( -world_water_depth( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
339 "\n"
340 " vec2 world_coord = aCo.xz * 0.008;\n"
341 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
342 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
343 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
344 "\n"
345 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
346 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
347 " \n"
348 " // Foam\n"
349 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
350 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
351 "\n"
352 " // Lighting\n"
353 " vec3 halfview = -normalize( aCo-uCamera );\n"
354 "\n"
355 " // Sample textures\n"
356 " vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
357 " vec4 beneath = texture( uTexBack, ssuv );\n"
358 "\n"
359 " // Fog\n"
360 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
361 "\n"
362 " // Composite\n"
363 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
364 " vsurface.a -= fdist;\n"
365 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
366 "}\n"
367 ""},
368 };
369
370 static GLuint _uniform_scene_water_uMdl;
371 static GLuint _uniform_scene_water_uPv;
372 static GLuint _uniform_scene_water_uPvmPrev;
373 static GLuint _uniform_scene_water_uLightsArray;
374 static GLuint _uniform_scene_water_uTexMain;
375 static GLuint _uniform_scene_water_uTexDudv;
376 static GLuint _uniform_scene_water_uTexBack;
377 static GLuint _uniform_scene_water_uInvRes;
378 static GLuint _uniform_scene_water_uTime;
379 static GLuint _uniform_scene_water_uCamera;
380 static GLuint _uniform_scene_water_uSurfaceY;
381 static GLuint _uniform_scene_water_uBoard0;
382 static GLuint _uniform_scene_water_uBoard1;
383 static GLuint _uniform_scene_water_uShoreColour;
384 static GLuint _uniform_scene_water_uOceanColour;
385 static GLuint _uniform_scene_water_g_world_depth;
386 static void shader_scene_water_uMdl(m4x3f m){
387 glUniformMatrix4x3fv(_uniform_scene_water_uMdl,1,GL_FALSE,(float*)m);
388 }
389 static void shader_scene_water_uPv(m4x4f m){
390 glUniformMatrix4fv(_uniform_scene_water_uPv,1,GL_FALSE,(float*)m);
391 }
392 static void shader_scene_water_uPvmPrev(m4x4f m){
393 glUniformMatrix4fv(_uniform_scene_water_uPvmPrev,1,GL_FALSE,(float*)m);
394 }
395 static void shader_scene_water_uTexMain(int i){
396 glUniform1i(_uniform_scene_water_uTexMain,i);
397 }
398 static void shader_scene_water_uTexDudv(int i){
399 glUniform1i(_uniform_scene_water_uTexDudv,i);
400 }
401 static void shader_scene_water_uTexBack(int i){
402 glUniform1i(_uniform_scene_water_uTexBack,i);
403 }
404 static void shader_scene_water_uInvRes(v2f v){
405 glUniform2fv(_uniform_scene_water_uInvRes,1,v);
406 }
407 static void shader_scene_water_uTime(float f){
408 glUniform1f(_uniform_scene_water_uTime,f);
409 }
410 static void shader_scene_water_uCamera(v3f v){
411 glUniform3fv(_uniform_scene_water_uCamera,1,v);
412 }
413 static void shader_scene_water_uSurfaceY(float f){
414 glUniform1f(_uniform_scene_water_uSurfaceY,f);
415 }
416 static void shader_scene_water_uBoard0(v3f v){
417 glUniform3fv(_uniform_scene_water_uBoard0,1,v);
418 }
419 static void shader_scene_water_uBoard1(v3f v){
420 glUniform3fv(_uniform_scene_water_uBoard1,1,v);
421 }
422 static void shader_scene_water_uShoreColour(v3f v){
423 glUniform3fv(_uniform_scene_water_uShoreColour,1,v);
424 }
425 static void shader_scene_water_uOceanColour(v3f v){
426 glUniform3fv(_uniform_scene_water_uOceanColour,1,v);
427 }
428 static void shader_scene_water_g_world_depth(int i){
429 glUniform1i(_uniform_scene_water_g_world_depth,i);
430 }
431 static void shader_scene_water_register(void){
432 vg_shader_register( &_shader_scene_water );
433 }
434 static void shader_scene_water_use(void){ glUseProgram(_shader_scene_water.id); }
435 static void shader_scene_water_link(void){
436 _uniform_scene_water_uMdl = glGetUniformLocation( _shader_scene_water.id, "uMdl" );
437 _uniform_scene_water_uPv = glGetUniformLocation( _shader_scene_water.id, "uPv" );
438 _uniform_scene_water_uPvmPrev = glGetUniformLocation( _shader_scene_water.id, "uPvmPrev" );
439 _uniform_scene_water_uLightsArray = glGetUniformLocation( _shader_scene_water.id, "uLightsArray" );
440 _uniform_scene_water_uTexMain = glGetUniformLocation( _shader_scene_water.id, "uTexMain" );
441 _uniform_scene_water_uTexDudv = glGetUniformLocation( _shader_scene_water.id, "uTexDudv" );
442 _uniform_scene_water_uTexBack = glGetUniformLocation( _shader_scene_water.id, "uTexBack" );
443 _uniform_scene_water_uInvRes = glGetUniformLocation( _shader_scene_water.id, "uInvRes" );
444 _uniform_scene_water_uTime = glGetUniformLocation( _shader_scene_water.id, "uTime" );
445 _uniform_scene_water_uCamera = glGetUniformLocation( _shader_scene_water.id, "uCamera" );
446 _uniform_scene_water_uSurfaceY = glGetUniformLocation( _shader_scene_water.id, "uSurfaceY" );
447 _uniform_scene_water_uBoard0 = glGetUniformLocation( _shader_scene_water.id, "uBoard0" );
448 _uniform_scene_water_uBoard1 = glGetUniformLocation( _shader_scene_water.id, "uBoard1" );
449 _uniform_scene_water_uShoreColour = glGetUniformLocation( _shader_scene_water.id, "uShoreColour" );
450 _uniform_scene_water_uOceanColour = glGetUniformLocation( _shader_scene_water.id, "uOceanColour" );
451 _uniform_scene_water_g_world_depth = glGetUniformLocation( _shader_scene_water.id, "g_world_depth" );
452 }
453 #endif /* SHADER_scene_water_H */