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