]> git.tdb.fi Git - libs/gl.git/blob - source/frustumculler.cpp
Rename Culler::setup_frame to setup_pass
[libs/gl.git] / source / frustumculler.cpp
1 #include "camera.h"
2 #include "frustumculler.h"
3 #include "renderable.h"
4 #include "renderer.h"
5
6 #include <msp/io/print.h>
7
8 namespace Msp {
9 namespace GL {
10
11 void FrustumCuller::setup_pass(const Renderer &renderer)
12 {
13         const Camera *camera = renderer.get_camera();
14         if(!camera)
15                 return;
16
17         float y = tan(camera->get_field_of_view()/2.0f);
18         float s = sqrt(y*y+1);
19         edges[0] = Vector3(0, 1/s, y/s);
20         edges[1] = Vector3(0, -1/s, y/s);
21
22         float x = y*camera->get_aspect();
23         s = sqrt(x*x+1);
24         edges[2] = Vector3(1/s, 0, x/s);
25         edges[3] = Vector3(-1/s, 0, x/s);
26 }
27
28 bool FrustumCuller::cull(const Renderer &renderer, const Renderable &renderable) const
29 {
30         const Matrix *matrix = renderable.get_matrix();
31         const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
32         const Camera *camera = renderer.get_camera();
33         if(!matrix || !bsphere || !camera)
34                 return false;
35
36         Matrix mvm = camera->get_matrix()**matrix;
37         float n = camera->get_near_clip();
38         float f = camera->get_far_clip();
39
40         Vector3 center = mvm*bsphere->get_center();
41         float radius = bsphere->get_radius();
42
43         if(center.z-radius>-n || center.z+radius<-f)
44                 return true;
45         for(unsigned i=0; i<4; ++i)
46                 if(dot(center, edges[i])>radius)
47                         return true;
48
49         return false;
50 }
51
52 } // namespace GL
53 } // namespace Msp