1658e96af822cc9393738536d0271e4617af881b
[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 "\n"
40 "out vec2 aUv;\n"
41 "out vec4 aNorm;\n"
42 "out vec3 aCo;\n"
43 "out vec3 aWorldCo;\n"
44 "flat out ivec4 aLights;\n"
45 "\n"
46 "void main()\n"
47 "{\n"
48 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
49 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
50 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
51 "\n"
52 " vs_motion_out( vproj0, vproj1 );\n"
53 "\n"
54 " gl_Position = vproj0;\n"
55 "\n"
56 " aUv = a_uv;\n"
57 " aNorm = vec4( mat3(uMdl) * a_norm.xyz, a_norm.w );\n"
58 " aCo = a_co;\n"
59 " aWorldCo = world_pos0;\n"
60 " aLights = a_lights;\n"
61 "}\n"
62 ""},
63 .fs =
64 {
65 .static_src =
66 "uniform sampler2D uTexMain;\n"
67 "uniform sampler2D uTexDudv;\n"
68 "uniform sampler2D uTexBack;\n"
69 "\n"
70 "uniform vec2 uInvRes;\n"
71 "uniform float uTime;\n"
72 "uniform vec3 uCamera;\n"
73 "uniform float uSurfaceY;\n"
74 "uniform vec3 uBoard0;\n"
75 "uniform vec3 uBoard1;\n"
76 "\n"
77 "uniform vec3 uShoreColour;\n"
78 "uniform vec3 uOceanColour;\n"
79 "\n"
80 "in vec2 aUv;\n"
81 "in vec4 aNorm;\n"
82 "in vec3 aCo;\n"
83 "in vec3 aWorldCo;\n"
84 "flat in ivec4 aLights;\n"
85 "\n"
86 "#line 1 1 \n"
87 "// :D\n"
88 "\n"
89 "#line 1 1 \n"
90 "layout (location = 0) out vec4 oColour;\n"
91 "\n"
92 "layout (std140) uniform ub_world_lighting\n"
93 "{\n"
94 " vec4 g_light_colours[3];\n"
95 " vec4 g_light_directions[3];\n"
96 " vec4 g_ambient_colour;\n"
97 "\n"
98 " vec4 g_water_plane;\n"
99 " vec4 g_depth_bounds;\n"
100 " float g_water_fog;\n"
101 " int g_light_count;\n"
102 " int g_light_preview;\n"
103 " int g_shadow_samples;\n"
104 "\n"
105 " vec4 g_point_light_positions[32];\n"
106 " vec4 g_point_light_colours[32];\n"
107 "};\n"
108 "\n"
109 "uniform sampler2D g_world_depth;\n"
110 "\n"
111 "float world_depth_sample( vec3 pos )\n"
112 "{\n"
113 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
114 " return texture( g_world_depth, depth_coord ).r;\n"
115 "}\n"
116 "\n"
117 "float world_water_depth( vec3 pos )\n"
118 "{\n"
119 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
120 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
121 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
122 "}\n"
123 "\n"
124 "float shadow_sample( vec3 vdir )\n"
125 "{\n"
126 " vec3 sample_pos = aWorldCo + vdir;\n"
127 " float height_sample = world_depth_sample( sample_pos );\n"
128 "\n"
129 " float fdelta = height_sample - sample_pos.y;\n"
130 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
131 "}\n"
132 "\n"
133 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
134 "{\n"
135 " vec3 pa = p - a;\n"
136 " vec3 ba = b - a;\n"
137 "\n"
138 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
139 " return length( pa - ba*h );\n"
140 "}\n"
141 "\n"
142 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
143 "{\n"
144 " float dist = pow(fdist*0.0008,1.2);\n"
145 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
146 "}\n"
147 "\n"
148 "\n"
149 "// New lighting model\n"
150 "\n"
151 "vec3 newlight_compute_ambient()\n"
152 "{\n"
153 " return g_ambient_colour.rgb;\n"
154 "}\n"
155 "\n"
156 "float newlight_compute_sun_shadow()\n"
157 "{\n"
158 " if( g_shadow_samples == 0 )\n"
159 " {\n"
160 " return 1.0;\n"
161 " }\n"
162 "\n"
163 " float fspread = g_light_colours[0].w;\n"
164 " vec3 vdir = g_light_directions[0].xyz;\n"
165 " float flength = g_light_directions[0].w;\n"
166 "\n"
167 " float famt = 0.0;\n"
168 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
171 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
172 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
173 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
174 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
175 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
176 "\n"
177 " // player shadow\n"
178 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
179 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
180 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
181 "\n"
182 " return 1.0 - max( player_shadow*0.8, famt );\n"
183 "}\n"
184 "\n"
185 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
186 "{\n"
187 " vec3 vtotal = g_ambient_colour.rgb;\n"
188 "\n"
189 " for( int i=0; i<g_light_count; i++ )\n"
190 " {\n"
191 " vec3 vcolour = g_light_colours[i].rgb;\n"
192 " vec3 vdir = g_light_directions[i].xyz;\n"
193 "\n"
194 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
195 " vtotal += vcolour*flight;\n"
196 " }\n"
197 "\n"
198 " return vtotal;\n"
199 "}\n"
200 "\n"
201 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
202 "{\n"
203 " vec3 vcolour = g_light_colours[0].rgb;\n"
204 " vec3 vdir = g_light_directions[0].xyz;\n"
205 "\n"
206 " vec3 specdir = reflect( -vdir, wnormal );\n"
207 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
208 " return vcolour*spec*fintensity;\n"
209 "}\n"
210 "\n"
211 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
212 " vec3 light_pos, vec3 light_colour )\n"
213 "{\n"
214 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
215 "\n"
216 " float quadratic = dot(light_delta,light_delta);\n"
217 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
218 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
219 "\n"
220 " return light_colour*attenuation;\n"
221 "}\n"
222 "\n"
223 "#line 4 0 \n"
224 "\n"
225 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
226 "{\n"
227 " // Lighting\n"
228 " vec3 halfview = uCamera - aWorldCo;\n"
229 " float fdist = length(halfview);\n"
230 " halfview /= fdist;\n"
231 "\n"
232 " vec3 total_light = newlight_compute_ambient();\n"
233 " \n"
234 " // Compute world lighting contribution and apply it according to the\n"
235 " // shadow map\n"
236 " //\n"
237 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
238 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
239 "\n"
240 " float world_shadow = newlight_compute_sun_shadow();\n"
241 "\n"
242 " total_light += world_light * world_shadow;\n"
243 "\n"
244 " // Compute the other lights that exist in the map, not effected by the sun\n"
245 " // shadow\n"
246 " total_light += newlight_compute_quadratic\n"
247 " ( \n"
248 " wnormal, halfview,\n"
249 " g_point_light_positions[ aLights.x ].xyz,\n"
250 " g_point_light_colours[ aLights.x ].rgb \n"
251 " );\n"
252 " total_light += newlight_compute_quadratic\n"
253 " ( \n"
254 " wnormal, halfview,\n"
255 " g_point_light_positions[ aLights.y ].xyz,\n"
256 " g_point_light_colours[ aLights.y ].rgb \n"
257 " );\n"
258 " total_light += newlight_compute_quadratic\n"
259 " ( \n"
260 " wnormal, halfview,\n"
261 " g_point_light_positions[ aLights.z ].xyz,\n"
262 " g_point_light_colours[ aLights.z ].rgb \n"
263 " );\n"
264 "\n"
265 " return apply_fog( diffuse * total_light, fdist );\n"
266 "}\n"
267 "\n"
268 "#line 22 0 \n"
269 "#line 1 2 \n"
270 "const float k_motion_lerp_amount = 0.01;\n"
271 "\n"
272 "#line 2 0 \n"
273 "\n"
274 "layout (location = 1) out vec2 oMotionVec;\n"
275 "\n"
276 "in vec3 aMotionVec0;\n"
277 "in vec3 aMotionVec1;\n"
278 "\n"
279 "void compute_motion_vectors()\n"
280 "{\n"
281 " // Write motion vectors\n"
282 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
283 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
284 "\n"
285 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
286 "}\n"
287 "\n"
288 "#line 23 0 \n"
289 "\n"
290 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
291 " vec4 beneath, vec4 above )\n"
292 "{\n"
293 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
294 "\n"
295 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
296 "\n"
297 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
298 " vec3 specdir = reflect( -lightdir, vnorm );\n"
299 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
300 " \n"
301 " // Depth \n"
302 " float depthblend = pow( beneath.r, 0.8 );\n"
303 "\n"
304 " // Composite\n"
305 " vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
306 " //vsurface += spec;\n"
307 "\n"
308 " return vec4( vsurface,depthblend );\n"
309 "}\n"
310 "\n"
311 "void main()\n"
312 "{\n"
313 " compute_motion_vectors();\n"
314 "\n"
315 " // Create texture coords\n"
316 " vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
317 " \n"
318 " // Surface colour composite\n"
319 " float depthvalue = clamp( -world_water_depth( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
320 "\n"
321 " vec2 world_coord = aCo.xz * 0.008;\n"
322 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
323 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
324 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
325 "\n"
326 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
327 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
328 " \n"
329 " // Foam\n"
330 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
331 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
332 "\n"
333 " // Lighting\n"
334 " vec3 halfview = -normalize( aCo-uCamera );\n"
335 "\n"
336 " // Sample textures\n"
337 " vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
338 " vec4 beneath = texture( uTexBack, ssuv );\n"
339 "\n"
340 " // Fog\n"
341 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
342 "\n"
343 " // Composite\n"
344 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
345 " vsurface.a -= fdist;\n"
346 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
347 "}\n"
348 ""},
349 };
350
351 static GLuint _uniform_scene_water_uMdl;
352 static GLuint _uniform_scene_water_uPv;
353 static GLuint _uniform_scene_water_uPvmPrev;
354 static GLuint _uniform_scene_water_uTexMain;
355 static GLuint _uniform_scene_water_uTexDudv;
356 static GLuint _uniform_scene_water_uTexBack;
357 static GLuint _uniform_scene_water_uInvRes;
358 static GLuint _uniform_scene_water_uTime;
359 static GLuint _uniform_scene_water_uCamera;
360 static GLuint _uniform_scene_water_uSurfaceY;
361 static GLuint _uniform_scene_water_uBoard0;
362 static GLuint _uniform_scene_water_uBoard1;
363 static GLuint _uniform_scene_water_uShoreColour;
364 static GLuint _uniform_scene_water_uOceanColour;
365 static GLuint _uniform_scene_water_g_world_depth;
366 static void shader_scene_water_uMdl(m4x3f m){
367 glUniformMatrix4x3fv(_uniform_scene_water_uMdl,1,GL_FALSE,(float*)m);
368 }
369 static void shader_scene_water_uPv(m4x4f m){
370 glUniformMatrix4fv(_uniform_scene_water_uPv,1,GL_FALSE,(float*)m);
371 }
372 static void shader_scene_water_uPvmPrev(m4x4f m){
373 glUniformMatrix4fv(_uniform_scene_water_uPvmPrev,1,GL_FALSE,(float*)m);
374 }
375 static void shader_scene_water_uTexMain(int i){
376 glUniform1i(_uniform_scene_water_uTexMain,i);
377 }
378 static void shader_scene_water_uTexDudv(int i){
379 glUniform1i(_uniform_scene_water_uTexDudv,i);
380 }
381 static void shader_scene_water_uTexBack(int i){
382 glUniform1i(_uniform_scene_water_uTexBack,i);
383 }
384 static void shader_scene_water_uInvRes(v2f v){
385 glUniform2fv(_uniform_scene_water_uInvRes,1,v);
386 }
387 static void shader_scene_water_uTime(float f){
388 glUniform1f(_uniform_scene_water_uTime,f);
389 }
390 static void shader_scene_water_uCamera(v3f v){
391 glUniform3fv(_uniform_scene_water_uCamera,1,v);
392 }
393 static void shader_scene_water_uSurfaceY(float f){
394 glUniform1f(_uniform_scene_water_uSurfaceY,f);
395 }
396 static void shader_scene_water_uBoard0(v3f v){
397 glUniform3fv(_uniform_scene_water_uBoard0,1,v);
398 }
399 static void shader_scene_water_uBoard1(v3f v){
400 glUniform3fv(_uniform_scene_water_uBoard1,1,v);
401 }
402 static void shader_scene_water_uShoreColour(v3f v){
403 glUniform3fv(_uniform_scene_water_uShoreColour,1,v);
404 }
405 static void shader_scene_water_uOceanColour(v3f v){
406 glUniform3fv(_uniform_scene_water_uOceanColour,1,v);
407 }
408 static void shader_scene_water_g_world_depth(int i){
409 glUniform1i(_uniform_scene_water_g_world_depth,i);
410 }
411 static void shader_scene_water_register(void){
412 vg_shader_register( &_shader_scene_water );
413 }
414 static void shader_scene_water_use(void){ glUseProgram(_shader_scene_water.id); }
415 static void shader_scene_water_link(void){
416 _uniform_scene_water_uMdl = glGetUniformLocation( _shader_scene_water.id, "uMdl" );
417 _uniform_scene_water_uPv = glGetUniformLocation( _shader_scene_water.id, "uPv" );
418 _uniform_scene_water_uPvmPrev = glGetUniformLocation( _shader_scene_water.id, "uPvmPrev" );
419 _uniform_scene_water_uTexMain = glGetUniformLocation( _shader_scene_water.id, "uTexMain" );
420 _uniform_scene_water_uTexDudv = glGetUniformLocation( _shader_scene_water.id, "uTexDudv" );
421 _uniform_scene_water_uTexBack = glGetUniformLocation( _shader_scene_water.id, "uTexBack" );
422 _uniform_scene_water_uInvRes = glGetUniformLocation( _shader_scene_water.id, "uInvRes" );
423 _uniform_scene_water_uTime = glGetUniformLocation( _shader_scene_water.id, "uTime" );
424 _uniform_scene_water_uCamera = glGetUniformLocation( _shader_scene_water.id, "uCamera" );
425 _uniform_scene_water_uSurfaceY = glGetUniformLocation( _shader_scene_water.id, "uSurfaceY" );
426 _uniform_scene_water_uBoard0 = glGetUniformLocation( _shader_scene_water.id, "uBoard0" );
427 _uniform_scene_water_uBoard1 = glGetUniformLocation( _shader_scene_water.id, "uBoard1" );
428 _uniform_scene_water_uShoreColour = glGetUniformLocation( _shader_scene_water.id, "uShoreColour" );
429 _uniform_scene_water_uOceanColour = glGetUniformLocation( _shader_scene_water.id, "uOceanColour" );
430 _uniform_scene_water_g_world_depth = glGetUniformLocation( _shader_scene_water.id, "g_world_depth" );
431 }
432 #endif /* SHADER_scene_water_H */