]> git.tdb.fi Git - r2c2.git/blob - source/3d/endpoint.cpp
Don't crash if a train has no router
[r2c2.git] / source / 3d / endpoint.cpp
1 #include <cmath>
2 #include <msp/gl/matrix.h>
3 #include <msp/gl/misc.h>
4 #include <msp/gl/renderer.h>
5 #include "endpoint.h"
6 #include "layout.h"
7 #include "track.h"
8
9 using namespace Msp;
10
11 namespace R2C2 {
12
13 Endpoint3D::Endpoint3D(const Track3D &t, unsigned i):
14         track(t),
15         index(i),
16         mesh(track.get_layout().get_catalogue().get_endpoint_mesh(track.get_track().get_type().get_appearance()))
17 {
18         track.get_layout().get_endpoint_scene().add(*this);
19 }
20
21 Endpoint3D::~Endpoint3D()
22 {
23         track.get_layout().get_endpoint_scene().remove(*this);
24 }
25
26 void Endpoint3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
27 {
28         if(tag=="unlit")
29         {
30                 Snap sn = track.get_track().get_snap_node(index);
31
32                 GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
33                 GL::Matrix matrix;
34                 matrix.translate(sn.position);
35                 matrix.rotate(sn.rotation+Angle::half_turn(), 0, 0, 1);
36                 renderer.matrix_stack() *= matrix;
37
38                 if(track.get_track().get_link(index))
39                         glColor4f(0.5, 0, 1, 0.5);
40                 else
41                         glColor4f(1, 0, 0.5, 0.5);
42
43                 mesh.draw(renderer);
44         }
45 }
46
47 } // namespace R2C2