1 #include <msp/datafile/collection.h>
3 #include "objectinstance.h"
4 #include "orderedscene.h"
7 #include "simplescene.h"
8 #include "zsortedscene.h"
17 for(vector<Renderable *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
21 bool Scene::setup_frustum(const Renderer &renderer) const
23 const Camera *camera = renderer.get_camera();
27 culling_matrix = renderer.get_matrix();
29 if(camera->is_orthographic())
31 float h = camera->get_orthographic_height();
32 frustum_edges[0] = Vector4(0, 1, 0, -h);
33 frustum_edges[1] = Vector4(0, -1, 0, -h);
35 float w = camera->get_orthographic_width();
36 frustum_edges[2] = Vector4(1, 0, 0, -w);
37 frustum_edges[3] = Vector4(-1, 0, 0, -w);
41 float y = tan(camera->get_field_of_view()/2.0f);
42 float s = sqrt(y*y+1);
43 frustum_edges[0] = Vector4(0, 1/s, y/s, 0);
44 frustum_edges[1] = Vector4(0, -1/s, y/s, 0);
46 float x = y*camera->get_aspect_ratio();
48 frustum_edges[2] = Vector4(1/s, 0, x/s, 0);
49 frustum_edges[3] = Vector4(-1/s, 0, x/s, 0);
52 frustum_edges[4] = Vector4(0, 0, -1, -camera->get_far_clip());
53 frustum_edges[5] = Vector4(0, 0, 1, camera->get_near_clip());
58 bool Scene::frustum_cull(const Renderable &renderable) const
60 const Matrix *matrix = renderable.get_matrix();
61 const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
62 if(!matrix || !bsphere)
65 Vector4 center = culling_matrix*(*matrix*compose(bsphere->get_center(), 1.0f));
66 Vector3 x_axis = (matrix->column(0)*bsphere->get_radius()).slice<3>(0);
67 float radius_sq = inner_product(x_axis, x_axis);
69 for(unsigned i=0; i<6; ++i)
71 float distance = inner_product(center, frustum_edges[i]);
72 if(distance>0 && distance*distance>radius_sq)
79 Scene::SceneRegistry &Scene::get_scene_registry()
81 static SceneRegistry registry;
82 static bool initialized = false;
86 register_type<SimpleScene>("simple");
87 register_type<ZSortedScene>("zsorted");
88 register_type<OrderedScene>("ordered");
89 // TODO OccludedScene requires a collection as a constructor parameter
90 //register_type<OccludedScene>("occluded");
96 Scene::Loader::Loader(Scene &s, Collection &c):
97 DataFile::CollectionObjectLoader<Scene>(s, &c),
103 Scene::Loader::Loader(Scene &s, Collection &c, ContentMap &m):
104 DataFile::CollectionObjectLoader<Scene>(s, &c),
110 void Scene::Loader::init()
112 add("object", &Loader::object);
113 add("object", &Loader::object_tagged);
116 void Scene::Loader::object(const string &n)
118 object_tagged(n, string());
121 void Scene::Loader::object_tagged(const string &n, const string &t)
123 RefPtr<ObjectInstance> inst = new ObjectInstance(get_collection().get<GL::Object>(n));
126 if(content && !t.empty())
127 (*content)[t] = inst.get();
128 obj.owned_data.push_back(inst.release());
132 DataFile::Loader::ActionMap Scene::GenericLoader::shared_actions;
134 Scene::GenericLoader::GenericLoader(DataFile::Collection &c):
139 set_actions(shared_actions);
142 Scene::GenericLoader::~GenericLoader()
148 void Scene::GenericLoader::init_actions()
150 add("type", &GenericLoader::type);
153 void Scene::GenericLoader::type(const DataFile::Symbol &sym)
155 get_scene_registry().invoke(sym.name, *this);