]> git.tdb.fi Git - libs/gl.git/blob - source/render/zsortedscene.cpp
Fix export of the stub technique
[libs/gl.git] / source / render / zsortedscene.cpp
1 #include "camera.h"
2 #include "renderer.h"
3 #include "zsortedscene.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 void ZSortedScene::add(Renderable &r)
11 {
12         if(renderables.insert(&r).second && !sorted_cache.empty())
13                 sorted_cache.push_back(&r);
14 }
15
16 void ZSortedScene::remove(Renderable &r)
17 {
18         renderables.erase(&r);
19         sorted_cache.clear();
20 }
21
22 void ZSortedScene::set_order(SortOrder o)
23 {
24         order = o;
25 }
26
27 void ZSortedScene::set_reference(DepthReference r)
28 {
29         reference = r;
30 }
31
32 void ZSortedScene::populate_cache() const
33 {
34         if(sorted_cache.empty() && !renderables.empty())
35         {
36                 sorted_cache.reserve(renderables.size());
37                 sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.end());
38         }
39 }
40
41 void ZSortedScene::setup_frame(Renderer &renderer)
42 {
43         populate_cache();
44         for(const SortedRenderable &r: sorted_cache)
45                 r.renderable->setup_frame(renderer);
46 }
47
48 void ZSortedScene::finish_frame()
49 {
50         for(const SortedRenderable &r: sorted_cache)
51                 r.renderable->finish_frame();
52 }
53
54 void ZSortedScene::render(Renderer &renderer, Tag tag) const
55 {
56         if(renderables.empty())
57                 return;
58
59         populate_cache();
60
61         const Camera *camera = renderer.get_camera();
62         if(!camera)
63         {
64                 for(const SortedRenderable &r: sorted_cache)
65                         renderer.render(*r.renderable, tag);
66                 return;
67         }
68
69         const Vector3 &camera_pos = camera->get_position();
70         const Vector3 &look_dir = camera->get_look_direction();
71         float radius_factor = reference-1.0f;
72         float sign = 1.0f-order*2.0f;
73
74         bool use_frustum = setup_frustum(renderer);
75         for(SortedRenderable &r: sorted_cache)
76         {
77                 r.in_frustum = (!use_frustum || !frustum_cull(*r.renderable));
78                 if(!r.in_frustum)
79                         continue;
80
81                 if(const Matrix *matrix = r.renderable->get_matrix())
82                 {
83                         if(const Geometry::BoundingSphere<float, 3> *bsphere = r.renderable->get_bounding_sphere())
84                                 r.depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
85                         else
86                                 r.depth = dot(*matrix*Vector3()-camera_pos, look_dir);
87                         r.depth *= sign;
88                 }
89                 else
90                         r.depth = 0;
91         }
92
93         for(auto i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
94                 if(i->in_frustum)
95                 {
96                         if(i!=j)
97                                 swap(*i, *j);
98
99                         if(j!=sorted_cache.begin() && *j<*(j-1))
100                         {
101                                 SortedRenderable sr = *j;
102                                 auto k = j-1;
103                                 *j = *k;
104                                 while(k!=sorted_cache.begin() && sr<*(k-1))
105                                 {
106                                         *k = *(k-1);
107                                         --k;
108                                 }
109                                 *k = sr;
110                         }
111
112                         ++j;
113                 }
114
115         for(auto i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
116                 renderer.render(*i->renderable, tag);
117 }
118
119
120 ZSortedScene::SortedRenderable::SortedRenderable(Renderable *r):
121         renderable(r),
122         in_frustum(false),
123         depth(0.0f)
124 { }
125
126 } // namespace GL
127 } // namespace Msp