1 #include <msp/datafile/collection.h>
2 #include <msp/fs/utils.h>
4 #include "objectinstance.h"
5 #include "occludedscene.h"
6 #include "orderedscene.h"
9 #include "simplescene.h"
10 #include "zsortedscene.h"
17 bool Scene::setup_frustum(const Renderer &renderer) const
19 const Camera *camera = renderer.get_camera();
23 culling_matrix = camera->get_view_matrix()*renderer.get_matrix();
25 if(camera->is_orthographic())
27 float h = camera->get_orthographic_height();
28 frustum_edges[0] = Vector4(0, 1, 0, -h);
29 frustum_edges[1] = Vector4(0, -1, 0, -h);
31 float w = camera->get_orthographic_width();
32 frustum_edges[2] = Vector4(1, 0, 0, -w);
33 frustum_edges[3] = Vector4(-1, 0, 0, -w);
37 float y = tan(camera->get_field_of_view()/2.0f);
38 float s = sqrt(y*y+1);
39 frustum_edges[0] = Vector4(0, 1/s, y/s, 0);
40 frustum_edges[1] = Vector4(0, -1/s, y/s, 0);
42 float x = y*camera->get_aspect_ratio();
44 frustum_edges[2] = Vector4(1/s, 0, x/s, 0);
45 frustum_edges[3] = Vector4(-1/s, 0, x/s, 0);
48 frustum_edges[4] = Vector4(0, 0, -1, -camera->get_far_clip());
49 frustum_edges[5] = Vector4(0, 0, 1, camera->get_near_clip());
54 bool Scene::frustum_cull(const Renderable &renderable) const
56 const Matrix *matrix = renderable.get_matrix();
57 const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
58 if(!matrix || !bsphere)
61 Vector4 center = culling_matrix*(*matrix*compose(bsphere->get_center(), 1.0f));
62 Vector3 x_axis = (matrix->column(0)*bsphere->get_radius()).slice<3>(0);
63 float radius_sq = inner_product(x_axis, x_axis);
65 for(unsigned i=0; i<6; ++i)
67 float distance = inner_product(center, frustum_edges[i]);
68 if(distance>0 && distance*distance>radius_sq)
75 Scene::SceneRegistry &Scene::get_scene_registry()
77 static SceneRegistry registry;
78 static bool initialized = false;
82 register_type<SimpleScene>("simple");
83 register_type<ZSortedScene>("zsorted");
84 register_type<OrderedScene>("ordered");
85 register_type<OccludedScene>("occluded");
91 Scene::Loader::Loader(Scene &s, Collection &c):
92 DataFile::CollectionObjectLoader<Scene>(s, &c),
98 Scene::Loader::Loader(Scene &s, Collection &c, ContentMap &m):
99 DataFile::CollectionObjectLoader<Scene>(s, &c),
105 void Scene::Loader::init()
109 add("object", &Loader::object);
110 add("object", &Loader::object_tagged);
111 add("scene", &Loader::scene);
114 void Scene::Loader::object(const string &n)
116 object_tagged(n, string());
119 void Scene::Loader::object_tagged(const string &n, const string &t)
121 RefPtr<ObjectInstance> inst = new ObjectInstance(get_collection().get<GL::Object>(n));
123 get_collection().add(format("%s/%d.inst", FS::basename(get_source()), inst_counter++), inst.get());
124 if(content && !t.empty())
125 (*content)[t] = inst.get();
126 obj.add(*inst.release());
129 void Scene::Loader::scene(const string &n)
131 obj.add(get_collection().get<Scene>(n));
135 DataFile::Loader::ActionMap Scene::GenericLoader::shared_actions;
137 Scene::GenericLoader::GenericLoader(DataFile::Collection &c):
142 set_actions(shared_actions);
145 Scene::GenericLoader::~GenericLoader()
151 void Scene::GenericLoader::init_actions()
153 add("type", &GenericLoader::type);
156 void Scene::GenericLoader::type(const DataFile::Symbol &sym)
158 get_scene_registry().invoke(sym.name, *this);