medfall

A super great game engine
Log | Files | Refs

shadowed_vertex_colours.glsl (2339B)


      1 layout( std140 ) uniform model {
      2 	mat4 M;
      3 };
      4 
      5 layout( std140 ) uniform view {
      6 	mat4 V;
      7 	mat4 P;
      8 	vec3 camera_pos;
      9 };
     10 
     11 layout( std140 ) uniform light_view {
     12 	mat4 lightVP;
     13 	vec3 light_pos;
     14 };
     15 
     16 uniform sampler2D shadowmap;
     17 uniform sampler2D dither_noise;
     18 
     19 struct VSOut {
     20 	vec4 light_space_position;
     21 	vec3 position;
     22 	vec3 normal;
     23 	vec3 colour;
     24 };
     25 
     26 #ifdef VERTEX_SHADER
     27 
     28 in vec3 position;
     29 in vec3 normal;
     30 in vec3 colour;
     31 
     32 out VSOut v2f;
     33 
     34 void main() {
     35 	vec4 world_space = M * vec4( position, 1.0 );
     36 	gl_Position = P * V * world_space;
     37 	v2f.light_space_position = lightVP * world_space;
     38 	v2f.position = world_space.xyz;
     39 	v2f.normal = mat3( M ) * normal;
     40 	v2f.colour = colour;
     41 }
     42 
     43 #else
     44 
     45 in VSOut v2f;
     46 out vec4 screen_colour;
     47 
     48 // http://the-witness.net/news/2013/09/shadow-mapping-summary-part-1/
     49 vec2 get_shadow_offsets( vec3 N, vec3 L ) {
     50 	float cos_alpha = dot_sat( N, L );
     51 	float offset_scale_N = sqrt( 1.0 - cos_alpha * cos_alpha );
     52 	float offset_scale_L = offset_scale_N / cos_alpha;
     53 	return vec2( offset_scale_N, min( 5.0, offset_scale_L ) );
     54 }
     55 
     56 void main() {
     57 	vec3 light_colour = vec3( 400.0, 400.0, 400.0 );
     58 	vec3 lightdir = normalize( light_pos - v2f.position );
     59 	vec3 to_camera = normalize( camera_pos - v2f.position );
     60 	vec3 normal = normalize( v2f.normal );
     61 
     62 	vec3 light_ndc = v2f.light_space_position.xyz / v2f.light_space_position.w;
     63 	vec3 light_norm = light_ndc * 0.5 + 0.5;
     64 	vec2 offsets = get_shadow_offsets( normal, lightdir );
     65 	float bias = 0.00002 + 0.00001 * offsets.x + 0.00005 * offsets.y;
     66 
     67 	float shadow = 0;
     68 
     69 	if( light_norm.z > 1.0 ) {
     70 		shadow = 1.0;
     71 	}
     72 	else {
     73 		vec2 inv_shadowmap_size = 1.0 / textureSize( shadowmap, 0 );
     74 		for( int x = -1; x <= 1; x++ ) {
     75 			for( int y = -1; y <= 1; y++ ) {
     76 				vec2 offset = vec2( x, y ) * inv_shadowmap_size;
     77 				float shadow_depth = texture( shadowmap, light_norm.xy + offset ).r;
     78 				if( light_norm.z - bias <= shadow_depth ) {
     79 					shadow += 1.0 / 9.0;
     80 				}
     81 			}
     82 		}
     83 	}
     84 
     85 	vec3 noise = texture( dither_noise, gl_FragCoord.xy / textureSize( dither_noise, 0 ) ).xxx;
     86 	noise = ( noise - vec3( 0.5 ) ) / 128.0;
     87 
     88 	vec3 ambient = vec3( 0.03 ) * v2f.colour;
     89 	vec3 Lo = cook_torrance_brdf( to_camera, v2f.position, normal, light_pos, light_colour, v2f.colour, 0.75, 0.0 );
     90 
     91 	screen_colour = vec4( linear_to_srgb( reinhard_tonemap( ambient + Lo * shadow + noise ) ), 1.0 );
     92 }
     93 
     94 #endif