add motion vectors to all shaders
[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 "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 uTexDudv;\n"
61 "\n"
62 "uniform float uTime;\n"
63 "uniform vec3 uCamera;\n"
64 "uniform float uSurfaceY;\n"
65 "\n"
66 "uniform vec3 uShoreColour;\n"
67 "uniform vec3 uOceanColour;\n"
68 "\n"
69 "in vec4 aColour;\n"
70 "in vec2 aUv;\n"
71 "in vec3 aNorm;\n"
72 "in vec3 aCo;\n"
73 "in vec3 aWorldCo;\n"
74 "\n"
75 "#line 1 1 \n"
76 "layout (location = 0) out vec4 oColour;\n"
77 "\n"
78 "layout (std140) uniform ub_world_lighting\n"
79 "{\n"
80 " vec4 g_light_colours[3];\n"
81 " vec4 g_light_directions[3];\n"
82 " vec4 g_ambient_colour;\n"
83 "\n"
84 " vec4 g_water_plane;\n"
85 " vec4 g_depth_bounds;\n"
86 " float g_water_fog;\n"
87 " int g_light_count;\n"
88 " int g_light_preview;\n"
89 " int g_shadow_samples;\n"
90 "};\n"
91 "\n"
92 "uniform sampler2D g_world_depth;\n"
93 "\n"
94 "// Standard diffuse + spec models\n"
95 "// ==============================\n"
96 "\n"
97 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
98 "{\n"
99 " vec3 vtotal = g_ambient_colour.rgb;\n"
100 "\n"
101 " for( int i=0; i<g_light_count; i++ )\n"
102 " {\n"
103 " vec3 vcolour = g_light_colours[i].rgb;\n"
104 " vec3 vdir = g_light_directions[i].xyz;\n"
105 "\n"
106 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
107 " vtotal += vcolour*flight;\n"
108 " }\n"
109 "\n"
110 " return vfrag * vtotal;\n"
111 "}\n"
112 "\n"
113 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
114 "{\n"
115 " vec3 vcolour = g_light_colours[0].rgb;\n"
116 " vec3 vdir = g_light_directions[0].xyz;\n"
117 "\n"
118 " vec3 specdir = reflect( -vdir, wnormal );\n"
119 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
120 " return vfrag + vcolour*spec*fintensity;\n"
121 "}\n"
122 "\n"
123 "float world_depth_sample( vec3 pos )\n"
124 "{\n"
125 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
126 " return texture( g_world_depth, depth_coord ).r;\n"
127 "}\n"
128 "\n"
129 "float shadow_sample( vec3 vdir )\n"
130 "{\n"
131 " vec3 sample_pos = aWorldCo + vdir;\n"
132 " float height_sample = world_depth_sample( sample_pos );\n"
133 "\n"
134 " float fdelta = height_sample - sample_pos.y;\n"
135 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
136 "}\n"
137 "\n"
138 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
139 "{\n"
140 " float faccum = 0.0;\n"
141 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
142 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
143 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
144 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
145 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
146 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
147 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
148 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
149 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
150 "}\n"
151 "\n"
152 "vec3 do_light_shadowing( vec3 vfrag )\n"
153 "{\n"
154 " if( g_shadow_samples == 0 )\n"
155 " {\n"
156 " return vfrag;\n"
157 " }\n"
158 "\n"
159 " float fspread = g_light_colours[0].w;\n"
160 " vec3 vdir = g_light_directions[0].xyz;\n"
161 " float flength = g_light_directions[0].w;\n"
162 "\n"
163 " float famt = 0.0;\n"
164 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
165 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
167 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
168 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
172 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
173 "}\n"
174 "\n"
175 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
176 "{\n"
177 " float dist = pow(fdist*0.0008,1.2);\n"
178 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
179 "}\n"
180 "\n"
181 "#line 17 0 \n"
182 "#line 1 2 \n"
183 "layout (location = 1) out vec2 oMotionVec;\n"
184 "\n"
185 "in vec3 aMotionVec0;\n"
186 "in vec3 aMotionVec1;\n"
187 "\n"
188 "void compute_motion_vectors()\n"
189 "{\n"
190 " // Write motion vectors\n"
191 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
192 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
193 " oMotionVec = vmotion1-vmotion0;\n"
194 "}\n"
195 "\n"
196 "#line 18 0 \n"
197 "\n"
198 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )\n"
199 "{\n"
200 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
201 "\n"
202 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
203 "\n"
204 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
205 " vec3 specdir = reflect( -lightdir, vnorm );\n"
206 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
207 " \n"
208 " return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );\n"
209 "}\n"
210 "\n"
211 "void main()\n"
212 "{\n"
213 " compute_motion_vectors();\n"
214 "\n"
215 " // Surface colour composite\n"
216 " float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
217 "\n"
218 " vec2 world_coord = aCo.xz * 0.008;\n"
219 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
220 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
221 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
222 "\n"
223 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
224 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
225 " \n"
226 " // Foam\n"
227 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
228 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
229 "\n"
230 " // Lighting\n"
231 " vec3 halfview = -normalize( aCo-uCamera );\n"
232 "\n"
233 " // Fog\n"
234 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
235 "\n"
236 " // Composite\n"
237 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );\n"
238 " vsurface.a -= fdist;\n"
239 " oColour = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
240 "}\n"
241 ""},
242 };
243
244 static GLuint _uniform_water_fast_uMdl;
245 static GLuint _uniform_water_fast_uPv;
246 static GLuint _uniform_water_fast_uPvmPrev;
247 static GLuint _uniform_water_fast_uTexDudv;
248 static GLuint _uniform_water_fast_uTime;
249 static GLuint _uniform_water_fast_uCamera;
250 static GLuint _uniform_water_fast_uSurfaceY;
251 static GLuint _uniform_water_fast_uShoreColour;
252 static GLuint _uniform_water_fast_uOceanColour;
253 static GLuint _uniform_water_fast_g_world_depth;
254 static void shader_water_fast_uMdl(m4x3f m){
255 glUniformMatrix4x3fv(_uniform_water_fast_uMdl,1,GL_FALSE,(float*)m);
256 }
257 static void shader_water_fast_uPv(m4x4f m){
258 glUniformMatrix4fv(_uniform_water_fast_uPv,1,GL_FALSE,(float*)m);
259 }
260 static void shader_water_fast_uPvmPrev(m4x4f m){
261 glUniformMatrix4fv(_uniform_water_fast_uPvmPrev,1,GL_FALSE,(float*)m);
262 }
263 static void shader_water_fast_uTexDudv(int i){
264 glUniform1i(_uniform_water_fast_uTexDudv,i);
265 }
266 static void shader_water_fast_uTime(float f){
267 glUniform1f(_uniform_water_fast_uTime,f);
268 }
269 static void shader_water_fast_uCamera(v3f v){
270 glUniform3fv(_uniform_water_fast_uCamera,1,v);
271 }
272 static void shader_water_fast_uSurfaceY(float f){
273 glUniform1f(_uniform_water_fast_uSurfaceY,f);
274 }
275 static void shader_water_fast_uShoreColour(v3f v){
276 glUniform3fv(_uniform_water_fast_uShoreColour,1,v);
277 }
278 static void shader_water_fast_uOceanColour(v3f v){
279 glUniform3fv(_uniform_water_fast_uOceanColour,1,v);
280 }
281 static void shader_water_fast_g_world_depth(int i){
282 glUniform1i(_uniform_water_fast_g_world_depth,i);
283 }
284 static void shader_water_fast_register(void){
285 vg_shader_register( &_shader_water_fast );
286 }
287 static void shader_water_fast_use(void){ glUseProgram(_shader_water_fast.id); }
288 static void shader_water_fast_link(void){
289 _uniform_water_fast_uMdl = glGetUniformLocation( _shader_water_fast.id, "uMdl" );
290 _uniform_water_fast_uPv = glGetUniformLocation( _shader_water_fast.id, "uPv" );
291 _uniform_water_fast_uPvmPrev = glGetUniformLocation( _shader_water_fast.id, "uPvmPrev" );
292 _uniform_water_fast_uTexDudv = glGetUniformLocation( _shader_water_fast.id, "uTexDudv" );
293 _uniform_water_fast_uTime = glGetUniformLocation( _shader_water_fast.id, "uTime" );
294 _uniform_water_fast_uCamera = glGetUniformLocation( _shader_water_fast.id, "uCamera" );
295 _uniform_water_fast_uSurfaceY = glGetUniformLocation( _shader_water_fast.id, "uSurfaceY" );
296 _uniform_water_fast_uShoreColour = glGetUniformLocation( _shader_water_fast.id, "uShoreColour" );
297 _uniform_water_fast_uOceanColour = glGetUniformLocation( _shader_water_fast.id, "uOceanColour" );
298 _uniform_water_fast_g_world_depth = glGetUniformLocation( _shader_water_fast.id, "g_world_depth" );
299 }
300 #endif /* SHADER_water_fast_H */