1 #ifndef SHADER_scene_vertex_blend_H
2 #define SHADER_scene_vertex_blend_H
3 static void shader_scene_vertex_blend_link(void);
4 static void shader_scene_vertex_blend_register(void);
5 static struct vg_shader _shader_scene_vertex_blend
= {
6 .name
= "scene_vertex_blend",
7 .link
= shader_scene_vertex_blend_link
,
11 "layout (location=0) in vec3 a_co;\n"
12 "layout (location=1) in vec4 a_norm;\n"
13 "layout (location=2) in vec2 a_uv;\n"
14 "layout (location=3) in ivec4 a_lights;\n"
17 "const float k_motion_lerp_amount = 0.01;\n"
21 "out vec3 aMotionVec0;\n"
22 "out vec3 aMotionVec1;\n"
24 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
26 " // This magically solves some artifacting errors!\n"
28 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
30 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
31 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
36 "uniform mat4x3 uMdl;\n"
38 "uniform mat4 uPvmPrev;\n"
39 "uniform samplerBuffer uLightsArray;\n"
44 "out vec3 aWorldCo;\n"
45 "flat out vec4 light_colours[3];\n"
46 "flat out vec4 light_positions[3];\n"
50 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
51 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
52 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
54 " vs_motion_out( vproj0, vproj1 );\n"
56 " gl_Position = vproj0;\n"
59 " aNorm = vec4( mat3(uMdl) * a_norm.xyz, a_norm.w );\n"
61 " aWorldCo = world_pos0;\n"
64 " light_colours[0] = texelFetch( uLightsArray, a_lights.x*2+0 );\n"
65 " light_colours[1] = texelFetch( uLightsArray, a_lights.y*2+0 );\n"
66 " light_colours[2] = texelFetch( uLightsArray, a_lights.z*2+0 );\n"
67 " light_positions[0] = texelFetch( uLightsArray, a_lights.x*2+1 );\n"
68 " light_positions[1] = texelFetch( uLightsArray, a_lights.y*2+1 );\n"
69 " light_positions[2] = texelFetch( uLightsArray, a_lights.z*2+1 );\n"
75 "uniform sampler2D uTexGarbage;\n"
76 "uniform sampler2D uTexGradients;\n"
77 "uniform vec3 uCamera;\n"
78 "uniform vec3 uBoard0;\n"
79 "uniform vec3 uBoard1;\n"
88 "flat in vec4 light_colours[3];\n"
89 "flat in vec4 light_positions[3];\n"
92 "layout (location = 0) out vec4 oColour;\n"
94 "layout (std140) uniform ub_world_lighting\n"
96 " vec4 g_light_colours[3];\n"
97 " vec4 g_light_directions[3];\n"
98 " vec4 g_ambient_colour;\n"
100 " vec4 g_water_plane;\n"
101 " vec4 g_depth_bounds;\n"
102 " float g_water_fog;\n"
103 " int g_light_count;\n"
104 " int g_light_preview;\n"
105 " int g_shadow_samples;\n"
109 " //vec4 g_point_light_positions[32];\n"
110 " //vec4 g_point_light_colours[32];\n"
113 "uniform sampler2D g_world_depth;\n"
115 "float world_depth_sample( vec3 pos )\n"
117 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
118 " return texture( g_world_depth, depth_coord ).r;\n"
121 "float world_water_depth( vec3 pos )\n"
123 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
124 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
125 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
128 "float shadow_sample( vec3 vdir )\n"
130 " vec3 sample_pos = aWorldCo + vdir;\n"
131 " float height_sample = world_depth_sample( sample_pos );\n"
133 " float fdelta = height_sample - sample_pos.y;\n"
134 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
137 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
139 " float dist = pow(fdist*0.0008,1.2);\n"
140 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
144 "// New lighting model\n"
146 "vec3 newlight_compute_ambient()\n"
148 " return g_ambient_colour.rgb;\n"
151 "float newlight_compute_sun_shadow()\n"
153 " if( g_shadow_samples == 0 )\n"
158 " float fspread = g_light_colours[0].w;\n"
159 " vec3 vdir = g_light_directions[0].xyz;\n"
160 " float flength = g_light_directions[0].w;\n"
162 " float famt = 0.0;\n"
163 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
164 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
165 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
166 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
167 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
168 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
172 " return 1.0 - famt;\n"
175 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
177 " vec3 vtotal = g_ambient_colour.rgb;\n"
179 " for( int i=0; i<g_light_count; i++ )\n"
181 " vec3 vcolour = g_light_colours[i].rgb;\n"
182 " vec3 vdir = g_light_directions[i].xyz;\n"
184 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
185 " vtotal += vcolour*flight;\n"
191 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
193 " vec3 vcolour = g_light_colours[0].rgb;\n"
194 " vec3 vdir = g_light_directions[0].xyz;\n"
196 " vec3 specdir = reflect( -vdir, wnormal );\n"
197 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
198 " return vcolour*spec*fintensity;\n"
201 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
202 " vec3 light_pos, vec3 light_colour )\n"
204 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
206 " float quadratic = dot(light_delta,light_delta);\n"
207 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
208 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
210 " return light_colour*attenuation;\n"
215 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
217 " vec3 pa = p - a;\n"
218 " vec3 ba = b - a;\n"
220 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
221 " return length( pa - ba*h );\n"
224 "float compute_board_shadow()\n"
226 " // player shadow\n"
227 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
228 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
229 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
231 " return 1.0 - player_shadow*0.8;\n"
234 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
237 " vec3 halfview = uCamera - aWorldCo;\n"
238 " float fdist = length(halfview);\n"
239 " halfview /= fdist;\n"
241 " vec3 total_light = newlight_compute_ambient();\n"
243 " // Compute world lighting contribution and apply it according to the\n"
246 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
247 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
249 " float world_shadow = newlight_compute_sun_shadow();\n"
250 " float board_shadow = compute_board_shadow();\n"
252 " total_light += world_light * min( board_shadow, world_shadow );\n"
254 " // Compute the other lights that exist in the map, not effected by the sun\n"
257 " total_light += newlight_compute_quadratic\n"
259 " wnormal, halfview,\n"
260 " light_positions[0].xyz,\n"
261 " light_colours[0].rgb \n"
262 " ) * board_shadow;\n"
263 " total_light += newlight_compute_quadratic\n"
265 " wnormal, halfview,\n"
266 " light_positions[1].xyz,\n"
267 " light_colours[1].rgb \n"
268 " ) * board_shadow;\n"
269 " total_light += newlight_compute_quadratic\n"
271 " wnormal, halfview,\n"
272 " light_positions[2].xyz,\n"
273 " light_colours[2].rgb \n"
274 " ) * board_shadow;\n"
276 " return apply_fog( diffuse * total_light, fdist );\n"
281 "const float k_motion_lerp_amount = 0.01;\n"
285 "layout (location = 1) out vec2 oMotionVec;\n"
287 "in vec3 aMotionVec0;\n"
288 "in vec3 aMotionVec1;\n"
290 "void compute_motion_vectors()\n"
292 " // Write motion vectors\n"
293 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
294 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
296 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
303 " compute_motion_vectors();\n"
305 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
307 " // ws modulation\n"
308 " vec4 wgarbage = vec4(0.5,0.5,0.5,1.0);\n"
310 " // Creating normal patches\n"
311 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
312 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
313 " qnorm += vec3(0.001,0.0,0.0);\n"
315 " vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
316 " vec3 tangent1 = cross(qnorm,tangent0);\n"
317 " vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.160;\n"
319 " // Patch local noise\n"
320 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
322 " // Colour blending\n"
323 " float fblendclip = step(0.380,aNorm.w + (rgarbage.r-0.5)*-1.740)*0.320;\n"
324 " vec2 uvgradients = aUv + vec2( fblendclip, 0.0 );\n"
326 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
327 " vfrag -= rgarbage.a*0.04;\n"
329 " if( g_light_preview == 1 )\n"
331 " vfrag = vec3(0.5);\n"
334 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
335 " oColour = vec4(vfrag, 1.0);\n"
340 static GLuint _uniform_scene_vertex_blend_uMdl
;
341 static GLuint _uniform_scene_vertex_blend_uPv
;
342 static GLuint _uniform_scene_vertex_blend_uPvmPrev
;
343 static GLuint _uniform_scene_vertex_blend_uLightsArray
;
344 static GLuint _uniform_scene_vertex_blend_uTexGarbage
;
345 static GLuint _uniform_scene_vertex_blend_uTexGradients
;
346 static GLuint _uniform_scene_vertex_blend_uCamera
;
347 static GLuint _uniform_scene_vertex_blend_uBoard0
;
348 static GLuint _uniform_scene_vertex_blend_uBoard1
;
349 static GLuint _uniform_scene_vertex_blend_g_world_depth
;
350 static void shader_scene_vertex_blend_uMdl(m4x3f m
){
351 glUniformMatrix4x3fv(_uniform_scene_vertex_blend_uMdl
,1,GL_FALSE
,(float*)m
);
353 static void shader_scene_vertex_blend_uPv(m4x4f m
){
354 glUniformMatrix4fv(_uniform_scene_vertex_blend_uPv
,1,GL_FALSE
,(float*)m
);
356 static void shader_scene_vertex_blend_uPvmPrev(m4x4f m
){
357 glUniformMatrix4fv(_uniform_scene_vertex_blend_uPvmPrev
,1,GL_FALSE
,(float*)m
);
359 static void shader_scene_vertex_blend_uTexGarbage(int i
){
360 glUniform1i(_uniform_scene_vertex_blend_uTexGarbage
,i
);
362 static void shader_scene_vertex_blend_uTexGradients(int i
){
363 glUniform1i(_uniform_scene_vertex_blend_uTexGradients
,i
);
365 static void shader_scene_vertex_blend_uCamera(v3f v
){
366 glUniform3fv(_uniform_scene_vertex_blend_uCamera
,1,v
);
368 static void shader_scene_vertex_blend_uBoard0(v3f v
){
369 glUniform3fv(_uniform_scene_vertex_blend_uBoard0
,1,v
);
371 static void shader_scene_vertex_blend_uBoard1(v3f v
){
372 glUniform3fv(_uniform_scene_vertex_blend_uBoard1
,1,v
);
374 static void shader_scene_vertex_blend_g_world_depth(int i
){
375 glUniform1i(_uniform_scene_vertex_blend_g_world_depth
,i
);
377 static void shader_scene_vertex_blend_register(void){
378 vg_shader_register( &_shader_scene_vertex_blend
);
380 static void shader_scene_vertex_blend_use(void){ glUseProgram(_shader_scene_vertex_blend
.id
); }
381 static void shader_scene_vertex_blend_link(void){
382 _uniform_scene_vertex_blend_uMdl
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uMdl" );
383 _uniform_scene_vertex_blend_uPv
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uPv" );
384 _uniform_scene_vertex_blend_uPvmPrev
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uPvmPrev" );
385 _uniform_scene_vertex_blend_uLightsArray
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uLightsArray" );
386 _uniform_scene_vertex_blend_uTexGarbage
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uTexGarbage" );
387 _uniform_scene_vertex_blend_uTexGradients
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uTexGradients" );
388 _uniform_scene_vertex_blend_uCamera
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uCamera" );
389 _uniform_scene_vertex_blend_uBoard0
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uBoard0" );
390 _uniform_scene_vertex_blend_uBoard1
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "uBoard1" );
391 _uniform_scene_vertex_blend_g_world_depth
= glGetUniformLocation( _shader_scene_vertex_blend
.id
, "g_world_depth" );
393 #endif /* SHADER_scene_vertex_blend_H */