]> git.tdb.fi Git - r2c2.git/blob - source/3d/path.cpp
Strip Id tags and copyright notices from files
[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 Msp;
10
11 namespace R2C2 {
12
13 Path3D::Path3D(const Track3D &t):
14         track(t),
15         paths(0),
16         automatic(true),
17         z_offs(0)
18 {
19         track.get_layout().get_path_scene().add(*this);
20 }
21
22 Path3D::~Path3D()
23 {
24         track.get_layout().get_path_scene().remove(*this);
25 }
26
27 void Path3D::set_automatic()
28 {
29         automatic = true;
30 }
31
32 void Path3D::set_path(unsigned p)
33 {
34         if(!(track.get_track().get_type().get_paths()&(1<<p)))
35                 throw InvalidParameterValue("Invalid path");
36         automatic = false;
37         paths = 1<<p;
38 }
39
40 void Path3D::set_mask(unsigned p)
41 {
42         if(p&~track.get_track().get_type().get_paths())
43                 throw InvalidParameterValue("Invalid path mask");
44         automatic = false;
45         paths = p;
46 }
47
48 void Path3D::set_color(const GL::Color &c)
49 {
50         color = c;
51 }
52
53 void Path3D::set_layer(float l)
54 {
55         z_offs = l*track.get_track().get_layout().get_catalogue().get_gauge()*0.01;
56 }
57
58 long Path3D::get_instance_key() const
59 {
60         return reinterpret_cast<long>(&track.get_type());
61 }
62
63 void Path3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
64 {
65         if(tag=="unlit")
66         {
67                 unsigned mask = (automatic ? 1<<track.get_track().get_active_path() : paths);
68                 mask &= track.get_track().get_type().get_paths();
69                 if(!mask)
70                         return;
71
72                 GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
73                 GL::Matrix matrix = track.get_matrix();
74                 matrix.translate(0, 0, z_offs);
75                 renderer.matrix_stack() *= matrix;
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