]> git.tdb.fi Git - r2c2.git/blob - source/3d/view.cpp
Only use postprocessors that can be supported
[r2c2.git] / source / 3d / view.cpp
1 #include <msp/gl/blend.h>
2 #include <msp/gl/tests.h>
3 #include "layout.h"
4 #include "track.h"
5 #include "view.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 namespace R2C2 {
11
12 View3D::View3D(Layout3D &l, unsigned w, unsigned h):
13         layout(l),
14         width(w),
15         height(h),
16         pipeline(w, h),
17         sky(layout.get_catalogue()),
18         shadow(4096, layout.get_scene(), layout.get_sun())
19 {
20         pipeline.set_hdr(true);
21         pipeline.set_camera(&camera);
22         pipeline.add_renderable(sky);
23         pipeline.add_renderable_for_pass(shadow, 0);
24         pipeline.add_renderable_for_pass(layout.get_scene(), "translucent");
25
26         GL::Pipeline::Pass *pass = &pipeline.add_pass("sky");
27         pass->set_lighting(&layout.get_lighting());
28
29         pass = &pipeline.add_pass(0);
30         pass->set_lighting(&layout.get_lighting());
31         pass->set_depth_test(&GL::DepthTest::lequal());
32
33         pass = &pipeline.add_pass("translucent");
34         pass->set_lighting(&layout.get_lighting());
35         pass->set_depth_test(&GL::DepthTest::lequal());
36         pass->set_blend(&GL::Blend::alpha());
37
38         try
39         {
40                 ambient_occlusion = new GL::AmbientOcclusion(w, h, 100);
41                 pipeline.add_postprocessor(*ambient_occlusion);
42         }
43         catch(...)
44         {
45                 delete ambient_occlusion;
46                 ambient_occlusion = 0;
47         }
48
49         try
50         {
51                 colorcurve = new GL::ColorCurve;
52                 colorcurve->set_srgb();
53                 pipeline.add_postprocessor(*colorcurve);
54         }
55         catch(...)
56         {
57                 delete colorcurve;
58                 colorcurve = 0;
59         }
60
61         update_shadow_area();
62         layout.get_layout().signal_object_added.connect(sigc::hide(sigc::mem_fun(this, &View3D::update_shadow_area)));
63
64         camera.set_up_direction(GL::Vector3(0, 0, 1));
65         // Y+, 60° down
66         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
67         camera.set_aspect(float(width)/height);
68
69         view_all();
70 }
71
72 View3D::~View3D()
73 {
74         delete colorcurve;
75         delete ambient_occlusion;
76 }
77
78 Ray View3D::create_ray(int x, int y) const
79 {
80         return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
81 }
82
83 Ray View3D::create_ray(float x, float y) const
84 {
85         const GL::Vector3 &start = camera.get_position();
86         GL::Vector4 ray = camera.unproject(GL::Vector4(x, y, 0, 0));
87         return Ray(start, ray.slice<3>(0));
88 }
89
90 void View3D::compute_bounds(Vector &minp, Vector &maxp)
91 {
92         const set<Object *> &objects = layout.get_layout().get_all<Object>();
93         Geometry::BoundingBox<float, 3> bbox;
94         for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
95                 bbox = bbox|(*i)->get_bounding_box();
96
97         minp = bbox.get_minimum_point();
98         maxp = bbox.get_maximum_point();
99 }
100
101 void View3D::view_all(bool tight)
102 {
103         Vector minp, maxp;
104         compute_bounds(minp, maxp);
105
106         float t = tan(camera.get_field_of_view()/2.0f)*2.0f;
107         float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
108         float dist = size/t;
109         if(!tight)
110                 dist += sin(camera.get_field_of_view()/2.0f)*size;
111         GL::Vector3 center = (minp+maxp)/2.0f;
112         const GL::Vector3 &look = camera.get_look_direction();
113         camera.set_position(center-look*dist);
114         camera.set_depth_clip(dist*0.02, dist*50);
115 }
116
117 void View3D::render()
118 {
119         pipeline.render();
120 }
121
122 void View3D::update_shadow_area()
123 {
124         Vector minp, maxp;
125         compute_bounds(minp, maxp);
126         shadow.set_target((minp+maxp)/2.0f, max((maxp-minp).norm()/2.0f, 0.1f));
127 }
128
129 } // namespace R2C2