]> git.tdb.fi Git - libs/gl.git/commitdiff
Properly compute frustum culling for orthographic cameras
authorMikko Rasa <tdb@tdb.fi>
Sat, 5 Nov 2016 22:10:59 +0000 (00:10 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 5 Nov 2016 23:34:19 +0000 (01:34 +0200)
It's still incorrect for skewed frustums but I'll fix that later.

source/camera.h
source/scene.cpp

index 44d3bf33e6f7bf788f9e188f8ec91e8524084c30..59cbaae3760599148a4a9de610ae3b6e514d3003 100644 (file)
@@ -36,6 +36,9 @@ public:
        void set_frustum_axis(float, float);
        void set_frustum_rotation(const Geometry::Angle<float> &);
        const Geometry::Angle<float> &get_field_of_view() const { return fov; }
+       bool is_orthographic() const { return fov==Geometry::Angle<float>::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; }
index ad9dcd151110724a62581f1a7190de332b3ea86e..1625ca32c7c74052cdec47cdf10f741d26115f12 100644 (file)
@@ -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());