]> git.tdb.fi Git - libs/gl.git/blob - scene.cpp
0f7737227e8da37c4ba69e0260d99f4e893b87c0
[libs/gl.git] / scene.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include "camera.h"
5 #include "objectinstance.h"
6 #include "occludedscene.h"
7 #include "orderedscene.h"
8 #include "renderer.h"
9 #include "scene.h"
10 #include "simplescene.h"
11 #include "zsortedscene.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 bool Scene::setup_frustum(const Renderer &renderer) const
19 {
20         const Camera *camera = renderer.get_camera();
21         if(!camera)
22                 return false;
23
24         culling_matrix = camera->get_view_matrix()*renderer.get_matrix();
25
26         if(camera->is_orthographic())
27         {
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);
31
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);
35         }
36         else
37         {
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);
42
43                 float x = y*camera->get_aspect_ratio();
44                 s = sqrt(x*x+1);
45                 frustum_edges[2] = Vector4(1/s, 0, x/s, 0);
46                 frustum_edges[3] = Vector4(-1/s, 0, x/s, 0);
47         }
48
49         frustum_edges[4] = Vector4(0, 0, -1, -camera->get_far_clip());
50         frustum_edges[5] = Vector4(0, 0, 1, camera->get_near_clip());
51
52         return true;
53 }
54
55 bool Scene::frustum_cull(const Renderable &renderable) const
56 {
57         const Matrix *matrix = renderable.get_matrix();
58         const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
59         if(!matrix || !bsphere)
60                 return false;
61
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);
65
66         for(unsigned i=0; i<6; ++i)
67         {
68                 float distance = inner_product(center, frustum_edges[i]);
69                 if(distance>0 && distance*distance>radius_sq)
70                         return true;
71         }
72
73         return false;
74 }
75
76 Scene::GenericLoader::TypeRegistry &Scene::get_scene_registry()
77 {
78         static Scene::GenericLoader::TypeRegistry registry;
79         static bool initialized = false;
80         if(!initialized)
81         {
82                 initialized = true;
83                 register_type<SimpleScene>("simple");
84                 register_type<ZSortedScene>("zsorted");
85                 register_type<OrderedScene>("ordered");
86                 register_type<OccludedScene>("occluded");
87         }
88         return registry;
89 }
90
91
92 unsigned Scene::Loader::inline_counter = 0;
93
94 Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m):
95         DataFile::CollectionObjectLoader<Scene>(s, &c),
96         content(m)
97 {
98         add("object", &Loader::object);
99         add("object", &Loader::object_tagged);
100         add("scene", &Loader::scene);
101         add("scene", &Loader::scene_inline);
102 }
103
104 void Scene::Loader::object(const string &n)
105 {
106         object_tagged(n, string());
107 }
108
109 void Scene::Loader::object_tagged(const string &n, const string &t)
110 {
111         RefPtr<ObjectInstance> inst = new ObjectInstance(get_collection().get<GL::Object>(n));
112         load_sub(*inst);
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());
117 }
118
119 void Scene::Loader::scene(const string &n)
120 {
121         obj.add(get_collection().get<Scene>(n));
122 }
123
124 void Scene::Loader::scene_inline()
125 {
126         GenericLoader ldr(get_collection());
127         load_sub_with(ldr);
128         Scene *scene = ldr.store_object(get_collection(), format("_scene_%d.scene", ++inline_counter));
129         obj.add(*scene);
130 }
131
132 } // namespace GL
133 } // namespace Msp