df shadows
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / water_fast.h
1 #ifndef SHADER_water_fast_H
2 #define SHADER_water_fast_H
3 static void shader_water_fast_link(void);
4 static void shader_water_fast_register(void);
5 static struct vg_shader _shader_water_fast = {
6 .name = "water_fast",
7 .link = shader_water_fast_link,
8 .vs =
9 {
10 .static_src =
11 "layout (location=0) in vec3 a_co;\n"
12 "layout (location=1) in vec3 a_norm;\n"
13 "layout (location=2) in vec2 a_uv;\n"
14 "layout (location=3) in vec4 a_colour;\n"
15 "layout (location=4) in vec4 a_weights;\n"
16 "layout (location=5) in ivec4 a_groups;\n"
17 "\n"
18 "#line 2 0 \n"
19 "#line 1 2 \n"
20 "const float k_motion_lerp_amount = 0.01;\n"
21 "\n"
22 "#line 2 0 \n"
23 "\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
26 "\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
28 "{\n"
29 " // This magically solves some artifacting errors!\n"
30 " //\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
32 "\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
35 "}\n"
36 "\n"
37 "#line 3 0 \n"
38 "\n"
39 "uniform mat4x3 uMdl;\n"
40 "uniform mat4 uPv;\n"
41 "uniform mat4 uPvmPrev;\n"
42 "\n"
43 "out vec4 aColour;\n"
44 "out vec2 aUv;\n"
45 "out vec3 aNorm;\n"
46 "out vec3 aCo;\n"
47 "out vec3 aWorldCo;\n"
48 "\n"
49 "void main()\n"
50 "{\n"
51 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
52 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
53 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
54 "\n"
55 " vs_motion_out( vproj0, vproj1 );\n"
56 "\n"
57 " gl_Position = vproj0;\n"
58 " aWorldCo = world_pos0;\n"
59 " aColour = a_colour;\n"
60 " aUv = a_uv;\n"
61 " aNorm = mat3(uMdl) * a_norm;\n"
62 " aCo = a_co;\n"
63 "}\n"
64 ""},
65 .fs =
66 {
67 .static_src =
68 "uniform sampler2D uTexDudv;\n"
69 "\n"
70 "uniform float uTime;\n"
71 "uniform vec3 uCamera;\n"
72 "uniform float uSurfaceY;\n"
73 "uniform vec3 uBoard0;\n"
74 "uniform vec3 uBoard1;\n"
75 "\n"
76 "uniform vec3 uShoreColour;\n"
77 "uniform vec3 uOceanColour;\n"
78 "\n"
79 "in vec4 aColour;\n"
80 "in vec2 aUv;\n"
81 "in vec3 aNorm;\n"
82 "in vec3 aCo;\n"
83 "in vec3 aWorldCo;\n"
84 "\n"
85 "#line 1 1 \n"
86 "layout (location = 0) out vec4 oColour;\n"
87 "\n"
88 "layout (std140) uniform ub_world_lighting\n"
89 "{\n"
90 " vec4 g_light_colours[3];\n"
91 " vec4 g_light_directions[3];\n"
92 " vec4 g_ambient_colour;\n"
93 "\n"
94 " vec4 g_water_plane;\n"
95 " vec4 g_depth_bounds;\n"
96 " float g_water_fog;\n"
97 " int g_light_count;\n"
98 " int g_light_preview;\n"
99 " int g_shadow_samples;\n"
100 "};\n"
101 "\n"
102 "uniform sampler2D g_world_depth;\n"
103 "\n"
104 "// Standard diffuse + spec models\n"
105 "// ==============================\n"
106 "\n"
107 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
108 "{\n"
109 " vec3 vtotal = g_ambient_colour.rgb;\n"
110 "\n"
111 " for( int i=0; i<g_light_count; i++ )\n"
112 " {\n"
113 " vec3 vcolour = g_light_colours[i].rgb;\n"
114 " vec3 vdir = g_light_directions[i].xyz;\n"
115 "\n"
116 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
117 " vtotal += vcolour*flight;\n"
118 " }\n"
119 "\n"
120 " return vfrag * vtotal;\n"
121 "}\n"
122 "\n"
123 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
124 "{\n"
125 " vec3 vcolour = g_light_colours[0].rgb;\n"
126 " vec3 vdir = g_light_directions[0].xyz;\n"
127 "\n"
128 " vec3 specdir = reflect( -vdir, wnormal );\n"
129 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
130 " return vfrag + vcolour*spec*fintensity;\n"
131 "}\n"
132 "\n"
133 "float world_depth_sample( vec3 pos )\n"
134 "{\n"
135 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
136 " return texture( g_world_depth, depth_coord ).r;\n"
137 "}\n"
138 "\n"
139 "float shadow_sample( vec3 vdir )\n"
140 "{\n"
141 " vec3 sample_pos = aWorldCo + vdir;\n"
142 " float height_sample = world_depth_sample( sample_pos );\n"
143 "\n"
144 " float fdelta = height_sample - sample_pos.y;\n"
145 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
146 "}\n"
147 "\n"
148 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
149 "{\n"
150 " float faccum = 0.0;\n"
151 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
152 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
153 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
154 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
155 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
156 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
157 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
158 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
159 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
160 "}\n"
161 "\n"
162 "// FIXME\n"
163 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
164 "{\n"
165 " vec3 pa = p - a;\n"
166 " vec3 ba = b - a;\n"
167 "\n"
168 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
169 " return length( pa - ba*h );\n"
170 "}\n"
171 "\n"
172 "vec3 do_light_shadowing( vec3 vfrag )\n"
173 "{\n"
174 " if( g_shadow_samples == 0 )\n"
175 " {\n"
176 " return vfrag;\n"
177 " }\n"
178 "\n"
179 " float fspread = g_light_colours[0].w;\n"
180 " vec3 vdir = g_light_directions[0].xyz;\n"
181 " float flength = g_light_directions[0].w;\n"
182 "\n"
183 " float famt = 0.0;\n"
184 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
185 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
186 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
187 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
188 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
189 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
190 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
191 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
192 "\n"
193 " // player shadow\n"
194 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.03 );\n"
195 " float player_shadow = max( 1.0-dist_to_player*1.7, 0.0 );\n"
196 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
197 "\n"
198 " famt = max( player_shadow*0.6, famt );\n"
199 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
200 "}\n"
201 "\n"
202 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
203 "{\n"
204 " float dist = pow(fdist*0.0008,1.2);\n"
205 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
206 "}\n"
207 "\n"
208 "#line 19 0 \n"
209 "#line 1 2 \n"
210 "const float k_motion_lerp_amount = 0.01;\n"
211 "\n"
212 "#line 2 0 \n"
213 "\n"
214 "layout (location = 1) out vec2 oMotionVec;\n"
215 "\n"
216 "in vec3 aMotionVec0;\n"
217 "in vec3 aMotionVec1;\n"
218 "\n"
219 "void compute_motion_vectors()\n"
220 "{\n"
221 " // Write motion vectors\n"
222 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
223 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
224 "\n"
225 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
226 "}\n"
227 "\n"
228 "#line 20 0 \n"
229 "\n"
230 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )\n"
231 "{\n"
232 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
233 "\n"
234 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
235 "\n"
236 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
237 " vec3 specdir = reflect( -lightdir, vnorm );\n"
238 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
239 " \n"
240 " return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );\n"
241 "}\n"
242 "\n"
243 "void main()\n"
244 "{\n"
245 " compute_motion_vectors();\n"
246 "\n"
247 " // Surface colour composite\n"
248 " float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
249 "\n"
250 " vec2 world_coord = aCo.xz * 0.008;\n"
251 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
252 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
253 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
254 "\n"
255 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
256 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
257 " \n"
258 " // Foam\n"
259 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
260 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
261 "\n"
262 " // Lighting\n"
263 " vec3 halfview = -normalize( aCo-uCamera );\n"
264 "\n"
265 " // Fog\n"
266 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
267 "\n"
268 " // Composite\n"
269 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );\n"
270 " vsurface.a -= fdist;\n"
271 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
272 "}\n"
273 ""},
274 };
275
276 static GLuint _uniform_water_fast_uMdl;
277 static GLuint _uniform_water_fast_uPv;
278 static GLuint _uniform_water_fast_uPvmPrev;
279 static GLuint _uniform_water_fast_uTexDudv;
280 static GLuint _uniform_water_fast_uTime;
281 static GLuint _uniform_water_fast_uCamera;
282 static GLuint _uniform_water_fast_uSurfaceY;
283 static GLuint _uniform_water_fast_uBoard0;
284 static GLuint _uniform_water_fast_uBoard1;
285 static GLuint _uniform_water_fast_uShoreColour;
286 static GLuint _uniform_water_fast_uOceanColour;
287 static GLuint _uniform_water_fast_g_world_depth;
288 static void shader_water_fast_uMdl(m4x3f m){
289 glUniformMatrix4x3fv(_uniform_water_fast_uMdl,1,GL_FALSE,(float*)m);
290 }
291 static void shader_water_fast_uPv(m4x4f m){
292 glUniformMatrix4fv(_uniform_water_fast_uPv,1,GL_FALSE,(float*)m);
293 }
294 static void shader_water_fast_uPvmPrev(m4x4f m){
295 glUniformMatrix4fv(_uniform_water_fast_uPvmPrev,1,GL_FALSE,(float*)m);
296 }
297 static void shader_water_fast_uTexDudv(int i){
298 glUniform1i(_uniform_water_fast_uTexDudv,i);
299 }
300 static void shader_water_fast_uTime(float f){
301 glUniform1f(_uniform_water_fast_uTime,f);
302 }
303 static void shader_water_fast_uCamera(v3f v){
304 glUniform3fv(_uniform_water_fast_uCamera,1,v);
305 }
306 static void shader_water_fast_uSurfaceY(float f){
307 glUniform1f(_uniform_water_fast_uSurfaceY,f);
308 }
309 static void shader_water_fast_uBoard0(v3f v){
310 glUniform3fv(_uniform_water_fast_uBoard0,1,v);
311 }
312 static void shader_water_fast_uBoard1(v3f v){
313 glUniform3fv(_uniform_water_fast_uBoard1,1,v);
314 }
315 static void shader_water_fast_uShoreColour(v3f v){
316 glUniform3fv(_uniform_water_fast_uShoreColour,1,v);
317 }
318 static void shader_water_fast_uOceanColour(v3f v){
319 glUniform3fv(_uniform_water_fast_uOceanColour,1,v);
320 }
321 static void shader_water_fast_g_world_depth(int i){
322 glUniform1i(_uniform_water_fast_g_world_depth,i);
323 }
324 static void shader_water_fast_register(void){
325 vg_shader_register( &_shader_water_fast );
326 }
327 static void shader_water_fast_use(void){ glUseProgram(_shader_water_fast.id); }
328 static void shader_water_fast_link(void){
329 _uniform_water_fast_uMdl = glGetUniformLocation( _shader_water_fast.id, "uMdl" );
330 _uniform_water_fast_uPv = glGetUniformLocation( _shader_water_fast.id, "uPv" );
331 _uniform_water_fast_uPvmPrev = glGetUniformLocation( _shader_water_fast.id, "uPvmPrev" );
332 _uniform_water_fast_uTexDudv = glGetUniformLocation( _shader_water_fast.id, "uTexDudv" );
333 _uniform_water_fast_uTime = glGetUniformLocation( _shader_water_fast.id, "uTime" );
334 _uniform_water_fast_uCamera = glGetUniformLocation( _shader_water_fast.id, "uCamera" );
335 _uniform_water_fast_uSurfaceY = glGetUniformLocation( _shader_water_fast.id, "uSurfaceY" );
336 _uniform_water_fast_uBoard0 = glGetUniformLocation( _shader_water_fast.id, "uBoard0" );
337 _uniform_water_fast_uBoard1 = glGetUniformLocation( _shader_water_fast.id, "uBoard1" );
338 _uniform_water_fast_uShoreColour = glGetUniformLocation( _shader_water_fast.id, "uShoreColour" );
339 _uniform_water_fast_uOceanColour = glGetUniformLocation( _shader_water_fast.id, "uOceanColour" );
340 _uniform_water_fast_g_world_depth = glGetUniformLocation( _shader_water_fast.id, "g_world_depth" );
341 }
342 #endif /* SHADER_water_fast_H */