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