]> git.tdb.fi Git - r2c2.git/blob - source/3d/path.cpp
2e1521087da8e36ab10121ea512834d35fd23740
[r2c2.git] / source / 3d / path.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/renderer.h>
10 #include "libr2c2/tracktype.h"
11 #include "layout.h"
12 #include "path.h"
13 #include "track.h"
14 #include "tracktype.h"
15
16 using namespace Msp;
17
18 namespace R2C2 {
19
20 Path3D::Path3D(const Track3D &t):
21         track(t),
22         paths(0),
23         automatic(true),
24         z_offs(0)
25 {
26         track.get_layout().get_path_scene().add(*this);
27 }
28
29 Path3D::~Path3D()
30 {
31         track.get_layout().get_path_scene().remove(*this);
32 }
33
34 void Path3D::set_automatic()
35 {
36         automatic = true;
37 }
38
39 void Path3D::set_path(unsigned p)
40 {
41         if(!(track.get_track().get_type().get_paths()&(1<<p)))
42                 throw InvalidParameterValue("Invalid path");
43         automatic = false;
44         paths = 1<<p;
45 }
46
47 void Path3D::set_mask(unsigned p)
48 {
49         if(p&~track.get_track().get_type().get_paths())
50                 throw InvalidParameterValue("Invalid path mask");
51         automatic = false;
52         paths = p;
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_layout().get_catalogue().get_gauge()*0.01;
63 }
64
65 long Path3D::get_instance_key() const
66 {
67         return reinterpret_cast<long>(&track.get_type());
68 }
69
70 void Path3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
71 {
72         if(tag=="unlit")
73         {
74                 unsigned mask = (automatic ? 1<<track.get_track().get_active_path() : paths);
75                 mask &= track.get_track().get_type().get_paths();
76                 if(!mask)
77                         return;
78
79                 GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
80                 GL::Matrix matrix = track.get_matrix();
81                 matrix.translate(0, 0, z_offs);
82                 renderer.matrix_stack() *= matrix;
83
84                 glColor4f(color.r, color.g, color.b, color.a);
85                 for(unsigned i=0; mask; ++i, mask>>=1)
86                         if(mask&1)
87                                 track.get_type().get_path_mesh(i).draw(renderer);
88         }
89 }
90
91 } // namespace R2C2