void InstanceScene::render(Renderer &renderer, const Tag &tag) const
{
- setup_frustum(renderer);
- for(InstanceMap::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- for(RenderableSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
- if(!frustum_cull(renderer, **j))
+ if(setup_frustum(renderer))
+ {
+ for(InstanceMap::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ for(RenderableSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
+ if(!frustum_cull(**j))
+ renderer.render(**j, tag);
+ }
+ else
+ {
+ for(InstanceMap::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ for(RenderableSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
renderer.render(**j, tag);
+ }
}
} // namespace GL
void OrderedScene::render(Renderer &renderer, const Tag &tag) const
{
- setup_frustum(renderer);
- for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(!frustum_cull(renderer, **i))
+ if(setup_frustum(renderer))
+ {
+ for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ if(!frustum_cull(**i))
+ renderer.render(**i, tag);
+ }
+ else
+ {
+ for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
renderer.render(**i, tag);
+ }
}
} // namespace GL
render(renderer, tag);
}
-void Scene::setup_frustum(const Renderer &renderer) const
+bool Scene::setup_frustum(const Renderer &renderer) const
{
const Camera *camera = renderer.get_camera();
if(!camera)
- return;
+ return false;
+
+ const Matrix &matrix = camera->get_object_matrix();
float y = tan(camera->get_field_of_view()/2.0f);
float s = sqrt(y*y+1);
- frustum_edges[0] = Vector3(0, 1/s, y/s);
- frustum_edges[1] = Vector3(0, -1/s, y/s);
+ frustum_edges[0] = matrix*Vector4(0, 1/s, y/s, 0);
+ frustum_edges[1] = matrix*Vector4(0, -1/s, y/s, 0);
float x = y*camera->get_aspect();
s = sqrt(x*x+1);
- frustum_edges[2] = Vector3(1/s, 0, x/s);
- frustum_edges[3] = Vector3(-1/s, 0, x/s);
+ frustum_edges[2] = matrix*Vector4(1/s, 0, x/s, 0);
+ frustum_edges[3] = matrix*Vector4(-1/s, 0, x/s, 0);
+
+ const Vector3 &camera_pos = camera->get_position();
+ Vector4 pos4 = compose(camera_pos, 0.0f);
+ for(unsigned i=0; i<4; ++i)
+ frustum_edges[i].w = -inner_product(frustum_edges[i], pos4);
+
+ const Vector3 &look_dir = camera->get_look_direction();
+ frustum_edges[4] = compose(look_dir, -dot(camera_pos, look_dir)-camera->get_far_clip());
+ frustum_edges[5] = compose(-look_dir, dot(camera_pos, look_dir)+camera->get_near_clip());
+
+ return true;
}
-bool Scene::frustum_cull(const Renderer &renderer, const Renderable &renderable) const
+bool Scene::frustum_cull(const Renderable &renderable) const
{
const Matrix *matrix = renderable.get_matrix();
const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
- const Camera *camera = renderer.get_camera();
- if(!matrix || !bsphere || !camera)
+ if(!matrix || !bsphere)
return false;
- Matrix mvm = camera->get_matrix()**matrix;
- float n = camera->get_near_clip();
- float f = camera->get_far_clip();
-
- Vector3 center = mvm*bsphere->get_center();
+ Vector4 center = *matrix*compose(bsphere->get_center(), 1.0f);
float radius = bsphere->get_radius();
- if(center.z-radius>-n || center.z+radius<-f)
- return true;
- for(unsigned i=0; i<4; ++i)
- if(dot(center, frustum_edges[i])>radius)
+ for(unsigned i=0; i<6; ++i)
+ if(inner_product(center, frustum_edges[i])>radius)
return true;
return false;
void SimpleScene::render(Renderer &renderer, const Tag &tag) const
{
- setup_frustum(renderer);
- for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(!frustum_cull(renderer, **i))
+ if(setup_frustum(renderer))
+ {
+ for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ if(!frustum_cull(**i))
+ renderer.render(**i, tag);
+ }
+ else
+ {
+ for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
renderer.render(**i, tag);
+ }
}
} // namespace GL