3 static void shader_water_link(void);
4 static void shader_water_register(void);
5 static struct vg_shader _shader_water
= {
7 .link
= shader_water_link
,
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"
20 "const float k_motion_lerp_amount = 0.01;\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
29 " // This magically solves some artifacting errors!\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
39 "uniform mat4x3 uMdl;\n"
41 "uniform mat4 uPvmPrev;\n"
47 "out vec3 aWorldCo;\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"
55 " vs_motion_out( vproj0, vproj1 );\n"
57 " gl_Position = vproj0;\n"
58 " aWorldCo = world_pos0;\n"
59 " aColour = a_colour;\n"
61 " aNorm = mat3(uMdl) * a_norm;\n"
68 "uniform sampler2D uTexMain;\n"
69 "uniform sampler2D uTexDudv;\n"
70 "uniform sampler2D uTexBack;\n"
72 "uniform vec2 uInvRes;\n"
73 "uniform float uTime;\n"
74 "uniform vec3 uCamera;\n"
75 "uniform float uSurfaceY;\n"
76 "uniform vec3 uBoard0;\n"
77 "uniform vec3 uBoard1;\n"
79 "uniform vec3 uShoreColour;\n"
80 "uniform vec3 uOceanColour;\n"
89 "layout (location = 0) out vec4 oColour;\n"
91 "layout (std140) uniform ub_world_lighting\n"
93 " vec4 g_light_colours[3];\n"
94 " vec4 g_light_directions[3];\n"
95 " vec4 g_ambient_colour;\n"
97 " vec4 g_water_plane;\n"
98 " vec4 g_depth_bounds;\n"
99 " float g_water_fog;\n"
100 " int g_light_count;\n"
101 " int g_light_preview;\n"
102 " int g_shadow_samples;\n"
105 "uniform sampler2D g_world_depth;\n"
107 "// Standard diffuse + spec models\n"
108 "// ==============================\n"
110 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
112 " vec3 vtotal = g_ambient_colour.rgb;\n"
114 " for( int i=0; i<g_light_count; i++ )\n"
116 " vec3 vcolour = g_light_colours[i].rgb;\n"
117 " vec3 vdir = g_light_directions[i].xyz;\n"
119 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
120 " vtotal += vcolour*flight;\n"
123 " return vfrag * vtotal;\n"
126 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
128 " vec3 vcolour = g_light_colours[0].rgb;\n"
129 " vec3 vdir = g_light_directions[0].xyz;\n"
131 " vec3 specdir = reflect( -vdir, wnormal );\n"
132 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
133 " return vfrag + vcolour*spec*fintensity;\n"
136 "float world_depth_sample( vec3 pos )\n"
138 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
139 " return texture( g_world_depth, depth_coord ).r;\n"
142 "float shadow_sample( vec3 vdir )\n"
144 " vec3 sample_pos = aWorldCo + vdir;\n"
145 " float height_sample = world_depth_sample( sample_pos );\n"
147 " float fdelta = height_sample - sample_pos.y;\n"
148 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
151 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
153 " float faccum = 0.0;\n"
154 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
155 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
156 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
157 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
158 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
159 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
160 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
161 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
162 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
166 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
168 " vec3 pa = p - a;\n"
169 " vec3 ba = b - a;\n"
171 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
172 " return length( pa - ba*h );\n"
175 "vec3 do_light_shadowing( vec3 vfrag )\n"
177 " if( g_shadow_samples == 0 )\n"
182 " float fspread = g_light_colours[0].w;\n"
183 " vec3 vdir = g_light_directions[0].xyz;\n"
184 " float flength = g_light_directions[0].w;\n"
186 " float famt = 0.0;\n"
187 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
188 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
189 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
190 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
191 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
192 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
193 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
194 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
196 " // player shadow\n"
197 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.03 );\n"
198 " float player_shadow = max( 1.0-dist_to_player*1.7, 0.0 );\n"
199 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
201 " famt = max( player_shadow*0.6, famt );\n"
202 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
205 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
207 " float dist = pow(fdist*0.0008,1.2);\n"
208 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
213 "const float k_motion_lerp_amount = 0.01;\n"
217 "layout (location = 1) out vec2 oMotionVec;\n"
219 "in vec3 aMotionVec0;\n"
220 "in vec3 aMotionVec1;\n"
222 "void compute_motion_vectors()\n"
224 " // Write motion vectors\n"
225 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
226 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
228 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
233 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
234 " vec4 beneath, vec4 above )\n"
236 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
238 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
240 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
241 " vec3 specdir = reflect( -lightdir, vnorm );\n"
242 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
245 " float depthblend = pow( beneath.a,0.8 );\n"
248 " vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
249 " //vsurface += spec;\n"
251 " return vec4( vsurface,depthblend );\n"
256 " compute_motion_vectors();\n"
258 " // Create texture coords\n"
259 " vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
261 " // Surface colour composite\n"
262 " float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
264 " vec2 world_coord = aCo.xz * 0.008;\n"
265 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
266 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
267 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
269 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
270 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
273 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
274 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
277 " vec3 halfview = -normalize( aCo-uCamera );\n"
279 " // Sample textures\n"
280 " vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
281 " vec4 beneath = texture( uTexBack, ssuv );\n"
284 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
287 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
288 " vsurface.a -= fdist;\n"
289 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
294 static GLuint _uniform_water_uMdl
;
295 static GLuint _uniform_water_uPv
;
296 static GLuint _uniform_water_uPvmPrev
;
297 static GLuint _uniform_water_uTexMain
;
298 static GLuint _uniform_water_uTexDudv
;
299 static GLuint _uniform_water_uTexBack
;
300 static GLuint _uniform_water_uInvRes
;
301 static GLuint _uniform_water_uTime
;
302 static GLuint _uniform_water_uCamera
;
303 static GLuint _uniform_water_uSurfaceY
;
304 static GLuint _uniform_water_uBoard0
;
305 static GLuint _uniform_water_uBoard1
;
306 static GLuint _uniform_water_uShoreColour
;
307 static GLuint _uniform_water_uOceanColour
;
308 static GLuint _uniform_water_g_world_depth
;
309 static void shader_water_uMdl(m4x3f m
){
310 glUniformMatrix4x3fv(_uniform_water_uMdl
,1,GL_FALSE
,(float*)m
);
312 static void shader_water_uPv(m4x4f m
){
313 glUniformMatrix4fv(_uniform_water_uPv
,1,GL_FALSE
,(float*)m
);
315 static void shader_water_uPvmPrev(m4x4f m
){
316 glUniformMatrix4fv(_uniform_water_uPvmPrev
,1,GL_FALSE
,(float*)m
);
318 static void shader_water_uTexMain(int i
){
319 glUniform1i(_uniform_water_uTexMain
,i
);
321 static void shader_water_uTexDudv(int i
){
322 glUniform1i(_uniform_water_uTexDudv
,i
);
324 static void shader_water_uTexBack(int i
){
325 glUniform1i(_uniform_water_uTexBack
,i
);
327 static void shader_water_uInvRes(v2f v
){
328 glUniform2fv(_uniform_water_uInvRes
,1,v
);
330 static void shader_water_uTime(float f
){
331 glUniform1f(_uniform_water_uTime
,f
);
333 static void shader_water_uCamera(v3f v
){
334 glUniform3fv(_uniform_water_uCamera
,1,v
);
336 static void shader_water_uSurfaceY(float f
){
337 glUniform1f(_uniform_water_uSurfaceY
,f
);
339 static void shader_water_uBoard0(v3f v
){
340 glUniform3fv(_uniform_water_uBoard0
,1,v
);
342 static void shader_water_uBoard1(v3f v
){
343 glUniform3fv(_uniform_water_uBoard1
,1,v
);
345 static void shader_water_uShoreColour(v3f v
){
346 glUniform3fv(_uniform_water_uShoreColour
,1,v
);
348 static void shader_water_uOceanColour(v3f v
){
349 glUniform3fv(_uniform_water_uOceanColour
,1,v
);
351 static void shader_water_g_world_depth(int i
){
352 glUniform1i(_uniform_water_g_world_depth
,i
);
354 static void shader_water_register(void){
355 vg_shader_register( &_shader_water
);
357 static void shader_water_use(void){ glUseProgram(_shader_water
.id
); }
358 static void shader_water_link(void){
359 _uniform_water_uMdl
= glGetUniformLocation( _shader_water
.id
, "uMdl" );
360 _uniform_water_uPv
= glGetUniformLocation( _shader_water
.id
, "uPv" );
361 _uniform_water_uPvmPrev
= glGetUniformLocation( _shader_water
.id
, "uPvmPrev" );
362 _uniform_water_uTexMain
= glGetUniformLocation( _shader_water
.id
, "uTexMain" );
363 _uniform_water_uTexDudv
= glGetUniformLocation( _shader_water
.id
, "uTexDudv" );
364 _uniform_water_uTexBack
= glGetUniformLocation( _shader_water
.id
, "uTexBack" );
365 _uniform_water_uInvRes
= glGetUniformLocation( _shader_water
.id
, "uInvRes" );
366 _uniform_water_uTime
= glGetUniformLocation( _shader_water
.id
, "uTime" );
367 _uniform_water_uCamera
= glGetUniformLocation( _shader_water
.id
, "uCamera" );
368 _uniform_water_uSurfaceY
= glGetUniformLocation( _shader_water
.id
, "uSurfaceY" );
369 _uniform_water_uBoard0
= glGetUniformLocation( _shader_water
.id
, "uBoard0" );
370 _uniform_water_uBoard1
= glGetUniformLocation( _shader_water
.id
, "uBoard1" );
371 _uniform_water_uShoreColour
= glGetUniformLocation( _shader_water
.id
, "uShoreColour" );
372 _uniform_water_uOceanColour
= glGetUniformLocation( _shader_water
.id
, "uOceanColour" );
373 _uniform_water_g_world_depth
= glGetUniformLocation( _shader_water
.id
, "g_world_depth" );
375 #endif /* SHADER_water_H */