1 #ifndef SHADER_vblend_H
2 #define SHADER_vblend_H
3 static void shader_vblend_link(void);
4 static void shader_vblend_register(void);
5 static struct vg_shader _shader_vblend
= {
7 .link
= shader_vblend_link
,
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"
20 "const float k_motion_lerp_amount = 0.01;\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
29 " // This magically solves some artifacting errors!\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
39 "uniform mat4x3 uMdl;\n"
41 "uniform mat4 uPvmPrev;\n"
47 "out vec3 aWorldCo;\n"
51 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
52 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
53 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
55 " vs_motion_out( vproj0, vproj1 );\n"
57 " gl_Position = vproj0;\n"
58 " aWorldCo = world_pos0;\n"
59 " aColour = a_colour;\n"
61 " aNorm = mat3(uMdl) * a_norm;\n"
68 "uniform sampler2D uTexGarbage;\n"
69 "uniform sampler2D uTexGradients;\n"
70 "uniform vec3 uCamera;\n"
71 "uniform vec3 uBoard0;\n"
72 "uniform vec3 uBoard1;\n"
81 "layout (location = 0) out vec4 oColour;\n"
83 "layout (std140) uniform ub_world_lighting\n"
85 " vec4 g_light_colours[3];\n"
86 " vec4 g_light_directions[3];\n"
87 " vec4 g_ambient_colour;\n"
89 " vec4 g_water_plane;\n"
90 " vec4 g_depth_bounds;\n"
91 " float g_water_fog;\n"
92 " int g_light_count;\n"
93 " int g_light_preview;\n"
94 " int g_shadow_samples;\n"
97 "uniform sampler2D g_world_depth;\n"
99 "// Standard diffuse + spec models\n"
100 "// ==============================\n"
102 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
104 " vec3 vtotal = g_ambient_colour.rgb;\n"
106 " for( int i=0; i<g_light_count; i++ )\n"
108 " vec3 vcolour = g_light_colours[i].rgb;\n"
109 " vec3 vdir = g_light_directions[i].xyz;\n"
111 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
112 " vtotal += vcolour*flight;\n"
115 " return vfrag * vtotal;\n"
118 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
120 " vec3 vcolour = g_light_colours[0].rgb;\n"
121 " vec3 vdir = g_light_directions[0].xyz;\n"
123 " vec3 specdir = reflect( -vdir, wnormal );\n"
124 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
125 " return vfrag + vcolour*spec*fintensity;\n"
128 "float world_depth_sample( vec3 pos )\n"
130 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
131 " return texture( g_world_depth, depth_coord ).r;\n"
134 "float shadow_sample( vec3 vdir )\n"
136 " vec3 sample_pos = aWorldCo + vdir;\n"
137 " float height_sample = world_depth_sample( sample_pos );\n"
139 " float fdelta = height_sample - sample_pos.y;\n"
140 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
143 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
145 " float faccum = 0.0;\n"
146 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
147 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
148 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
149 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
150 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
151 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
152 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
153 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
154 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
158 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
160 " vec3 pa = p - a;\n"
161 " vec3 ba = b - a;\n"
163 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
164 " return length( pa - ba*h );\n"
167 "vec3 do_light_shadowing( vec3 vfrag )\n"
169 " if( g_shadow_samples == 0 )\n"
174 " float fspread = g_light_colours[0].w;\n"
175 " vec3 vdir = g_light_directions[0].xyz;\n"
176 " float flength = g_light_directions[0].w;\n"
178 " float famt = 0.0;\n"
179 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
180 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
181 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
182 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
183 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
184 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
185 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
186 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
188 " // player shadow\n"
189 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.03 );\n"
190 " float player_shadow = max( 1.0-dist_to_player*1.7, 0.0 );\n"
191 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
193 " famt = max( player_shadow*0.6, famt );\n"
194 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
197 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
199 " float dist = pow(fdist*0.0008,1.2);\n"
200 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
205 "const float k_motion_lerp_amount = 0.01;\n"
209 "layout (location = 1) out vec2 oMotionVec;\n"
211 "in vec3 aMotionVec0;\n"
212 "in vec3 aMotionVec1;\n"
214 "void compute_motion_vectors()\n"
216 " // Write motion vectors\n"
217 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
218 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
220 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
227 " compute_motion_vectors();\n"
229 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
231 " // ws modulation\n"
232 " vec4 wgarbage = vec4(0.5,0.5,0.5,1.0);//texture( uTexGarbage, aCo.xz * 0.160 );\n"
234 " // Creating normal patches\n"
235 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
236 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
238 " vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
239 " vec3 tangent1 = cross(qnorm,tangent0);\n"
240 " vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.160;\n"
242 " // Patch local noise\n"
243 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
245 " // Colour blending\n"
246 " float fblendclip = step(0.380,aColour.r + (rgarbage.r-0.5)*-1.740)*0.320;\n"
247 " vec2 uvgradients = aUv + vec2( fblendclip, 0.0 );\n"
249 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
250 " vfrag -= rgarbage.a*0.04;\n"
252 " if( g_light_preview == 1 )\n"
254 " vfrag = vec3(0.5);\n"
258 " vec3 halfview = uCamera - aWorldCo;\n"
259 " float fdist = length( halfview );\n"
260 " halfview /= fdist;\n"
262 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
263 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
264 " vfrag = do_light_shadowing( vfrag );\n"
265 " vfrag = apply_fog( vfrag, fdist );\n"
267 " oColour = vec4(vfrag, 1.0 );\n"
272 static GLuint _uniform_vblend_uMdl
;
273 static GLuint _uniform_vblend_uPv
;
274 static GLuint _uniform_vblend_uPvmPrev
;
275 static GLuint _uniform_vblend_uTexGarbage
;
276 static GLuint _uniform_vblend_uTexGradients
;
277 static GLuint _uniform_vblend_uCamera
;
278 static GLuint _uniform_vblend_uBoard0
;
279 static GLuint _uniform_vblend_uBoard1
;
280 static GLuint _uniform_vblend_g_world_depth
;
281 static void shader_vblend_uMdl(m4x3f m
){
282 glUniformMatrix4x3fv(_uniform_vblend_uMdl
,1,GL_FALSE
,(float*)m
);
284 static void shader_vblend_uPv(m4x4f m
){
285 glUniformMatrix4fv(_uniform_vblend_uPv
,1,GL_FALSE
,(float*)m
);
287 static void shader_vblend_uPvmPrev(m4x4f m
){
288 glUniformMatrix4fv(_uniform_vblend_uPvmPrev
,1,GL_FALSE
,(float*)m
);
290 static void shader_vblend_uTexGarbage(int i
){
291 glUniform1i(_uniform_vblend_uTexGarbage
,i
);
293 static void shader_vblend_uTexGradients(int i
){
294 glUniform1i(_uniform_vblend_uTexGradients
,i
);
296 static void shader_vblend_uCamera(v3f v
){
297 glUniform3fv(_uniform_vblend_uCamera
,1,v
);
299 static void shader_vblend_uBoard0(v3f v
){
300 glUniform3fv(_uniform_vblend_uBoard0
,1,v
);
302 static void shader_vblend_uBoard1(v3f v
){
303 glUniform3fv(_uniform_vblend_uBoard1
,1,v
);
305 static void shader_vblend_g_world_depth(int i
){
306 glUniform1i(_uniform_vblend_g_world_depth
,i
);
308 static void shader_vblend_register(void){
309 vg_shader_register( &_shader_vblend
);
311 static void shader_vblend_use(void){ glUseProgram(_shader_vblend
.id
); }
312 static void shader_vblend_link(void){
313 _uniform_vblend_uMdl
= glGetUniformLocation( _shader_vblend
.id
, "uMdl" );
314 _uniform_vblend_uPv
= glGetUniformLocation( _shader_vblend
.id
, "uPv" );
315 _uniform_vblend_uPvmPrev
= glGetUniformLocation( _shader_vblend
.id
, "uPvmPrev" );
316 _uniform_vblend_uTexGarbage
= glGetUniformLocation( _shader_vblend
.id
, "uTexGarbage" );
317 _uniform_vblend_uTexGradients
= glGetUniformLocation( _shader_vblend
.id
, "uTexGradients" );
318 _uniform_vblend_uCamera
= glGetUniformLocation( _shader_vblend
.id
, "uCamera" );
319 _uniform_vblend_uBoard0
= glGetUniformLocation( _shader_vblend
.id
, "uBoard0" );
320 _uniform_vblend_uBoard1
= glGetUniformLocation( _shader_vblend
.id
, "uBoard1" );
321 _uniform_vblend_g_world_depth
= glGetUniformLocation( _shader_vblend
.id
, "g_world_depth" );
323 #endif /* SHADER_vblend_H */