From: Mikko Rasa Date: Fri, 7 Oct 2016 12:38:09 +0000 (+0300) Subject: Perform culling in eye space and account for Renderer's matrix X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=0a20dd14a0d1fd027a7d411b4d427bd4252342cc Perform culling in eye space and account for Renderer's matrix 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. --- diff --git a/source/scene.cpp b/source/scene.cpp index 7d3c1679..ad9dcd15 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -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) diff --git a/source/scene.h b/source/scene.h index 27656a59..a04fbc01 100644 --- a/source/scene.h +++ b/source/scene.h @@ -3,6 +3,7 @@ #include #include +#include "matrix.h" #include "renderable.h" #include "vector.h" @@ -28,6 +29,7 @@ public: protected: std::list owned_data; + mutable Matrix culling_matrix; mutable Vector4 frustum_edges[6]; Scene() { }