1 #include <msp/datafile/collection.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
5 #include "objectinstance.h"
6 #include "occludedscene.h"
7 #include "orderedscene.h"
10 #include "simplescene.h"
11 #include "zsortedscene.h"
18 bool Scene::setup_frustum(const Renderer &renderer) const
20 const Camera *camera = renderer.get_camera();
24 culling_matrix = camera->get_view_matrix()*renderer.get_matrix();
26 if(camera->is_orthographic())
28 float h = camera->get_orthographic_height();
29 frustum_edges[0] = Vector4(0, 1, 0, -h);
30 frustum_edges[1] = Vector4(0, -1, 0, -h);
32 float w = camera->get_orthographic_width();
33 frustum_edges[2] = Vector4(1, 0, 0, -w);
34 frustum_edges[3] = Vector4(-1, 0, 0, -w);
38 float y = tan(camera->get_field_of_view()/2.0f);
39 float s = sqrt(y*y+1);
40 frustum_edges[0] = Vector4(0, 1/s, y/s, 0);
41 frustum_edges[1] = Vector4(0, -1/s, y/s, 0);
43 float x = y*camera->get_aspect_ratio();
45 frustum_edges[2] = Vector4(1/s, 0, x/s, 0);
46 frustum_edges[3] = Vector4(-1/s, 0, x/s, 0);
49 frustum_edges[4] = Vector4(0, 0, -1, -camera->get_far_clip());
50 frustum_edges[5] = Vector4(0, 0, 1, camera->get_near_clip());
55 bool Scene::frustum_cull(const Renderable &renderable) const
57 const Matrix *matrix = renderable.get_matrix();
58 const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
59 if(!matrix || !bsphere)
62 Vector4 center = culling_matrix*(*matrix*compose(bsphere->get_center(), 1.0f));
63 Vector3 x_axis = (matrix->column(0)*bsphere->get_radius()).slice<3>(0);
64 float radius_sq = inner_product(x_axis, x_axis);
66 for(unsigned i=0; i<6; ++i)
68 float distance = inner_product(center, frustum_edges[i]);
69 if(distance>0 && distance*distance>radius_sq)
76 Scene::GenericLoader::TypeRegistry &Scene::get_scene_registry()
78 static Scene::GenericLoader::TypeRegistry registry;
79 static bool initialized = false;
83 register_type<SimpleScene>("simple");
84 register_type<ZSortedScene>("zsorted");
85 register_type<OrderedScene>("ordered");
86 register_type<OccludedScene>("occluded");
92 unsigned Scene::Loader::inline_counter = 0;
94 Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m):
95 DataFile::CollectionObjectLoader<Scene>(s, &c),
98 add("object", &Loader::object);
99 add("object", &Loader::object_tagged);
100 add("scene", &Loader::scene);
101 add("scene", &Loader::scene_inline);
104 void Scene::Loader::object(const string &n)
106 object_tagged(n, string());
109 void Scene::Loader::object_tagged(const string &n, const string &t)
111 RefPtr<ObjectInstance> inst = new ObjectInstance(get_collection().get<GL::Object>(n));
113 get_collection().add(format("_scene_object_%d.inst", ++inline_counter), inst.get());
114 if(content && !t.empty())
115 (*content)[t] = inst.get();
116 obj.add(*inst.release());
119 void Scene::Loader::scene(const string &n)
121 obj.add(get_collection().get<Scene>(n));
124 void Scene::Loader::scene_inline()
126 GenericLoader ldr(get_collection());
128 Scene *scene = ldr.store_object(get_collection(), format("_scene_%d.scene", ++inline_counter));