]> git.tdb.fi Git - r2c2.git/blob - source/3d/view.cpp
Add View3D class to bundle Layout3D with a camera and a pipeline
[r2c2.git] / source / 3d / view.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gl/tests.h>
9 #include "layout.h"
10 #include "track.h"
11 #include "view.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace R2C2 {
17
18 View3D::View3D(Layout3D &l, unsigned w, unsigned h):
19         layout(l),
20         pipeline(w, h)
21 {
22         pipeline.set_camera(&camera);
23         pipeline.add_renderable_for_pass(layout.get_scene(), 0);
24
25         GL::PipelinePass *pass = &pipeline.add_pass(0);
26         pass->lighting = &layout.get_lighting();
27         pass->depth_test = &GL::DepthTest::lequal();
28
29         camera.set_up_direction(GL::Vector3(0, 0, 1));
30         // Y+, 60° down
31         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
32
33         view_all();
34 }
35
36 void View3D::view_all(bool tight)
37 {
38         Vector minp;
39         Vector maxp;
40
41         layout.get_bounds(minp, maxp);
42
43         float t = tan(camera.get_field_of_view()/2)*2;
44         float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
45         float dist = size/t;
46         if(!tight)
47                 dist += sin(camera.get_field_of_view()/2)*size;
48         GL::Vector3 center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
49         const GL::Vector3 &look = camera.get_look_direction();
50         camera.set_position(GL::Vector3(center.x-look.x*dist, center.y-look.y*dist, center.z-look.z*dist));
51 }
52
53 void View3D::render()
54 {
55         pipeline.render_all();
56 }
57
58 } // namespace R2C2