]> git.tdb.fi Git - r2c2.git/blob - source/3d/view.cpp
8c35245b4d97dd1b6d6c27147fc9494d6827ebd8
[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         ambient_occlusion(w, h, 100)
20 {
21         pipeline.set_hdr(true);
22         pipeline.set_camera(&camera);
23         pipeline.add_renderable(sky);
24         pipeline.add_renderable_for_pass(shadow, 0);
25         pipeline.add_renderable_for_pass(layout.get_scene(), "translucent");
26
27         GL::Pipeline::Pass *pass = &pipeline.add_pass("sky");
28         pass->set_lighting(&layout.get_lighting());
29
30         pass = &pipeline.add_pass(0);
31         pass->set_lighting(&layout.get_lighting());
32         pass->set_depth_test(&GL::DepthTest::lequal());
33
34         pass = &pipeline.add_pass("translucent");
35         pass->set_lighting(&layout.get_lighting());
36         pass->set_depth_test(&GL::DepthTest::lequal());
37         pass->set_blend(&GL::Blend::alpha());
38
39         pipeline.add_postprocessor(ambient_occlusion);
40         colorcurve.set_srgb();
41         pipeline.add_postprocessor(colorcurve);
42
43         update_shadow_area();
44         layout.get_layout().signal_object_added.connect(sigc::hide(sigc::mem_fun(this, &View3D::update_shadow_area)));
45
46         camera.set_up_direction(GL::Vector3(0, 0, 1));
47         // Y+, 60° down
48         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
49         camera.set_aspect(float(width)/height);
50
51         view_all();
52 }
53
54 Ray View3D::create_ray(int x, int y) const
55 {
56         return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
57 }
58
59 Ray View3D::create_ray(float x, float y) const
60 {
61         const GL::Vector3 &start = camera.get_position();
62         GL::Vector4 ray = camera.unproject(GL::Vector4(x, y, 0, 0));
63         return Ray(start, Vector(ray));
64 }
65
66 void View3D::compute_bounds(Vector &minp, Vector &maxp)
67 {
68         const set<Object *> &objects = layout.get_layout().get_all<Object>();
69         Geometry::BoundingBox<float, 3> bbox;
70         for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
71                 bbox = bbox|(*i)->get_bounding_box();
72
73         minp = bbox.get_minimum_point();
74         maxp = bbox.get_maximum_point();
75 }
76
77 void View3D::view_all(bool tight)
78 {
79         Vector minp, maxp;
80         compute_bounds(minp, maxp);
81
82         float t = tan(camera.get_field_of_view()/2.0f)*2.0f;
83         float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
84         float dist = size/t;
85         if(!tight)
86                 dist += sin(camera.get_field_of_view()/2.0f)*size;
87         GL::Vector3 center = (minp+maxp)/2.0f;
88         const GL::Vector3 &look = camera.get_look_direction();
89         camera.set_position(center-look*dist);
90         camera.set_depth_clip(dist*0.02, dist*50);
91 }
92
93 void View3D::render()
94 {
95         pipeline.render();
96 }
97
98 void View3D::update_shadow_area()
99 {
100         Vector minp, maxp;
101         compute_bounds(minp, maxp);
102         shadow.set_target((minp+maxp)/2.0f, (maxp-minp).norm()/2.0f);
103 }
104
105 } // namespace R2C2