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