2 #include "frustumculler.h"
3 #include "renderable.h"
6 #include <msp/io/print.h>
11 void FrustumCuller::setup_pass(const Renderer &renderer)
13 const Camera *camera = renderer.get_camera();
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);
22 float x = y*camera->get_aspect();
24 edges[2] = Vector3(1/s, 0, x/s);
25 edges[3] = Vector3(-1/s, 0, x/s);
28 bool FrustumCuller::cull(const Renderer &renderer, const Renderable &renderable) const
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)
36 Matrix mvm = camera->get_matrix()**matrix;
37 float n = camera->get_near_clip();
38 float f = camera->get_far_clip();
40 Vector3 center = mvm*bsphere->get_center();
41 float radius = bsphere->get_radius();
43 if(center.z-radius>-n || center.z+radius<-f)
45 for(unsigned i=0; i<4; ++i)
46 if(dot(center, edges[i])>radius)