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