From d16abe533233dc15810fe6bdf4a873d36eefc5dc Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 6 Nov 2016 00:10:59 +0200 Subject: [PATCH] Properly compute frustum culling for orthographic cameras It's still incorrect for skewed frustums but I'll fix that later. --- source/camera.h | 3 +++ source/scene.cpp | 31 ++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/source/camera.h b/source/camera.h index 44d3bf33..59cbaae3 100644 --- a/source/camera.h +++ b/source/camera.h @@ -36,6 +36,9 @@ public: void set_frustum_axis(float, float); void set_frustum_rotation(const Geometry::Angle &); const Geometry::Angle &get_field_of_view() const { return fov; } + bool is_orthographic() const { return fov==Geometry::Angle::zero(); } + float get_orthographic_width() const { return height*aspect; } + float get_orthographic_height() const { return height; } float get_aspect() const { return aspect; } float get_near_clip() const { return clip_near; } float get_far_clip() const { return clip_far; } diff --git a/source/scene.cpp b/source/scene.cpp index ad9dcd15..1625ca32 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -29,15 +29,28 @@ bool Scene::setup_frustum(const Renderer &renderer) const culling_matrix = renderer.get_matrix(); - float y = tan(camera->get_field_of_view()/2.0f); - float s = sqrt(y*y+1); - 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] = Vector4(1/s, 0, x/s, 0); - frustum_edges[3] = Vector4(-1/s, 0, x/s, 0); + if(camera->is_orthographic()) + { + float h = camera->get_orthographic_height(); + frustum_edges[0] = Vector4(0, 1, 0, -h); + frustum_edges[1] = Vector4(0, -1, 0, -h); + + float w = camera->get_orthographic_width(); + frustum_edges[2] = Vector4(1, 0, 0, -w); + frustum_edges[3] = Vector4(-1, 0, 0, -w); + } + else + { + float y = tan(camera->get_field_of_view()/2.0f); + float s = sqrt(y*y+1); + 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] = Vector4(1/s, 0, x/s, 0); + frustum_edges[3] = Vector4(-1/s, 0, x/s, 0); + } frustum_edges[4] = Vector4(0, 0, -1, -camera->get_far_clip()); frustum_edges[5] = Vector4(0, 0, 1, camera->get_near_clip()); -- 2.43.0