]> git.tdb.fi Git - libs/gl.git/blobdiff - source/shadowmap.cpp
Use vector and matrix operations in ShadowMap
[libs/gl.git] / source / shadowmap.cpp
index 86715fca956ed549c5723a110a86f2bc4e6c94c0..737423f7ff3687fd5e6c95870f2ad854623128c5 100644 (file)
@@ -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);