6e19ac3424274471f5ff539891d59a42fb6500eb
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_water_fast.h
1 #ifndef SHADER_scene_water_fast_H
2 #define SHADER_scene_water_fast_H
3 static void shader_scene_water_fast_link(void);
4 static void shader_scene_water_fast_register(void);
5 static struct vg_shader _shader_scene_water_fast = {
6 .name = "scene_water_fast",
7 .link = shader_scene_water_fast_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 uTexDudv;\n"
67 "\n"
68 "uniform float uTime;\n"
69 "uniform vec3 uCamera;\n"
70 "uniform float uSurfaceY;\n"
71 "uniform vec3 uBoard0;\n"
72 "uniform vec3 uBoard1;\n"
73 "\n"
74 "uniform vec3 uShoreColour;\n"
75 "uniform vec3 uOceanColour;\n"
76 "\n"
77 "in vec2 aUv;\n"
78 "in vec4 aNorm;\n"
79 "in vec3 aCo;\n"
80 "in vec3 aWorldCo;\n"
81 "flat in ivec4 aLights;\n"
82 "\n"
83 "#line 1 1 \n"
84 "// :D\n"
85 "\n"
86 "#line 1 1 \n"
87 "layout (location = 0) out vec4 oColour;\n"
88 "\n"
89 "layout (std140) uniform ub_world_lighting\n"
90 "{\n"
91 " vec4 g_light_colours[3];\n"
92 " vec4 g_light_directions[3];\n"
93 " vec4 g_ambient_colour;\n"
94 "\n"
95 " vec4 g_water_plane;\n"
96 " vec4 g_depth_bounds;\n"
97 " float g_water_fog;\n"
98 " int g_light_count;\n"
99 " int g_light_preview;\n"
100 " int g_shadow_samples;\n"
101 "\n"
102 " vec4 g_point_light_positions[32];\n"
103 " vec4 g_point_light_colours[32];\n"
104 "};\n"
105 "\n"
106 "uniform sampler2D g_world_depth;\n"
107 "\n"
108 "float world_depth_sample( vec3 pos )\n"
109 "{\n"
110 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
111 " return texture( g_world_depth, depth_coord ).r;\n"
112 "}\n"
113 "\n"
114 "float world_water_depth( vec3 pos )\n"
115 "{\n"
116 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
117 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
118 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
119 "}\n"
120 "\n"
121 "float shadow_sample( vec3 vdir )\n"
122 "{\n"
123 " vec3 sample_pos = aWorldCo + vdir;\n"
124 " float height_sample = world_depth_sample( sample_pos );\n"
125 "\n"
126 " float fdelta = height_sample - sample_pos.y;\n"
127 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
128 "}\n"
129 "\n"
130 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
131 "{\n"
132 " vec3 pa = p - a;\n"
133 " vec3 ba = b - a;\n"
134 "\n"
135 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
136 " return length( pa - ba*h );\n"
137 "}\n"
138 "\n"
139 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
140 "{\n"
141 " float dist = pow(fdist*0.0008,1.2);\n"
142 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
143 "}\n"
144 "\n"
145 "\n"
146 "// New lighting model\n"
147 "\n"
148 "vec3 newlight_compute_ambient()\n"
149 "{\n"
150 " return g_ambient_colour.rgb;\n"
151 "}\n"
152 "\n"
153 "float newlight_compute_sun_shadow()\n"
154 "{\n"
155 " if( g_shadow_samples == 0 )\n"
156 " {\n"
157 " return 1.0;\n"
158 " }\n"
159 "\n"
160 " float fspread = g_light_colours[0].w;\n"
161 " vec3 vdir = g_light_directions[0].xyz;\n"
162 " float flength = g_light_directions[0].w;\n"
163 "\n"
164 " float famt = 0.0;\n"
165 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
168 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
169 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
172 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
173 "\n"
174 " // player shadow\n"
175 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
176 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
177 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
178 "\n"
179 " return 1.0 - max( player_shadow*0.8, famt );\n"
180 "}\n"
181 "\n"
182 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
183 "{\n"
184 " vec3 vtotal = g_ambient_colour.rgb;\n"
185 "\n"
186 " for( int i=0; i<g_light_count; i++ )\n"
187 " {\n"
188 " vec3 vcolour = g_light_colours[i].rgb;\n"
189 " vec3 vdir = g_light_directions[i].xyz;\n"
190 "\n"
191 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
192 " vtotal += vcolour*flight;\n"
193 " }\n"
194 "\n"
195 " return vtotal;\n"
196 "}\n"
197 "\n"
198 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
199 "{\n"
200 " vec3 vcolour = g_light_colours[0].rgb;\n"
201 " vec3 vdir = g_light_directions[0].xyz;\n"
202 "\n"
203 " vec3 specdir = reflect( -vdir, wnormal );\n"
204 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
205 " return vcolour*spec*fintensity;\n"
206 "}\n"
207 "\n"
208 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
209 " vec3 light_pos, vec3 light_colour )\n"
210 "{\n"
211 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
212 "\n"
213 " float quadratic = dot(light_delta,light_delta);\n"
214 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
215 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
216 "\n"
217 " return light_colour*attenuation;\n"
218 "}\n"
219 "\n"
220 "#line 4 0 \n"
221 "\n"
222 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
223 "{\n"
224 " // Lighting\n"
225 " vec3 halfview = uCamera - aWorldCo;\n"
226 " float fdist = length(halfview);\n"
227 " halfview /= fdist;\n"
228 "\n"
229 " vec3 total_light = newlight_compute_ambient();\n"
230 " \n"
231 " // Compute world lighting contribution and apply it according to the\n"
232 " // shadow map\n"
233 " //\n"
234 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
235 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
236 "\n"
237 " float world_shadow = newlight_compute_sun_shadow();\n"
238 "\n"
239 " total_light += world_light * world_shadow;\n"
240 "\n"
241 " // Compute the other lights that exist in the map, not effected by the sun\n"
242 " // shadow\n"
243 " total_light += newlight_compute_quadratic\n"
244 " ( \n"
245 " wnormal, halfview,\n"
246 " g_point_light_positions[ aLights.x ].xyz,\n"
247 " g_point_light_colours[ aLights.x ].rgb \n"
248 " );\n"
249 " total_light += newlight_compute_quadratic\n"
250 " ( \n"
251 " wnormal, halfview,\n"
252 " g_point_light_positions[ aLights.y ].xyz,\n"
253 " g_point_light_colours[ aLights.y ].rgb \n"
254 " );\n"
255 " total_light += newlight_compute_quadratic\n"
256 " ( \n"
257 " wnormal, halfview,\n"
258 " g_point_light_positions[ aLights.z ].xyz,\n"
259 " g_point_light_colours[ aLights.z ].rgb \n"
260 " );\n"
261 "\n"
262 " return apply_fog( diffuse * total_light, fdist );\n"
263 "}\n"
264 "\n"
265 "#line 19 0 \n"
266 "#line 1 2 \n"
267 "const float k_motion_lerp_amount = 0.01;\n"
268 "\n"
269 "#line 2 0 \n"
270 "\n"
271 "layout (location = 1) out vec2 oMotionVec;\n"
272 "\n"
273 "in vec3 aMotionVec0;\n"
274 "in vec3 aMotionVec1;\n"
275 "\n"
276 "void compute_motion_vectors()\n"
277 "{\n"
278 " // Write motion vectors\n"
279 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
280 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
281 "\n"
282 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
283 "}\n"
284 "\n"
285 "#line 20 0 \n"
286 "\n"
287 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )\n"
288 "{\n"
289 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
290 "\n"
291 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
292 "\n"
293 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
294 " vec3 specdir = reflect( -lightdir, vnorm );\n"
295 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
296 " \n"
297 " return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );\n"
298 "}\n"
299 "\n"
300 "void main()\n"
301 "{\n"
302 " compute_motion_vectors();\n"
303 "\n"
304 " // Surface colour composite\n"
305 " float depthvalue = clamp( -world_water_depth( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
306 "\n"
307 " vec2 world_coord = aCo.xz * 0.008;\n"
308 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
309 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
310 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
311 "\n"
312 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
313 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
314 " \n"
315 " // Foam\n"
316 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
317 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
318 "\n"
319 " // Lighting\n"
320 " vec3 halfview = -normalize( aCo-uCamera );\n"
321 "\n"
322 " // Fog\n"
323 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
324 "\n"
325 " // Composite\n"
326 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );\n"
327 " vsurface.a -= fdist;\n"
328 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
329 "}\n"
330 ""},
331 };
332
333 static GLuint _uniform_scene_water_fast_uMdl;
334 static GLuint _uniform_scene_water_fast_uPv;
335 static GLuint _uniform_scene_water_fast_uPvmPrev;
336 static GLuint _uniform_scene_water_fast_uTexDudv;
337 static GLuint _uniform_scene_water_fast_uTime;
338 static GLuint _uniform_scene_water_fast_uCamera;
339 static GLuint _uniform_scene_water_fast_uSurfaceY;
340 static GLuint _uniform_scene_water_fast_uBoard0;
341 static GLuint _uniform_scene_water_fast_uBoard1;
342 static GLuint _uniform_scene_water_fast_uShoreColour;
343 static GLuint _uniform_scene_water_fast_uOceanColour;
344 static GLuint _uniform_scene_water_fast_g_world_depth;
345 static void shader_scene_water_fast_uMdl(m4x3f m){
346 glUniformMatrix4x3fv(_uniform_scene_water_fast_uMdl,1,GL_FALSE,(float*)m);
347 }
348 static void shader_scene_water_fast_uPv(m4x4f m){
349 glUniformMatrix4fv(_uniform_scene_water_fast_uPv,1,GL_FALSE,(float*)m);
350 }
351 static void shader_scene_water_fast_uPvmPrev(m4x4f m){
352 glUniformMatrix4fv(_uniform_scene_water_fast_uPvmPrev,1,GL_FALSE,(float*)m);
353 }
354 static void shader_scene_water_fast_uTexDudv(int i){
355 glUniform1i(_uniform_scene_water_fast_uTexDudv,i);
356 }
357 static void shader_scene_water_fast_uTime(float f){
358 glUniform1f(_uniform_scene_water_fast_uTime,f);
359 }
360 static void shader_scene_water_fast_uCamera(v3f v){
361 glUniform3fv(_uniform_scene_water_fast_uCamera,1,v);
362 }
363 static void shader_scene_water_fast_uSurfaceY(float f){
364 glUniform1f(_uniform_scene_water_fast_uSurfaceY,f);
365 }
366 static void shader_scene_water_fast_uBoard0(v3f v){
367 glUniform3fv(_uniform_scene_water_fast_uBoard0,1,v);
368 }
369 static void shader_scene_water_fast_uBoard1(v3f v){
370 glUniform3fv(_uniform_scene_water_fast_uBoard1,1,v);
371 }
372 static void shader_scene_water_fast_uShoreColour(v3f v){
373 glUniform3fv(_uniform_scene_water_fast_uShoreColour,1,v);
374 }
375 static void shader_scene_water_fast_uOceanColour(v3f v){
376 glUniform3fv(_uniform_scene_water_fast_uOceanColour,1,v);
377 }
378 static void shader_scene_water_fast_g_world_depth(int i){
379 glUniform1i(_uniform_scene_water_fast_g_world_depth,i);
380 }
381 static void shader_scene_water_fast_register(void){
382 vg_shader_register( &_shader_scene_water_fast );
383 }
384 static void shader_scene_water_fast_use(void){ glUseProgram(_shader_scene_water_fast.id); }
385 static void shader_scene_water_fast_link(void){
386 _uniform_scene_water_fast_uMdl = glGetUniformLocation( _shader_scene_water_fast.id, "uMdl" );
387 _uniform_scene_water_fast_uPv = glGetUniformLocation( _shader_scene_water_fast.id, "uPv" );
388 _uniform_scene_water_fast_uPvmPrev = glGetUniformLocation( _shader_scene_water_fast.id, "uPvmPrev" );
389 _uniform_scene_water_fast_uTexDudv = glGetUniformLocation( _shader_scene_water_fast.id, "uTexDudv" );
390 _uniform_scene_water_fast_uTime = glGetUniformLocation( _shader_scene_water_fast.id, "uTime" );
391 _uniform_scene_water_fast_uCamera = glGetUniformLocation( _shader_scene_water_fast.id, "uCamera" );
392 _uniform_scene_water_fast_uSurfaceY = glGetUniformLocation( _shader_scene_water_fast.id, "uSurfaceY" );
393 _uniform_scene_water_fast_uBoard0 = glGetUniformLocation( _shader_scene_water_fast.id, "uBoard0" );
394 _uniform_scene_water_fast_uBoard1 = glGetUniformLocation( _shader_scene_water_fast.id, "uBoard1" );
395 _uniform_scene_water_fast_uShoreColour = glGetUniformLocation( _shader_scene_water_fast.id, "uShoreColour" );
396 _uniform_scene_water_fast_uOceanColour = glGetUniformLocation( _shader_scene_water_fast.id, "uOceanColour" );
397 _uniform_scene_water_fast_g_world_depth = glGetUniformLocation( _shader_scene_water_fast.id, "g_world_depth" );
398 }
399 #endif /* SHADER_scene_water_fast_H */