]> git.tdb.fi Git - libs/gl.git/blob - source/render/scene.cpp
Fix valgrind warnings
[libs/gl.git] / source / render / scene.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/fs/utils.h>
3 #include "camera.h"
4 #include "objectinstance.h"
5 #include "occludedscene.h"
6 #include "orderedscene.h"
7 #include "renderer.h"
8 #include "scene.h"
9 #include "simplescene.h"
10 #include "zsortedscene.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 bool Scene::setup_frustum(const Renderer &renderer) const
18 {
19         const Camera *camera = renderer.get_camera();
20         if(!camera)
21                 return false;
22
23         culling_matrix = camera->get_view_matrix()*renderer.get_matrix();
24
25         if(camera->is_orthographic())
26         {
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);
30
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);
34         }
35         else
36         {
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);
41
42                 float x = y*camera->get_aspect_ratio();
43                 s = sqrt(x*x+1);
44                 frustum_edges[2] = Vector4(1/s, 0, x/s, 0);
45                 frustum_edges[3] = Vector4(-1/s, 0, x/s, 0);
46         }
47
48         frustum_edges[4] = Vector4(0, 0, -1, -camera->get_far_clip());
49         frustum_edges[5] = Vector4(0, 0, 1, camera->get_near_clip());
50
51         return true;
52 }
53
54 bool Scene::frustum_cull(const Renderable &renderable) const
55 {
56         const Matrix *matrix = renderable.get_matrix();
57         const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
58         if(!matrix || !bsphere)
59                 return false;
60
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);
64
65         for(unsigned i=0; i<6; ++i)
66         {
67                 float distance = inner_product(center, frustum_edges[i]);
68                 if(distance>0 && distance*distance>radius_sq)
69                         return true;
70         }
71
72         return false;
73 }
74
75 Scene::SceneRegistry &Scene::get_scene_registry()
76 {
77         static SceneRegistry registry;
78         static bool initialized = false;
79         if(!initialized)
80         {
81                 initialized = true;
82                 register_type<SimpleScene>("simple");
83                 register_type<ZSortedScene>("zsorted");
84                 register_type<OrderedScene>("ordered");
85                 register_type<OccludedScene>("occluded");
86         }
87         return registry;
88 }
89
90
91 Scene::Loader::Loader(Scene &s, Collection &c):
92         DataFile::CollectionObjectLoader<Scene>(s, &c),
93         content(0)
94 {
95         init();
96 }
97
98 Scene::Loader::Loader(Scene &s, Collection &c, ContentMap &m):
99         DataFile::CollectionObjectLoader<Scene>(s, &c),
100         content(&m)
101 {
102         init();
103 }
104
105 void Scene::Loader::init()
106 {
107         inst_counter = 0;
108
109         add("object", &Loader::object);
110         add("object", &Loader::object_tagged);
111         add("scene", &Loader::scene);
112 }
113
114 void Scene::Loader::object(const string &n)
115 {
116         object_tagged(n, string());
117 }
118
119 void Scene::Loader::object_tagged(const string &n, const string &t)
120 {
121         RefPtr<ObjectInstance> inst = new ObjectInstance(get_collection().get<GL::Object>(n));
122         load_sub(*inst);
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());
127 }
128
129 void Scene::Loader::scene(const string &n)
130 {
131         obj.add(get_collection().get<Scene>(n));
132 }
133
134
135 DataFile::Loader::ActionMap Scene::GenericLoader::shared_actions;
136
137 Scene::GenericLoader::GenericLoader(DataFile::Collection &c):
138         coll(c),
139         scene(0),
140         scene_loader(0)
141 {
142         set_actions(shared_actions);
143 }
144
145 Scene::GenericLoader::~GenericLoader()
146 {
147         delete scene;
148         delete scene_loader;
149 }
150
151 void Scene::GenericLoader::init_actions()
152 {
153         add("type", &GenericLoader::type);
154 }
155
156 void Scene::GenericLoader::type(const DataFile::Symbol &sym)
157 {
158         get_scene_registry().invoke(sym.name, *this);
159 }
160
161 } // namespace GL
162 } // namespace Msp