]> git.tdb.fi Git - r2c2.git/blob - source/3d/view.cpp
Make use of the mspmath library
[r2c2.git] / source / 3d / view.cpp
1 #include <msp/gl/tests.h>
2 #include "layout.h"
3 #include "track.h"
4 #include "view.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 namespace R2C2 {
10
11 View3D::View3D(Layout3D &l, unsigned w, unsigned h):
12         layout(l),
13         pipeline(w, h)
14 {
15         pipeline.set_camera(&camera);
16         pipeline.add_renderable_for_pass(layout.get_scene(), 0);
17
18         GL::Pipeline::Pass *pass = &pipeline.add_pass(0);
19         pass->set_lighting(&layout.get_lighting());
20         pass->set_depth_test(&GL::DepthTest::lequal());
21
22         camera.set_up_direction(GL::Vector3(0, 0, 1));
23         // Y+, 60° down
24         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
25
26         view_all();
27 }
28
29 void View3D::view_all(bool tight)
30 {
31         Vector minp;
32         Vector maxp;
33
34         layout.get_bounds(minp, maxp);
35
36         float t = tan(camera.get_field_of_view()/2)*2;
37         float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
38         float dist = size/t;
39         if(!tight)
40                 dist += sin(camera.get_field_of_view()/2)*size;
41         GL::Vector3 center = (minp+maxp)/2.0f;
42         const GL::Vector3 &look = camera.get_look_direction();
43         camera.set_position(center-look*dist);
44 }
45
46 void View3D::render()
47 {
48         pipeline.render();
49 }
50
51 } // namespace R2C2