3 static void shader_water_link(void);
4 static void shader_water_register(void);
5 static struct vg_shader _shader_water
= {
7 .link
= shader_water_link
,
10 .orig_file
= "../shaders/standard.vs",
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"
20 "uniform mat4x3 uMdl;\n"
29 " gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );\n"
30 " aColour = a_colour;\n"
32 " aNorm = mat3(uMdl) * a_norm;\n"
38 .orig_file
= "../shaders/water.fs",
40 "out vec4 FragColor;\n"
42 "uniform sampler2D uTexMain;\n"
43 "uniform sampler2D uTexDudv;\n"
44 "uniform sampler2D uTexBack;\n"
46 "uniform vec2 uInvRes;\n"
47 "uniform float uTime;\n"
48 "uniform vec3 uCamera;\n"
49 "uniform float uSurfaceY;\n"
57 "layout (std140) uniform ub_world_lighting\n"
59 " vec4 g_light_colours[3];\n"
60 " vec4 g_light_directions[3];\n"
61 " vec4 g_ambient_colour;\n"
63 " vec4 g_water_plane;\n"
64 " vec4 g_depth_bounds;\n"
65 " float g_water_fog;\n"
66 " int g_light_count;\n"
67 " int g_light_preview;\n"
70 "uniform sampler2D g_world_depth;\n"
72 "// Standard diffuse + spec models\n"
73 "// ==============================\n"
75 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
77 " vec3 vtotal = g_ambient_colour.rgb;\n"
79 " for( int i=0; i<g_light_count; i++ )\n"
81 " vec3 vcolour = g_light_colours[i].rgb;\n"
82 " vec3 vdir = g_light_directions[i].xyz;\n"
84 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
85 " vtotal += vcolour*flight;\n"
88 " return vfrag * vtotal;\n"
91 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
93 " vec3 vcolour = g_light_colours[0].rgb;\n"
94 " vec3 vdir = g_light_directions[0].xyz;\n"
96 " vec3 specdir = reflect( -vdir, wnormal );\n"
97 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
98 " return vfrag + vcolour*spec*fintensity;\n"
101 "float world_depth_sample( vec3 pos )\n"
103 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
104 " return texture( g_world_depth, depth_coord ).r;\n"
107 "float shadow_sample( vec3 vdir )\n"
109 " vec3 sample_pos = aCo + vdir;\n"
110 " float height_sample = world_depth_sample( sample_pos );\n"
112 " float fdelta = height_sample - sample_pos.y;\n"
113 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
116 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
118 " float faccum = 0.0;\n"
119 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
120 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
121 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
122 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
123 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
124 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
125 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
126 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
127 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
130 "vec3 do_light_shadowing( vec3 vfrag )\n"
132 " float fspread = g_light_colours[0].w;\n"
133 " vec3 vdir = g_light_directions[0].xyz;\n"
134 " float flength = g_light_directions[0].w;\n"
136 " float famt = 0.0;\n"
137 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
138 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
139 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
140 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
141 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
142 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
143 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
144 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
145 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
148 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
150 " float dist = pow(fdist*0.0008,1.2);\n"
151 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
156 "vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, \n"
157 " vec4 beneath, vec4 above )\n"
159 " vec3 colour_shore = vec3( 0.21, 0.6, 0.8 );\n"
160 " vec3 colour_ocean = vec3( 0.01, 0.1, 0.2 );\n"
161 " vec3 surface_tint = mix(colour_shore, colour_ocean, depthvalue);\n"
163 " float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0);\n"
165 " vec3 lightdir = vec3(0.95,0.0,-0.3);\n"
166 " vec3 specdir = reflect( -lightdir, vnorm );\n"
167 " float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3;\n"
170 " float depthblend = pow( beneath.a,0.8 );\n"
173 " vec3 vsurface = mix(surface_tint, above.rgb, ffresnel );\n"
174 " //vsurface += spec;\n"
176 " return vec4( vsurface,depthblend );\n"
181 " // Create texture coords\n"
182 " vec2 ssuv = gl_FragCoord.xy*uInvRes;\n"
184 " // Surface colour composite\n"
185 " float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 );\n"
187 " vec2 world_coord = aCo.xz * 0.008;\n"
188 " vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 );\n"
189 " vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5;\n"
190 " vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5;\n"
192 " vec3 surfnorm = dudva.rgb + dudvb.rgb;\n"
193 " surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1);\n"
196 " float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 );\n"
197 " fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0);\n"
200 " vec3 halfview = -normalize( aCo-uCamera );\n"
202 " // Sample textures\n"
203 " vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 );\n"
204 " vec4 beneath = texture( uTexBack, ssuv );\n"
207 " float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6);\n"
210 " vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above );\n"
211 " vsurface.a -= fdist;\n"
212 " FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband );\n"
217 static GLuint _uniform_water_uPv
;
218 static GLuint _uniform_water_uMdl
;
219 static GLuint _uniform_water_uTexMain
;
220 static GLuint _uniform_water_uTexDudv
;
221 static GLuint _uniform_water_uTexBack
;
222 static GLuint _uniform_water_uInvRes
;
223 static GLuint _uniform_water_uTime
;
224 static GLuint _uniform_water_uCamera
;
225 static GLuint _uniform_water_uSurfaceY
;
226 static GLuint _uniform_water_g_world_depth
;
227 static void shader_water_uPv(m4x4f m
){
228 glUniformMatrix4fv( _uniform_water_uPv
, 1, GL_FALSE
, (float *)m
);
230 static void shader_water_uMdl(m4x3f m
){
231 glUniformMatrix4x3fv( _uniform_water_uMdl
, 1, GL_FALSE
, (float *)m
);
233 static void shader_water_uTexMain(int i
){
234 glUniform1i( _uniform_water_uTexMain
, i
);
236 static void shader_water_uTexDudv(int i
){
237 glUniform1i( _uniform_water_uTexDudv
, i
);
239 static void shader_water_uTexBack(int i
){
240 glUniform1i( _uniform_water_uTexBack
, i
);
242 static void shader_water_uInvRes(v2f v
){
243 glUniform2fv( _uniform_water_uInvRes
, 1, v
);
245 static void shader_water_uTime(float f
){
246 glUniform1f( _uniform_water_uTime
, f
);
248 static void shader_water_uCamera(v3f v
){
249 glUniform3fv( _uniform_water_uCamera
, 1, v
);
251 static void shader_water_uSurfaceY(float f
){
252 glUniform1f( _uniform_water_uSurfaceY
, f
);
254 static void shader_water_g_world_depth(int i
){
255 glUniform1i( _uniform_water_g_world_depth
, i
);
257 static void shader_water_register(void){
258 vg_shader_register( &_shader_water
);
260 static void shader_water_use(void){ glUseProgram(_shader_water
.id
); }
261 static void shader_water_link(void){
262 _uniform_water_uPv
= glGetUniformLocation( _shader_water
.id
, "uPv" );
263 _uniform_water_uMdl
= glGetUniformLocation( _shader_water
.id
, "uMdl" );
264 _uniform_water_uTexMain
= glGetUniformLocation( _shader_water
.id
, "uTexMain" );
265 _uniform_water_uTexDudv
= glGetUniformLocation( _shader_water
.id
, "uTexDudv" );
266 _uniform_water_uTexBack
= glGetUniformLocation( _shader_water
.id
, "uTexBack" );
267 _uniform_water_uInvRes
= glGetUniformLocation( _shader_water
.id
, "uInvRes" );
268 _uniform_water_uTime
= glGetUniformLocation( _shader_water
.id
, "uTime" );
269 _uniform_water_uCamera
= glGetUniformLocation( _shader_water
.id
, "uCamera" );
270 _uniform_water_uSurfaceY
= glGetUniformLocation( _shader_water
.id
, "uSurfaceY" );
271 _uniform_water_g_world_depth
= glGetUniformLocation( _shader_water
.id
, "g_world_depth" );
273 #endif /* SHADER_water_H */