93dcba858e07048a0ecdba1a9247ffd05cceb3c3
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / water.h
1 #ifndef SHADER_water_H
2 #define SHADER_water_H
3 static void shader_water_link(void);
4 static void shader_water_register(void);
5 static struct vg_shader _shader_water = {
6 .name = "water",
7 .link = shader_water_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 uTexMain;\n"
69 "uniform sampler2D uTexDudv;\n"
70 "uniform sampler2D uTexBack;\n"
71 "\n"
72 "uniform vec2 uInvRes;\n"
73 "uniform float uTime;\n"
74 "uniform vec3 uCamera;\n"
75 "uniform float uSurfaceY;\n"
76 "\n"
77 "uniform vec3 uShoreColour;\n"
78 "uniform vec3 uOceanColour;\n"
79 "\n"
80 "in vec4 aColour;\n"
81 "in vec2 aUv;\n"
82 "in vec3 aNorm;\n"
83 "in vec3 aCo;\n"
84 "in vec3 aWorldCo;\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 "\n"
103 "uniform sampler2D g_world_depth;\n"
104 "\n"
105 "// Standard diffuse + spec models\n"
106 "// ==============================\n"
107 "\n"
108 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
109 "{\n"
110 " vec3 vtotal = g_ambient_colour.rgb;\n"
111 "\n"
112 " for( int i=0; i<g_light_count; i++ )\n"
113 " {\n"
114 " vec3 vcolour = g_light_colours[i].rgb;\n"
115 " vec3 vdir = g_light_directions[i].xyz;\n"
116 "\n"
117 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
118 " vtotal += vcolour*flight;\n"
119 " }\n"
120 "\n"
121 " return vfrag * vtotal;\n"
122 "}\n"
123 "\n"
124 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
125 "{\n"
126 " vec3 vcolour = g_light_colours[0].rgb;\n"
127 " vec3 vdir = g_light_directions[0].xyz;\n"
128 "\n"
129 " vec3 specdir = reflect( -vdir, wnormal );\n"
130 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
131 " return vfrag + vcolour*spec*fintensity;\n"
132 "}\n"
133 "\n"
134 "float world_depth_sample( vec3 pos )\n"
135 "{\n"
136 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
137 " return texture( g_world_depth, depth_coord ).r;\n"
138 "}\n"
139 "\n"
140 "float shadow_sample( vec3 vdir )\n"
141 "{\n"
142 " vec3 sample_pos = aWorldCo + vdir;\n"
143 " float height_sample = world_depth_sample( sample_pos );\n"
144 "\n"
145 " float fdelta = height_sample - sample_pos.y;\n"
146 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
147 "}\n"
148 "\n"
149 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
150 "{\n"
151 " float faccum = 0.0;\n"
152 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
153 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
154 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
155 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
156 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
157 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
158 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
159 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
160 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
161 "}\n"
162 "\n"
163 "vec3 do_light_shadowing( vec3 vfrag )\n"
164 "{\n"
165 " if( g_shadow_samples == 0 )\n"
166 " {\n"
167 " return vfrag;\n"
168 " }\n"
169 "\n"
170 " float fspread = g_light_colours[0].w;\n"
171 " vec3 vdir = g_light_directions[0].xyz;\n"
172 " float flength = g_light_directions[0].w;\n"
173 "\n"
174 " float famt = 0.0;\n"
175 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
176 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
177 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
178 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
179 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
180 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
181 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
182 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
183 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
184 "}\n"
185 "\n"
186 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
187 "{\n"
188 " float dist = pow(fdist*0.0008,1.2);\n"
189 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
190 "}\n"
191 "\n"
192 "#line 20 0 \n"
193 "#line 1 2 \n"
194 "const float k_motion_lerp_amount = 0.01;\n"
195 "\n"
196 "#line 2 0 \n"
197 "\n"
198 "layout (location = 1) out vec2 oMotionVec;\n"
199 "\n"
200 "in vec3 aMotionVec0;\n"
201 "in vec3 aMotionVec1;\n"
202 "\n"
203 "void compute_motion_vectors()\n"
204 "{\n"
205 " // Write motion vectors\n"
206 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
207 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
208 "\n"
209 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
210 "}\n"
211 "\n"
212 "#line 21 0 \n"
213 "\n"
214 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
215 " vec4 beneath, vec4 above )\n"
216 "{\n"
217 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
218 "\n"
219 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
220 "\n"
221 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
222 " vec3 specdir = reflect( -lightdir, vnorm );\n"
223 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
224 " \n"
225 " // Depth \n"
226 " float depthblend = pow( beneath.a,0.8 );\n"
227 "\n"
228 " // Composite\n"
229 " vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
230 " //vsurface += spec;\n"
231 "\n"
232 " return vec4( vsurface,depthblend );\n"
233 "}\n"
234 "\n"
235 "void main()\n"
236 "{\n"
237 " compute_motion_vectors();\n"
238 "\n"
239 " // Create texture coords\n"
240 " vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
241 " \n"
242 " // Surface colour composite\n"
243 " float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
244 "\n"
245 " vec2 world_coord = aCo.xz * 0.008;\n"
246 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
247 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
248 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
249 "\n"
250 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
251 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
252 " \n"
253 " // Foam\n"
254 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
255 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
256 "\n"
257 " // Lighting\n"
258 " vec3 halfview = -normalize( aCo-uCamera );\n"
259 "\n"
260 " // Sample textures\n"
261 " vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
262 " vec4 beneath = texture( uTexBack, ssuv );\n"
263 "\n"
264 " // Fog\n"
265 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
266 "\n"
267 " // Composite\n"
268 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
269 " vsurface.a -= fdist;\n"
270 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
271 "}\n"
272 ""},
273 };
274
275 static GLuint _uniform_water_uMdl;
276 static GLuint _uniform_water_uPv;
277 static GLuint _uniform_water_uPvmPrev;
278 static GLuint _uniform_water_uTexMain;
279 static GLuint _uniform_water_uTexDudv;
280 static GLuint _uniform_water_uTexBack;
281 static GLuint _uniform_water_uInvRes;
282 static GLuint _uniform_water_uTime;
283 static GLuint _uniform_water_uCamera;
284 static GLuint _uniform_water_uSurfaceY;
285 static GLuint _uniform_water_uShoreColour;
286 static GLuint _uniform_water_uOceanColour;
287 static GLuint _uniform_water_g_world_depth;
288 static void shader_water_uMdl(m4x3f m){
289 glUniformMatrix4x3fv(_uniform_water_uMdl,1,GL_FALSE,(float*)m);
290 }
291 static void shader_water_uPv(m4x4f m){
292 glUniformMatrix4fv(_uniform_water_uPv,1,GL_FALSE,(float*)m);
293 }
294 static void shader_water_uPvmPrev(m4x4f m){
295 glUniformMatrix4fv(_uniform_water_uPvmPrev,1,GL_FALSE,(float*)m);
296 }
297 static void shader_water_uTexMain(int i){
298 glUniform1i(_uniform_water_uTexMain,i);
299 }
300 static void shader_water_uTexDudv(int i){
301 glUniform1i(_uniform_water_uTexDudv,i);
302 }
303 static void shader_water_uTexBack(int i){
304 glUniform1i(_uniform_water_uTexBack,i);
305 }
306 static void shader_water_uInvRes(v2f v){
307 glUniform2fv(_uniform_water_uInvRes,1,v);
308 }
309 static void shader_water_uTime(float f){
310 glUniform1f(_uniform_water_uTime,f);
311 }
312 static void shader_water_uCamera(v3f v){
313 glUniform3fv(_uniform_water_uCamera,1,v);
314 }
315 static void shader_water_uSurfaceY(float f){
316 glUniform1f(_uniform_water_uSurfaceY,f);
317 }
318 static void shader_water_uShoreColour(v3f v){
319 glUniform3fv(_uniform_water_uShoreColour,1,v);
320 }
321 static void shader_water_uOceanColour(v3f v){
322 glUniform3fv(_uniform_water_uOceanColour,1,v);
323 }
324 static void shader_water_g_world_depth(int i){
325 glUniform1i(_uniform_water_g_world_depth,i);
326 }
327 static void shader_water_register(void){
328 vg_shader_register( &_shader_water );
329 }
330 static void shader_water_use(void){ glUseProgram(_shader_water.id); }
331 static void shader_water_link(void){
332 _uniform_water_uMdl = glGetUniformLocation( _shader_water.id, "uMdl" );
333 _uniform_water_uPv = glGetUniformLocation( _shader_water.id, "uPv" );
334 _uniform_water_uPvmPrev = glGetUniformLocation( _shader_water.id, "uPvmPrev" );
335 _uniform_water_uTexMain = glGetUniformLocation( _shader_water.id, "uTexMain" );
336 _uniform_water_uTexDudv = glGetUniformLocation( _shader_water.id, "uTexDudv" );
337 _uniform_water_uTexBack = glGetUniformLocation( _shader_water.id, "uTexBack" );
338 _uniform_water_uInvRes = glGetUniformLocation( _shader_water.id, "uInvRes" );
339 _uniform_water_uTime = glGetUniformLocation( _shader_water.id, "uTime" );
340 _uniform_water_uCamera = glGetUniformLocation( _shader_water.id, "uCamera" );
341 _uniform_water_uSurfaceY = glGetUniformLocation( _shader_water.id, "uSurfaceY" );
342 _uniform_water_uShoreColour = glGetUniformLocation( _shader_water.id, "uShoreColour" );
343 _uniform_water_uOceanColour = glGetUniformLocation( _shader_water.id, "uOceanColour" );
344 _uniform_water_g_world_depth = glGetUniformLocation( _shader_water.id, "g_world_depth" );
345 }
346 #endif /* SHADER_water_H */