]> git.tdb.fi Git - libs/gl.git/blob - source/scene.cpp
Remove commented out debugging statements
[libs/gl.git] / source / scene.cpp
1 #include <msp/datafile/collection.h>
2 #include "animatedobject.h"
3 #include "camera.h"
4 #include "renderer.h"
5 #include "scene.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 Scene::~Scene()
13 {
14         for(vector<Renderable *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
15                 delete *i;
16 }
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 = 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
77 Scene::Loader::Loader(Scene &s, Collection &c):
78         DataFile::CollectionObjectLoader<Scene>(s, &c),
79         content(0)
80 {
81         init();
82 }
83
84 Scene::Loader::Loader(Scene &s, Collection &c, ContentMap &m):
85         DataFile::CollectionObjectLoader<Scene>(s, &c),
86         content(&m)
87 {
88         init();
89 }
90
91 void Scene::Loader::init()
92 {
93         add("object", &Loader::object);
94         add("object", &Loader::object_tagged);
95 }
96
97 void Scene::Loader::object(const string &n)
98 {
99         object_tagged(n, string());
100 }
101
102 void Scene::Loader::object_tagged(const string &n, const string &t)
103 {
104         RefPtr<AnimatedObject> anob = new AnimatedObject(get_collection().get<GL::Object>(n));
105         load_sub(*anob);
106         obj.add(*anob);
107         if(content && !t.empty())
108                 (*content)[t] = anob.get();
109         obj.owned_data.push_back(anob.release());
110 }
111
112 } // namespace GL
113 } // namespace Msp