]> git.tdb.fi Git - libs/gl.git/commitdiff
Perform culling in eye space and account for Renderer's matrix
authorMikko Rasa <tdb@tdb.fi>
Fri, 7 Oct 2016 12:38:09 +0000 (15:38 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 7 Oct 2016 12:38:09 +0000 (15:38 +0300)
This allows container renderables to apply intermediate transforms to the
Renderer.  Sometimes it may be inconvenient to propagate every matrix
change down through the hierarchy.

source/scene.cpp
source/scene.h

index 7d3c167975e7a3c548de29cc5aff782439e46a3a..ad9dcd151110724a62581f1a7190de332b3ea86e 100644 (file)
@@ -27,26 +27,20 @@ bool Scene::setup_frustum(const Renderer &renderer) const
        if(!camera)
                return false;
 
-       const Matrix &matrix = camera->get_object_matrix();
+       culling_matrix = renderer.get_matrix();
 
        float y = tan(camera->get_field_of_view()/2.0f);
        float s = sqrt(y*y+1);
-       frustum_edges[0] = matrix*Vector4(0, 1/s, y/s, 0);
-       frustum_edges[1] = matrix*Vector4(0, -1/s, y/s, 0);
+       frustum_edges[0] = Vector4(0, 1/s, y/s, 0);
+       frustum_edges[1] = Vector4(0, -1/s, y/s, 0);
 
        float x = y*camera->get_aspect();
        s = sqrt(x*x+1);
-       frustum_edges[2] = matrix*Vector4(1/s, 0, x/s, 0);
-       frustum_edges[3] = matrix*Vector4(-1/s, 0, x/s, 0);
+       frustum_edges[2] = Vector4(1/s, 0, x/s, 0);
+       frustum_edges[3] = Vector4(-1/s, 0, x/s, 0);
 
-       const Vector3 &camera_pos = camera->get_position();
-       Vector4 pos4 = compose(camera_pos, 0.0f);
-       for(unsigned i=0; i<4; ++i)
-               frustum_edges[i].w = -inner_product(frustum_edges[i], pos4);
-
-       const Vector3 &look_dir = camera->get_look_direction();
-       frustum_edges[4] = compose(look_dir, -dot(camera_pos, look_dir)-camera->get_far_clip());
-       frustum_edges[5] = compose(-look_dir, dot(camera_pos, look_dir)+camera->get_near_clip());
+       frustum_edges[4] = Vector4(0, 0, -1, -camera->get_far_clip());
+       frustum_edges[5] = Vector4(0, 0, 1, camera->get_near_clip());
 
        return true;
 }
@@ -58,7 +52,7 @@ bool Scene::frustum_cull(const Renderable &renderable) const
        if(!matrix || !bsphere)
                return false;
 
-       Vector4 center = *matrix*compose(bsphere->get_center(), 1.0f);
+       Vector4 center = culling_matrix*(*matrix*compose(bsphere->get_center(), 1.0f));
        float radius = bsphere->get_radius();
 
        for(unsigned i=0; i<6; ++i)
index 27656a59db2fbbdc5af57de1daf498361f5f07a5..a04fbc01f9a304c5969759fd6afc6380af52b8fe 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <list>
 #include <msp/datafile/objectloader.h>
+#include "matrix.h"
 #include "renderable.h"
 #include "vector.h"
 
@@ -28,6 +29,7 @@ public:
 
 protected:
        std::list<Renderable *> owned_data;
+       mutable Matrix culling_matrix;
        mutable Vector4 frustum_edges[6];
 
        Scene() { }