]> git.tdb.fi Git - libs/gl.git/blobdiff - source/scene.cpp
Properly compute frustum culling for orthographic cameras
[libs/gl.git] / source / scene.cpp
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());