]> git.tdb.fi Git - r2c2.git/blob - source/3d/path.cpp
Use event-based matrix updates for objects
[r2c2.git] / source / 3d / path.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/renderer.h>
3 #include "libr2c2/tracktype.h"
4 #include "layout.h"
5 #include "path.h"
6 #include "track.h"
7 #include "tracktype.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 Path3D::Path3D(const Track3D &t):
15         track(t),
16         paths(0),
17         automatic(true),
18         z_offs(0)
19 {
20         track.get_layout().get_path_scene().add(*this);
21 }
22
23 Path3D::~Path3D()
24 {
25         track.get_layout().get_path_scene().remove(*this);
26 }
27
28 void Path3D::set_automatic()
29 {
30         automatic = true;
31 }
32
33 void Path3D::set_path(unsigned p)
34 {
35         if(!(track.get_track().get_type().get_paths()&(1<<p)))
36                 throw invalid_argument("Path3D::set_path");
37         automatic = false;
38         paths = 1<<p;
39 }
40
41 void Path3D::set_mask(unsigned p)
42 {
43         if(p&~track.get_track().get_type().get_paths())
44                 throw invalid_argument("Path3D::set_mask");
45         automatic = false;
46         paths = p;
47 }
48
49 void Path3D::set_color(const GL::Color &c)
50 {
51         color = c;
52 }
53
54 void Path3D::set_layer(float l)
55 {
56         z_offs = l*track.get_track().get_layout().get_catalogue().get_gauge()*0.01;
57 }
58
59 long Path3D::get_instance_key() const
60 {
61         return reinterpret_cast<long>(&track.get_type());
62 }
63
64 void Path3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
65 {
66         if(tag=="unlit")
67         {
68                 unsigned mask = (automatic ? 1<<track.get_track().get_active_path() : paths);
69                 mask &= track.get_track().get_type().get_paths();
70                 if(!mask)
71                         return;
72
73                 GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
74                 renderer.matrix_stack() *= track.Object3D::get_matrix();
75                 renderer.matrix_stack() *= GL::Matrix::translation(0, 0, z_offs);
76
77                 glColor4f(color.r, color.g, color.b, color.a);
78                 for(unsigned i=0; mask; ++i, mask>>=1)
79                         if(mask&1)
80                                 track.get_type().get_path_mesh(i).draw(renderer);
81         }
82 }
83
84 } // namespace R2C2