polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit da368dfb2da7ea0e12efc1baa2b53b0d12f83734
parent 3ecc86de3665ada0426d6bf15d876f496b04e539
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  9 Jan 2020 22:37:03 -0800

shadow updates

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Retc/shaders/test.f.glsl -> etc/shaders/main.f.glsl | 0
Metc/shaders/shadows.glsl | 40++++++++++++++++++++++++++++++++--------
Msrc/render.c | 2+-
Msrc/update.c | 4++--
4 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/etc/shaders/test.f.glsl b/etc/shaders/main.f.glsl diff --git a/etc/shaders/shadows.glsl b/etc/shaders/shadows.glsl @@ -1,17 +1,41 @@ +float texture2DCompare(sampler2D depths, vec2 uv, float compare){ + float depth = texture2D(depths, uv).r; + return step(compare, depth); +} + +float texture2DShadowLerp(sampler2D depths, vec2 size, vec2 uv, float compare){ + vec2 texelSize = vec2(1.0)/size; + vec2 f = fract(uv*size+0.5); + vec2 centroidUV = floor(uv*size+0.5)/size; + + float lb = texture2DCompare(depths, centroidUV+texelSize*vec2(0.0, 0.0), compare); + float lt = texture2DCompare(depths, centroidUV+texelSize*vec2(0.0, 1.0), compare); + float rb = texture2DCompare(depths, centroidUV+texelSize*vec2(1.0, 0.0), compare); + float rt = texture2DCompare(depths, centroidUV+texelSize*vec2(1.0, 1.0), compare); + float a = mix(lb, lt, f.y); + float b = mix(rb, rt, f.y); + float c = mix(a, b, f.x); + return c; +} + +float PCF(sampler2D depths, vec2 size, vec2 uv, float compare){ + float result = 0.0; + for(int x=-1; x<=1; x++){ + for(int y=-1; y<=1; y++){ + vec2 off = vec2(x,y)/size; + result += texture2DShadowLerp(depths, size, uv+off, compare); + } + } + return result/9.0; +} + vec3 shadow_strength(vec4 position, vec4 normal, vec4 v_shadow_coord) { vec3 visibility = vec3(1.0); - // vec4 world_space_normal = normalize(normal * normal); - // float cos_theta = max(0.0, dot(light_dir, world_space_normal.xyz)); - // float shadow_normal_offset = 1.0; - // float normal_offset_scale = clamp(1.0 - cos_theta, 0.0, 1.0); - // normal_offset_scale *= shadow_normal_offset * 0.01; - // vec4 shadow_offset = world * (world_space_normal * normal_offset_scale); - // vec4 shadow_coord = depth_vp * ((world * position) + shadow_offset); vec4 shadow_sample = texture(shadow_map, v_shadow_coord.xy); float light_angle = dot(light_dir, normal.xyz); - float bias = 0.0002; + float bias = 0.002; bool in_shadow = shadow_sample.z < v_shadow_coord.z - bias diff --git a/src/render.c b/src/render.c @@ -99,7 +99,7 @@ init_gl(struct resources *resources, int width, int height) { /* assert(ok && "terrain tessellation eval shader"); */ /* check_gl(); */ - ok = make_shader(GL_FRAGMENT_SHADER, SHADER("test.f.glsl"), &fragment); + ok = make_shader(GL_FRAGMENT_SHADER, SHADER("main.f.glsl"), &fragment); rtassert(ok, "default fragment shader"); check_gl(); diff --git a/src/update.c b/src/update.c @@ -171,7 +171,7 @@ void resize_fbos(struct entity *player, struct fbo *shadow_buffer, } // TODO: compute better bounds based - const float factor = 10.5; + const float factor = 5.5; struct model *model = get_model(&player->model_id); assert(model); struct geometry *geom = get_geometry(&model->geom_id); assert(geom); @@ -386,7 +386,7 @@ static void player_update(struct game *game, struct entity *player) float pos[3]; float pen; vec3_copy(node_world(node), pos); - debug("node_world(player) %f %f %f\n", pos[0], pos[1], pos[2]); + /* debug("node_world(player) %f %f %f\n", pos[0], pos[1], pos[2]); */ struct tri *tri = collide_terrain(terrain, pos, move, &pen); /* node_translate(node, move); */