]> git.tdb.fi Git - r2c2.git/blob - source/3d/view.cpp
Improve graphics quality with some shaders and effects
[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
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         pipeline.add_postprocessor(ambient_occlusion);
39         colorcurve.set_srgb();
40         pipeline.add_postprocessor(colorcurve);
41
42         update_shadow_area();
43         layout.get_layout().signal_object_added.connect(sigc::hide(sigc::mem_fun(this, &View3D::update_shadow_area)));
44
45         camera.set_up_direction(GL::Vector3(0, 0, 1));
46         // Y+, 60° down
47         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
48         camera.set_aspect(float(width)/height);
49
50         view_all();
51 }
52
53 Ray View3D::create_ray(int x, int y) const
54 {
55         return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
56 }
57
58 Ray View3D::create_ray(float x, float y) const
59 {
60         const GL::Vector3 &start = camera.get_position();
61         GL::Vector4 ray = camera.unproject(GL::Vector4(x, y, 0, 0));
62         return Ray(start, Vector(ray));
63 }
64
65 void View3D::compute_bounds(Vector &minp, Vector &maxp)
66 {
67         const set<Object *> &objects = layout.get_layout().get_all<Object>();
68         Geometry::BoundingBox<float, 3> bbox;
69         for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
70                 bbox = bbox|(*i)->get_bounding_box();
71
72         minp = bbox.get_minimum_point();
73         maxp = bbox.get_maximum_point();
74 }
75
76 void View3D::view_all(bool tight)
77 {
78         Vector minp, maxp;
79         compute_bounds(minp, maxp);
80
81         float t = tan(camera.get_field_of_view()/2.0f)*2.0f;
82         float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
83         float dist = size/t;
84         if(!tight)
85                 dist += sin(camera.get_field_of_view()/2.0f)*size;
86         GL::Vector3 center = (minp+maxp)/2.0f;
87         const GL::Vector3 &look = camera.get_look_direction();
88         camera.set_position(center-look*dist);
89         camera.set_depth_clip(dist*0.02, dist*50);
90 }
91
92 void View3D::render()
93 {
94         pipeline.render();
95 }
96
97 void View3D::update_shadow_area()
98 {
99         Vector minp, maxp;
100         compute_bounds(minp, maxp);
101         shadow.set_target((minp+maxp)/2.0f, (maxp-minp).norm()/2.0f);
102 }
103
104 } // namespace R2C2