8cc3f50a8406854b858f2d78d22842c4ad601f28
[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 "\n"
20 "uniform mat4x3 uMdl;\n"
21 "uniform mat4 uPv;\n"
22 "\n"
23 "out vec4 aColour;\n"
24 "out vec2 aUv;\n"
25 "out vec3 aNorm;\n"
26 "out vec3 aCo;\n"
27 "out vec3 aWorldCo;\n"
28 "\n"
29 "void main()\n"
30 "{\n"
31 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
32 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
33 " aColour = a_colour;\n"
34 " aUv = a_uv;\n"
35 " aNorm = mat3(uMdl) * a_norm;\n"
36 " aCo = a_co;\n"
37 " aWorldCo = world_pos;\n"
38 "}\n"
39 ""},
40 .fs =
41 {
42 .static_src =
43 "out vec4 FragColor;\n"
44 "\n"
45 "uniform sampler2D uTexDudv;\n"
46 "\n"
47 "uniform float uTime;\n"
48 "uniform vec3 uCamera;\n"
49 "uniform float uSurfaceY;\n"
50 "\n"
51 "uniform vec3 uShoreColour;\n"
52 "uniform vec3 uOceanColour;\n"
53 "\n"
54 "in vec4 aColour;\n"
55 "in vec2 aUv;\n"
56 "in vec3 aNorm;\n"
57 "in vec3 aCo;\n"
58 "in vec3 aWorldCo;\n"
59 "\n"
60 "#line 1 1 \n"
61 "layout (std140) uniform ub_world_lighting\n"
62 "{\n"
63 " vec4 g_light_colours[3];\n"
64 " vec4 g_light_directions[3];\n"
65 " vec4 g_ambient_colour;\n"
66 "\n"
67 " vec4 g_water_plane;\n"
68 " vec4 g_depth_bounds;\n"
69 " float g_water_fog;\n"
70 " int g_light_count;\n"
71 " int g_light_preview;\n"
72 " int g_shadow_samples;\n"
73 "};\n"
74 "\n"
75 "uniform sampler2D g_world_depth;\n"
76 "\n"
77 "// Standard diffuse + spec models\n"
78 "// ==============================\n"
79 "\n"
80 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
81 "{\n"
82 " vec3 vtotal = g_ambient_colour.rgb;\n"
83 "\n"
84 " for( int i=0; i<g_light_count; i++ )\n"
85 " {\n"
86 " vec3 vcolour = g_light_colours[i].rgb;\n"
87 " vec3 vdir = g_light_directions[i].xyz;\n"
88 "\n"
89 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
90 " vtotal += vcolour*flight;\n"
91 " }\n"
92 "\n"
93 " return vfrag * vtotal;\n"
94 "}\n"
95 "\n"
96 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
97 "{\n"
98 " vec3 vcolour = g_light_colours[0].rgb;\n"
99 " vec3 vdir = g_light_directions[0].xyz;\n"
100 "\n"
101 " vec3 specdir = reflect( -vdir, wnormal );\n"
102 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
103 " return vfrag + vcolour*spec*fintensity;\n"
104 "}\n"
105 "\n"
106 "float world_depth_sample( vec3 pos )\n"
107 "{\n"
108 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
109 " return texture( g_world_depth, depth_coord ).r;\n"
110 "}\n"
111 "\n"
112 "float shadow_sample( vec3 vdir )\n"
113 "{\n"
114 " vec3 sample_pos = aWorldCo + vdir;\n"
115 " float height_sample = world_depth_sample( sample_pos );\n"
116 "\n"
117 " float fdelta = height_sample - sample_pos.y;\n"
118 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
119 "}\n"
120 "\n"
121 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
122 "{\n"
123 " float faccum = 0.0;\n"
124 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
125 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
126 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
127 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
128 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
129 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
130 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
131 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
132 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
133 "}\n"
134 "\n"
135 "vec3 do_light_shadowing( vec3 vfrag )\n"
136 "{\n"
137 " if( g_shadow_samples == 0 )\n"
138 " {\n"
139 " return vfrag;\n"
140 " }\n"
141 "\n"
142 " float fspread = g_light_colours[0].w;\n"
143 " vec3 vdir = g_light_directions[0].xyz;\n"
144 " float flength = g_light_directions[0].w;\n"
145 "\n"
146 " float famt = 0.0;\n"
147 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
148 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
149 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
150 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
151 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
152 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
153 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
154 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
155 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
156 "}\n"
157 "\n"
158 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
159 "{\n"
160 " float dist = pow(fdist*0.0008,1.2);\n"
161 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
162 "}\n"
163 "\n"
164 "#line 19 0 \n"
165 "\n"
166 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue )\n"
167 "{\n"
168 " vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue);\n"
169 "\n"
170 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
171 "\n"
172 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
173 " vec3 specdir = reflect( -lightdir, vnorm );\n"
174 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
175 " \n"
176 " return vec4( surface_tint + spec, max(min(depthvalue*4.0, 1.0),0.0) );\n"
177 "}\n"
178 "\n"
179 "void main()\n"
180 "{\n"
181 " // Surface colour composite\n"
182 " float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
183 "\n"
184 " vec2 world_coord = aCo.xz * 0.008;\n"
185 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
186 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
187 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
188 "\n"
189 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
190 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
191 " \n"
192 " // Foam\n"
193 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
194 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
195 "\n"
196 " // Lighting\n"
197 " vec3 halfview = -normalize( aCo-uCamera );\n"
198 "\n"
199 " // Fog\n"
200 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
201 "\n"
202 " // Composite\n"
203 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue );\n"
204 " vsurface.a -= fdist;\n"
205 " FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
206 "}\n"
207 ""},
208 };
209
210 static GLuint _uniform_water_fast_uMdl;
211 static GLuint _uniform_water_fast_uPv;
212 static GLuint _uniform_water_fast_uTexDudv;
213 static GLuint _uniform_water_fast_uTime;
214 static GLuint _uniform_water_fast_uCamera;
215 static GLuint _uniform_water_fast_uSurfaceY;
216 static GLuint _uniform_water_fast_uShoreColour;
217 static GLuint _uniform_water_fast_uOceanColour;
218 static GLuint _uniform_water_fast_g_world_depth;
219 static void shader_water_fast_uMdl(m4x3f m){
220 glUniformMatrix4x3fv(_uniform_water_fast_uMdl,1,GL_FALSE,(float*)m);
221 }
222 static void shader_water_fast_uPv(m4x4f m){
223 glUniformMatrix4fv(_uniform_water_fast_uPv,1,GL_FALSE,(float*)m);
224 }
225 static void shader_water_fast_uTexDudv(int i){
226 glUniform1i(_uniform_water_fast_uTexDudv,i);
227 }
228 static void shader_water_fast_uTime(float f){
229 glUniform1f(_uniform_water_fast_uTime,f);
230 }
231 static void shader_water_fast_uCamera(v3f v){
232 glUniform3fv(_uniform_water_fast_uCamera,1,v);
233 }
234 static void shader_water_fast_uSurfaceY(float f){
235 glUniform1f(_uniform_water_fast_uSurfaceY,f);
236 }
237 static void shader_water_fast_uShoreColour(v3f v){
238 glUniform3fv(_uniform_water_fast_uShoreColour,1,v);
239 }
240 static void shader_water_fast_uOceanColour(v3f v){
241 glUniform3fv(_uniform_water_fast_uOceanColour,1,v);
242 }
243 static void shader_water_fast_g_world_depth(int i){
244 glUniform1i(_uniform_water_fast_g_world_depth,i);
245 }
246 static void shader_water_fast_register(void){
247 vg_shader_register( &_shader_water_fast );
248 }
249 static void shader_water_fast_use(void){ glUseProgram(_shader_water_fast.id); }
250 static void shader_water_fast_link(void){
251 _uniform_water_fast_uMdl = glGetUniformLocation( _shader_water_fast.id, "uMdl" );
252 _uniform_water_fast_uPv = glGetUniformLocation( _shader_water_fast.id, "uPv" );
253 _uniform_water_fast_uTexDudv = glGetUniformLocation( _shader_water_fast.id, "uTexDudv" );
254 _uniform_water_fast_uTime = glGetUniformLocation( _shader_water_fast.id, "uTime" );
255 _uniform_water_fast_uCamera = glGetUniformLocation( _shader_water_fast.id, "uCamera" );
256 _uniform_water_fast_uSurfaceY = glGetUniformLocation( _shader_water_fast.id, "uSurfaceY" );
257 _uniform_water_fast_uShoreColour = glGetUniformLocation( _shader_water_fast.id, "uShoreColour" );
258 _uniform_water_fast_uOceanColour = glGetUniformLocation( _shader_water_fast.id, "uOceanColour" );
259 _uniform_water_fast_g_world_depth = glGetUniformLocation( _shader_water_fast.id, "g_world_depth" );
260 }
261 #endif /* SHADER_water_fast_H */