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
= {
7 .link
= shader_water_fast_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.05;\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
29 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
31 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
32 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
37 "uniform mat4x3 uMdl;\n"
39 "uniform mat4 uPvmPrev;\n"
45 "out vec3 aWorldCo;\n"
49 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
50 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
51 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
53 " vs_motion_out( vproj0, vproj1 );\n"
55 " gl_Position = vproj0;\n"
56 " aWorldCo = world_pos0;\n"
57 " aColour = a_colour;\n"
59 " aNorm = mat3(uMdl) * a_norm;\n"
66 "uniform sampler2D uTexDudv;\n"
68 "uniform float uTime;\n"
69 "uniform vec3 uCamera;\n"
70 "uniform float uSurfaceY;\n"
72 "uniform vec3 uShoreColour;\n"
73 "uniform vec3 uOceanColour;\n"
82 "layout (location = 0) out vec4 oColour;\n"
84 "layout (std140) uniform ub_world_lighting\n"
86 " vec4 g_light_colours[3];\n"
87 " vec4 g_light_directions[3];\n"
88 " vec4 g_ambient_colour;\n"
90 " vec4 g_water_plane;\n"
91 " vec4 g_depth_bounds;\n"
92 " float g_water_fog;\n"
93 " int g_light_count;\n"
94 " int g_light_preview;\n"
95 " int g_shadow_samples;\n"
98 "uniform sampler2D g_world_depth;\n"
100 "// Standard diffuse + spec models\n"
101 "// ==============================\n"
103 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
105 " vec3 vtotal = g_ambient_colour.rgb;\n"
107 " for( int i=0; i<g_light_count; i++ )\n"
109 " vec3 vcolour = g_light_colours[i].rgb;\n"
110 " vec3 vdir = g_light_directions[i].xyz;\n"
112 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
113 " vtotal += vcolour*flight;\n"
116 " return vfrag * vtotal;\n"
119 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
121 " vec3 vcolour = g_light_colours[0].rgb;\n"
122 " vec3 vdir = g_light_directions[0].xyz;\n"
124 " vec3 specdir = reflect( -vdir, wnormal );\n"
125 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
126 " return vfrag + vcolour*spec*fintensity;\n"
129 "float world_depth_sample( vec3 pos )\n"
131 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
132 " return texture( g_world_depth, depth_coord ).r;\n"
135 "float shadow_sample( vec3 vdir )\n"
137 " vec3 sample_pos = aWorldCo + vdir;\n"
138 " float height_sample = world_depth_sample( sample_pos );\n"
140 " float fdelta = height_sample - sample_pos.y;\n"
141 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
144 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
146 " float faccum = 0.0;\n"
147 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
148 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
149 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
150 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
151 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
152 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
153 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
154 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
155 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
158 "vec3 do_light_shadowing( vec3 vfrag )\n"
160 " if( g_shadow_samples == 0 )\n"
165 " float fspread = g_light_colours[0].w;\n"
166 " vec3 vdir = g_light_directions[0].xyz;\n"
167 " float flength = g_light_directions[0].w;\n"
169 " float famt = 0.0;\n"
170 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
172 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
173 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
174 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
175 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
176 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
177 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
178 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
181 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
183 " float dist = pow(fdist*0.0008,1.2);\n"
184 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
189 "const float k_motion_lerp_amount = 0.05;\n"
193 "layout (location = 1) out vec2 oMotionVec;\n"
195 "in vec3 aMotionVec0;\n"
196 "in vec3 aMotionVec1;\n"
198 "void compute_motion_vectors()\n"
200 " // Write motion vectors\n"
201 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
202 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
204 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
209 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )\n"
211 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
213 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
215 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
216 " vec3 specdir = reflect( -lightdir, vnorm );\n"
217 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
219 " return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );\n"
224 " compute_motion_vectors();\n"
226 " // Surface colour composite\n"
227 " float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
229 " vec2 world_coord = aCo.xz * 0.008;\n"
230 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
231 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
232 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
234 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
235 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
238 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
239 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
242 " vec3 halfview = -normalize( aCo-uCamera );\n"
245 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
248 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );\n"
249 " vsurface.a -= fdist;\n"
250 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
255 static GLuint _uniform_water_fast_uMdl
;
256 static GLuint _uniform_water_fast_uPv
;
257 static GLuint _uniform_water_fast_uPvmPrev
;
258 static GLuint _uniform_water_fast_uTexDudv
;
259 static GLuint _uniform_water_fast_uTime
;
260 static GLuint _uniform_water_fast_uCamera
;
261 static GLuint _uniform_water_fast_uSurfaceY
;
262 static GLuint _uniform_water_fast_uShoreColour
;
263 static GLuint _uniform_water_fast_uOceanColour
;
264 static GLuint _uniform_water_fast_g_world_depth
;
265 static void shader_water_fast_uMdl(m4x3f m
){
266 glUniformMatrix4x3fv(_uniform_water_fast_uMdl
,1,GL_FALSE
,(float*)m
);
268 static void shader_water_fast_uPv(m4x4f m
){
269 glUniformMatrix4fv(_uniform_water_fast_uPv
,1,GL_FALSE
,(float*)m
);
271 static void shader_water_fast_uPvmPrev(m4x4f m
){
272 glUniformMatrix4fv(_uniform_water_fast_uPvmPrev
,1,GL_FALSE
,(float*)m
);
274 static void shader_water_fast_uTexDudv(int i
){
275 glUniform1i(_uniform_water_fast_uTexDudv
,i
);
277 static void shader_water_fast_uTime(float f
){
278 glUniform1f(_uniform_water_fast_uTime
,f
);
280 static void shader_water_fast_uCamera(v3f v
){
281 glUniform3fv(_uniform_water_fast_uCamera
,1,v
);
283 static void shader_water_fast_uSurfaceY(float f
){
284 glUniform1f(_uniform_water_fast_uSurfaceY
,f
);
286 static void shader_water_fast_uShoreColour(v3f v
){
287 glUniform3fv(_uniform_water_fast_uShoreColour
,1,v
);
289 static void shader_water_fast_uOceanColour(v3f v
){
290 glUniform3fv(_uniform_water_fast_uOceanColour
,1,v
);
292 static void shader_water_fast_g_world_depth(int i
){
293 glUniform1i(_uniform_water_fast_g_world_depth
,i
);
295 static void shader_water_fast_register(void){
296 vg_shader_register( &_shader_water_fast
);
298 static void shader_water_fast_use(void){ glUseProgram(_shader_water_fast
.id
); }
299 static void shader_water_fast_link(void){
300 _uniform_water_fast_uMdl
= glGetUniformLocation( _shader_water_fast
.id
, "uMdl" );
301 _uniform_water_fast_uPv
= glGetUniformLocation( _shader_water_fast
.id
, "uPv" );
302 _uniform_water_fast_uPvmPrev
= glGetUniformLocation( _shader_water_fast
.id
, "uPvmPrev" );
303 _uniform_water_fast_uTexDudv
= glGetUniformLocation( _shader_water_fast
.id
, "uTexDudv" );
304 _uniform_water_fast_uTime
= glGetUniformLocation( _shader_water_fast
.id
, "uTime" );
305 _uniform_water_fast_uCamera
= glGetUniformLocation( _shader_water_fast
.id
, "uCamera" );
306 _uniform_water_fast_uSurfaceY
= glGetUniformLocation( _shader_water_fast
.id
, "uSurfaceY" );
307 _uniform_water_fast_uShoreColour
= glGetUniformLocation( _shader_water_fast
.id
, "uShoreColour" );
308 _uniform_water_fast_uOceanColour
= glGetUniformLocation( _shader_water_fast
.id
, "uOceanColour" );
309 _uniform_water_fast_g_world_depth
= glGetUniformLocation( _shader_water_fast
.id
, "g_world_depth" );
311 #endif /* SHADER_water_fast_H */