1 #ifndef SHADER_scene_terrain_H
2 #define SHADER_scene_terrain_H
3 static void shader_scene_terrain_link(void);
4 static void shader_scene_terrain_register(void);
5 static struct vg_shader _shader_scene_terrain
= {
6 .name
= "scene_terrain",
7 .link
= shader_scene_terrain_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 uSandColour;\n"
79 "uniform vec2 uBlendOffset;\n"
80 "uniform vec3 uBoard0;\n"
81 "uniform vec3 uBoard1;\n"
90 "flat in vec4 light_colours[3];\n"
91 "flat in vec4 light_positions[3];\n"
94 "layout (location = 0) out vec4 oColour;\n"
96 "layout (std140) uniform ub_world_lighting\n"
98 " vec4 g_light_colours[3];\n"
99 " vec4 g_light_directions[3];\n"
100 " vec4 g_ambient_colour;\n"
102 " vec4 g_water_plane;\n"
103 " vec4 g_depth_bounds;\n"
104 " float g_water_fog;\n"
105 " int g_light_count;\n"
106 " int g_light_preview;\n"
107 " int g_shadow_samples;\n"
111 " //vec4 g_point_light_positions[32];\n"
112 " //vec4 g_point_light_colours[32];\n"
115 "uniform sampler2D g_world_depth;\n"
117 "float world_depth_sample( vec3 pos )\n"
119 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
120 " return texture( g_world_depth, depth_coord ).r;\n"
123 "float world_water_depth( vec3 pos )\n"
125 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
126 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
127 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
130 "float shadow_sample( vec3 vdir )\n"
132 " vec3 sample_pos = aWorldCo + vdir;\n"
133 " float height_sample = world_depth_sample( sample_pos );\n"
135 " float fdelta = height_sample - sample_pos.y;\n"
136 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
139 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
141 " float dist = pow(fdist*0.0008,1.2);\n"
142 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
146 "// New lighting model\n"
148 "vec3 newlight_compute_ambient()\n"
150 " return g_ambient_colour.rgb;\n"
153 "float newlight_compute_sun_shadow()\n"
155 " if( g_shadow_samples == 0 )\n"
160 " float fspread = g_light_colours[0].w;\n"
161 " vec3 vdir = g_light_directions[0].xyz;\n"
162 " float flength = g_light_directions[0].w;\n"
164 " float famt = 0.0;\n"
165 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
168 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
169 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
172 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
174 " return 1.0 - famt;\n"
177 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
179 " vec3 vtotal = g_ambient_colour.rgb;\n"
181 " for( int i=0; i<g_light_count; i++ )\n"
183 " vec3 vcolour = g_light_colours[i].rgb;\n"
184 " vec3 vdir = g_light_directions[i].xyz;\n"
186 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
187 " vtotal += vcolour*flight;\n"
193 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
195 " vec3 vcolour = g_light_colours[0].rgb;\n"
196 " vec3 vdir = g_light_directions[0].xyz;\n"
198 " vec3 specdir = reflect( -vdir, wnormal );\n"
199 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
200 " return vcolour*spec*fintensity;\n"
203 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
204 " vec3 light_pos, vec3 light_colour )\n"
206 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
208 " float quadratic = dot(light_delta,light_delta);\n"
209 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
210 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
212 " return light_colour*attenuation;\n"
217 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
219 " vec3 pa = p - a;\n"
220 " vec3 ba = b - a;\n"
222 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
223 " return length( pa - ba*h );\n"
226 "float compute_board_shadow()\n"
228 " // player shadow\n"
229 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
230 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
231 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
233 " return 1.0 - player_shadow*0.8;\n"
236 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
239 " vec3 halfview = uCamera - aWorldCo;\n"
240 " float fdist = length(halfview);\n"
241 " halfview /= fdist;\n"
243 " vec3 total_light = newlight_compute_ambient();\n"
245 " // Compute world lighting contribution and apply it according to the\n"
248 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
249 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
251 " float world_shadow = newlight_compute_sun_shadow();\n"
252 " float board_shadow = compute_board_shadow();\n"
254 " total_light += world_light * min( board_shadow, world_shadow );\n"
256 " // Compute the other lights that exist in the map, not effected by the sun\n"
259 " total_light += newlight_compute_quadratic\n"
261 " wnormal, halfview,\n"
262 " light_positions[0].xyz,\n"
263 " light_colours[0].rgb \n"
264 " ) * board_shadow;\n"
265 " total_light += newlight_compute_quadratic\n"
267 " wnormal, halfview,\n"
268 " light_positions[1].xyz,\n"
269 " light_colours[1].rgb \n"
270 " ) * board_shadow;\n"
271 " total_light += newlight_compute_quadratic\n"
273 " wnormal, halfview,\n"
274 " light_positions[2].xyz,\n"
275 " light_colours[2].rgb \n"
276 " ) * board_shadow;\n"
278 " return apply_fog( diffuse * total_light, fdist );\n"
283 "const float k_motion_lerp_amount = 0.01;\n"
287 "layout (location = 1) out vec2 oMotionVec;\n"
289 "in vec3 aMotionVec0;\n"
290 "in vec3 aMotionVec1;\n"
292 "void compute_motion_vectors()\n"
294 " // Write motion vectors\n"
295 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
296 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
298 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
305 " compute_motion_vectors();\n"
309 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
311 " // ws modulation\n"
312 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
314 " // Creating normal patches\n"
315 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
316 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
317 " qnorm += vec3(0.001,0.0,0.0);\n"
319 " vec2 dir = normalize(qnorm.xz);\n"
320 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
321 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
323 " // Patch local noise\n"
324 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
326 " // Colour blending\n"
327 " float amtgrass = step(qnorm.y,0.6);\n"
328 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
329 " vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;\n"
330 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
331 " vfrag = mix( vfrag, uSandColour, amtsand );\n"
333 " qnorm = mix( qnorm, aNorm.xyz, amtsand );\n"
335 " if( g_light_preview == 1 )\n"
337 " vfrag = vec3(0.5);\n"
340 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
341 " oColour = vec4(vfrag, 1.0);\n"
346 static GLuint _uniform_scene_terrain_uMdl
;
347 static GLuint _uniform_scene_terrain_uPv
;
348 static GLuint _uniform_scene_terrain_uPvmPrev
;
349 static GLuint _uniform_scene_terrain_uLightsArray
;
350 static GLuint _uniform_scene_terrain_uTexGarbage
;
351 static GLuint _uniform_scene_terrain_uTexGradients
;
352 static GLuint _uniform_scene_terrain_uCamera
;
353 static GLuint _uniform_scene_terrain_uSandColour
;
354 static GLuint _uniform_scene_terrain_uBlendOffset
;
355 static GLuint _uniform_scene_terrain_uBoard0
;
356 static GLuint _uniform_scene_terrain_uBoard1
;
357 static GLuint _uniform_scene_terrain_g_world_depth
;
358 static void shader_scene_terrain_uMdl(m4x3f m
){
359 glUniformMatrix4x3fv(_uniform_scene_terrain_uMdl
,1,GL_FALSE
,(float*)m
);
361 static void shader_scene_terrain_uPv(m4x4f m
){
362 glUniformMatrix4fv(_uniform_scene_terrain_uPv
,1,GL_FALSE
,(float*)m
);
364 static void shader_scene_terrain_uPvmPrev(m4x4f m
){
365 glUniformMatrix4fv(_uniform_scene_terrain_uPvmPrev
,1,GL_FALSE
,(float*)m
);
367 static void shader_scene_terrain_uTexGarbage(int i
){
368 glUniform1i(_uniform_scene_terrain_uTexGarbage
,i
);
370 static void shader_scene_terrain_uTexGradients(int i
){
371 glUniform1i(_uniform_scene_terrain_uTexGradients
,i
);
373 static void shader_scene_terrain_uCamera(v3f v
){
374 glUniform3fv(_uniform_scene_terrain_uCamera
,1,v
);
376 static void shader_scene_terrain_uSandColour(v3f v
){
377 glUniform3fv(_uniform_scene_terrain_uSandColour
,1,v
);
379 static void shader_scene_terrain_uBlendOffset(v2f v
){
380 glUniform2fv(_uniform_scene_terrain_uBlendOffset
,1,v
);
382 static void shader_scene_terrain_uBoard0(v3f v
){
383 glUniform3fv(_uniform_scene_terrain_uBoard0
,1,v
);
385 static void shader_scene_terrain_uBoard1(v3f v
){
386 glUniform3fv(_uniform_scene_terrain_uBoard1
,1,v
);
388 static void shader_scene_terrain_g_world_depth(int i
){
389 glUniform1i(_uniform_scene_terrain_g_world_depth
,i
);
391 static void shader_scene_terrain_register(void){
392 vg_shader_register( &_shader_scene_terrain
);
394 static void shader_scene_terrain_use(void){ glUseProgram(_shader_scene_terrain
.id
); }
395 static void shader_scene_terrain_link(void){
396 _uniform_scene_terrain_uMdl
= glGetUniformLocation( _shader_scene_terrain
.id
, "uMdl" );
397 _uniform_scene_terrain_uPv
= glGetUniformLocation( _shader_scene_terrain
.id
, "uPv" );
398 _uniform_scene_terrain_uPvmPrev
= glGetUniformLocation( _shader_scene_terrain
.id
, "uPvmPrev" );
399 _uniform_scene_terrain_uLightsArray
= glGetUniformLocation( _shader_scene_terrain
.id
, "uLightsArray" );
400 _uniform_scene_terrain_uTexGarbage
= glGetUniformLocation( _shader_scene_terrain
.id
, "uTexGarbage" );
401 _uniform_scene_terrain_uTexGradients
= glGetUniformLocation( _shader_scene_terrain
.id
, "uTexGradients" );
402 _uniform_scene_terrain_uCamera
= glGetUniformLocation( _shader_scene_terrain
.id
, "uCamera" );
403 _uniform_scene_terrain_uSandColour
= glGetUniformLocation( _shader_scene_terrain
.id
, "uSandColour" );
404 _uniform_scene_terrain_uBlendOffset
= glGetUniformLocation( _shader_scene_terrain
.id
, "uBlendOffset" );
405 _uniform_scene_terrain_uBoard0
= glGetUniformLocation( _shader_scene_terrain
.id
, "uBoard0" );
406 _uniform_scene_terrain_uBoard1
= glGetUniformLocation( _shader_scene_terrain
.id
, "uBoard1" );
407 _uniform_scene_terrain_g_world_depth
= glGetUniformLocation( _shader_scene_terrain
.id
, "g_world_depth" );
409 #endif /* SHADER_scene_terrain_H */