]> git.tdb.fi Git - r2c2.git/blob - source/3d/view.cpp
673daef0c5ca4d7e2443ffeb9f127bfab451ea52
[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 {
18         pipeline.set_camera(&camera);
19         pipeline.add_renderable_for_pass(layout.get_scene(), 0);
20         pipeline.add_renderable_for_pass(layout.get_scene(), "translucent");
21
22         GL::Pipeline::Pass *pass = &pipeline.add_pass(0);
23         pass->set_lighting(&layout.get_lighting());
24         pass->set_depth_test(&GL::DepthTest::lequal());
25
26         pass = &pipeline.add_pass("translucent");
27         pass->set_lighting(&layout.get_lighting());
28         pass->set_depth_test(&GL::DepthTest::lequal());
29         pass->set_blend(&GL::Blend::alpha());
30
31         camera.set_up_direction(GL::Vector3(0, 0, 1));
32         // Y+, 60° down
33         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
34         camera.set_aspect(float(width)/height);
35
36         view_all();
37 }
38
39 Ray View3D::create_ray(int x, int y) const
40 {
41         return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
42 }
43
44 Ray View3D::create_ray(float x, float y) const
45 {
46         const GL::Vector3 &start = camera.get_position();
47         GL::Vector4 ray = camera.unproject(GL::Vector4(x, y, 0, 0));
48         return Ray(start, Vector(ray));
49 }
50
51 void View3D::view_all(bool tight)
52 {
53         const set<Track *> &tracks = layout.get_layout().get_all<Track>();
54         Geometry::BoundingBox<float, 3> bbox;
55         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
56                 bbox = bbox|(*i)->get_bounding_box();
57
58         const Vector &minp = bbox.get_minimum_point();
59         const Vector &maxp = bbox.get_maximum_point();
60
61         float t = tan(camera.get_field_of_view()/2.0f)*2.0f;
62         float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
63         float dist = size/t;
64         if(!tight)
65                 dist += sin(camera.get_field_of_view()/2.0f)*size;
66         GL::Vector3 center = (minp+maxp)/2.0f;
67         const GL::Vector3 &look = camera.get_look_direction();
68         camera.set_position(center-look*dist);
69 }
70
71 void View3D::render()
72 {
73         pipeline.render();
74 }
75
76 } // namespace R2C2