]> git.tdb.fi Git - libs/gl.git/blob - source/zsortedscene.cpp
Add a new transform API to Renderer.
[libs/gl.git] / source / 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 ZSortedScene::ZSortedScene():
11         order(BACK_TO_FRONT),
12         reference(FURTHEST)
13 { }
14
15 void ZSortedScene::set_order(SortOrder o)
16 {
17         order = o;
18 }
19
20 void ZSortedScene::set_reference(DepthReference r)
21 {
22         reference = r;
23 }
24
25 void ZSortedScene::render(Renderer &renderer, const Tag &tag) const
26 {
27         const Camera *camera = renderer.get_camera();
28         if(!camera)
29                 return SimpleScene::render(renderer, tag);
30
31         const Matrix &view_matrix = camera->get_matrix();
32
33         std::vector<DepthRenderable> sorted_renderables;
34         sorted_renderables.reserve(renderables.size());
35
36         float radius_factor = 1.0f-reference;
37         float sign = order*2.0f-1.0f;
38
39         setup_cullers(renderer);
40         for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
41                 if(!cull(renderer, **i))
42                 {
43                         float z = 0;
44                         if(const Matrix *model_matrix = (*i)->get_matrix())
45                         {
46                                 if(const Geometry::BoundingSphere<float, 3> *bsphere = (*i)->get_bounding_sphere())
47                                         z = (view_matrix**model_matrix*compose(bsphere->get_center(), 1.0f)).z+bsphere->get_radius()*radius_factor;
48                                 else
49                                         z = (view_matrix**model_matrix*Vector4(0.0f, 0.0f, 0.0f, 1.0f)).z;
50                         }
51                         sorted_renderables.push_back(DepthRenderable(z*sign, *i));
52                 }
53
54         sort(sorted_renderables.begin(), sorted_renderables.end());
55
56         for(vector<DepthRenderable>::const_iterator i=sorted_renderables.begin(); i!=sorted_renderables.end(); ++i)
57                 i->renderable->render(renderer, tag);
58 }
59
60
61 ZSortedScene::DepthRenderable::DepthRenderable(float d, const Renderable *r):
62         depth(d),
63         renderable(r)
64 { }
65
66 } // namespace GL
67 } // namespace Msp