]> git.tdb.fi Git - r2c2.git/blob - source/3d/path.cpp
8c36a3088f249f65305e9c13c5ea4e0a7f1d1e7f
[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         Utility3D(t.get_layout()),
16         track(t),
17         path(t.get_track().get_active_path()),
18         side(0),
19         automatic(true),
20         mesh(0),
21         z_offs(0)
22 {
23         update_mesh();
24
25         layout.get_path_scene().add(*this);
26         if(track.get_track().get_type().is_turnout())
27                 track.get_track().signal_path_changed.connect(sigc::mem_fun(this, &Path3D::path_changed));
28 }
29
30 Path3D::~Path3D()
31 {
32         layout.get_path_scene().remove(*this);
33 }
34
35 void Path3D::set_automatic()
36 {
37         automatic = true;
38 }
39
40 void Path3D::set_path(int p)
41 {
42         if(p>=0 && !(track.get_track().get_type().get_paths()&(1<<p)))
43                 throw invalid_argument("Path3D::set_path");
44         automatic = false;
45         path = p;
46         update_mesh();
47 }
48
49 void Path3D::set_side(int s)
50 {
51         side = (s<0 ? -1 : s>0 ? 1 : 0);
52         update_mesh();
53 }
54
55 void Path3D::set_color(const GL::Color &c)
56 {
57         color = c;
58 }
59
60 void Path3D::set_layer(float l)
61 {
62         z_offs = l*track.get_track().get_type().get_appearance().get_gauge()*0.01;
63 }
64
65 void Path3D::path_changed(unsigned p)
66 {
67         if(automatic)
68         {
69                 path = p;
70                 update_mesh();
71         }
72 }
73
74 void Path3D::update_mesh()
75 {
76         mesh = &track.get_type().get_path_mesh(path, side);
77 }
78
79 long Path3D::get_instance_key() const
80 {
81         return reinterpret_cast<long>(&track.get_type());
82 }
83
84 void Path3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
85 {
86         if(tag=="unlit")
87         {
88                 GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
89                 renderer.matrix_stack() *= track.Object3D::get_matrix();
90                 renderer.matrix_stack() *= GL::Matrix::translation(0, 0, z_offs);
91
92                 glColor4f(color.r, color.g, color.b, color.a);
93                 mesh->draw(renderer);
94         }
95 }
96
97 } // namespace R2C2