From a8c9bac7217e1750ce7578600429d16e8010d8b1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 16 Dec 2013 22:24:55 +0200 Subject: [PATCH] Use vector and matrix operations in ShadowMap --- source/shadowmap.cpp | 59 +++++++++++++++----------------------------- source/shadowmap.h | 1 + 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/source/shadowmap.cpp b/source/shadowmap.cpp index 86715fca..737423f7 100644 --- a/source/shadowmap.cpp +++ b/source/shadowmap.cpp @@ -74,58 +74,39 @@ void ShadowMap::setup_frame() const renderable.setup_frame(); rendered = true; - Vector4 lpos = light.get_position(); - + const Vector4 &lpos = light.get_position(); + Vector3 back; if(lpos.w) { /* XXX Not really proper way to support positional lights, but good enough when the light source is far away */ - lpos -= Vector4(target.x, target.y, target.z, 1.0f); - } - - lpos.normalize(); - - float matrix[16]; - if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y)) - { - float d = sqrt(lpos.x*lpos.x+lpos.z*lpos.z); - matrix[0] = lpos.z/d; - matrix[4] = 0; - matrix[8] = -lpos.x/d; - matrix[1] = -lpos.x*lpos.y/d; - matrix[5] = d; - matrix[9] = -lpos.z*lpos.y/d; + back = Vector3(lpos)-target; } else - { - float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y); - matrix[0] = -lpos.y/d; - matrix[4] = lpos.x/d; - matrix[8] = 0; - matrix[1] = -lpos.x*lpos.z/d; - matrix[5] = -lpos.y*lpos.z/d; - matrix[9] = d; - } + back = Vector3(lpos); + back.normalize(); - matrix[2] = lpos.x; - matrix[6] = lpos.y; - matrix[10] = lpos.z; + Vector3 up; + if(abs(back.z)<0.99) + up = Vector3(0, 0, 1); + else + up = Vector3(0, 1, 0); - matrix[12] = -(target.x*matrix[0]+target.y*matrix[4]+target.z*matrix[8]); - matrix[13] = -(target.x*matrix[1]+target.y*matrix[5]+target.z*matrix[9]); - matrix[14] = -(target.x*matrix[2]+target.y*matrix[6]+target.z*matrix[10]); - matrix[3] = 0; - matrix[7] = 0; - matrix[11] = 0; - matrix[15] = 1; + Vector3 right = normalize(cross(up, back)); - light_matrix = matrix; + Vector4 columns[4]; + columns[0] = Vector4(right, 0.0f); + columns[1] = Vector4(normalize(cross(back, right)), 0.0f); + columns[2] = Vector4(back, 0.0f); + columns[3] = Vector4(target, 1.0f); + light_matrix = Matrix::from_columns(columns); + view_matrix = invert(light_matrix); MatrixStack::Push push_mv(MatrixStack::modelview()); MatrixStack::Push push_proj(MatrixStack::projection()); MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius); - MatrixStack::modelview() = light_matrix; + MatrixStack::modelview() = view_matrix; Bind bind_fbo(fbo, true); Bind bind_depth(DepthTest::lequal()); @@ -144,7 +125,7 @@ void ShadowMap::render(Renderer &renderer, const Tag &tag) const if(!enabled_passes.count(tag)) return renderer.render(renderable, tag); - const float *matrix = light_matrix.data(); + const float *matrix = view_matrix.data(); // Has side effect of changing the current unit depth_buf.bind_to(unit); diff --git a/source/shadowmap.h b/source/shadowmap.h index aeabaeb1..1b9d8f14 100644 --- a/source/shadowmap.h +++ b/source/shadowmap.h @@ -26,6 +26,7 @@ private: const Light &light; mutable Framebuffer fbo; mutable Matrix light_matrix; + mutable Matrix view_matrix; unsigned unit; Texture2D depth_buf; Vector3 target; -- 2.45.2