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