]> git.tdb.fi Git - libs/gl.git/blob - source/scene.cpp
Add integer overload of ProgramData::uniform1_array
[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(list<Renderable *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
15                 delete *i;
16 }
17
18 void Scene::render(const Tag &tag) const
19 {
20         Renderer renderer(0);
21         render(renderer, tag);
22 }
23
24 bool Scene::setup_frustum(const Renderer &renderer) const
25 {
26         const Camera *camera = renderer.get_camera();
27         if(!camera)
28                 return false;
29
30         const Matrix &matrix = camera->get_object_matrix();
31
32         float y = tan(camera->get_field_of_view()/2.0f);
33         float s = sqrt(y*y+1);
34         frustum_edges[0] = matrix*Vector4(0, 1/s, y/s, 0);
35         frustum_edges[1] = matrix*Vector4(0, -1/s, y/s, 0);
36
37         float x = y*camera->get_aspect();
38         s = sqrt(x*x+1);
39         frustum_edges[2] = matrix*Vector4(1/s, 0, x/s, 0);
40         frustum_edges[3] = matrix*Vector4(-1/s, 0, x/s, 0);
41
42         const Vector3 &camera_pos = camera->get_position();
43         Vector4 pos4 = compose(camera_pos, 0.0f);
44         for(unsigned i=0; i<4; ++i)
45                 frustum_edges[i].w = -inner_product(frustum_edges[i], pos4);
46
47         const Vector3 &look_dir = camera->get_look_direction();
48         frustum_edges[4] = compose(look_dir, -dot(camera_pos, look_dir)-camera->get_far_clip());
49         frustum_edges[5] = compose(-look_dir, dot(camera_pos, look_dir)+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 = *matrix*compose(bsphere->get_center(), 1.0f);
62         float radius = bsphere->get_radius();
63
64         for(unsigned i=0; i<6; ++i)
65                 if(inner_product(center, frustum_edges[i])>radius)
66                         return true;
67
68         return false;
69 }
70
71
72 Scene::Loader::Loader(Scene &s, Collection &c):
73         DataFile::CollectionObjectLoader<Scene>(s, &c)
74 {
75         add("object", &Loader::object);
76 }
77
78 void Scene::Loader::object(const string &n)
79 {
80         RefPtr<AnimatedObject> anob = new AnimatedObject(get_collection().get<GL::Object>(n));
81         load_sub(*anob);
82         obj.add(*anob);
83         obj.owned_data.push_back(anob.release());
84 }
85
86 } // namespace GL
87 } // namespace Msp