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