]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
Make use of the mspmath library
[r2c2.git] / source / 3d / track.cpp
1 #include <cmath>
2 #include <msp/gl/matrix.h>
3 #include <msp/gl/renderer.h>
4 #include "libr2c2/tracktype.h"
5 #include "endpoint.h"
6 #include "layout.h"
7 #include "path.h"
8 #include "track.h"
9 #include "tracktype.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 Track3D::Track3D(Layout3D &l, Track &t):
17         GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
18         layout(l),
19         track(t),
20         type(layout.get_catalogue().get_track(track.get_type())),
21         path(new Path3D(*this))
22 {
23         layout.add_track(*this);
24         layout.get_scene().add(*this);
25
26         const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
27         const vector<Track *> &links = track.get_links();
28         for(unsigned i=0; i<type_eps.size(); ++i)
29         {
30                 if(!links[i] || links[i]>&track)
31                         endpoints.push_back(new Endpoint3D(*this, i));
32                 else
33                         endpoints.push_back(0);
34         }
35
36         track.signal_link_changed.connect(sigc::mem_fun(this, &Track3D::link_changed));
37 }
38
39 Track3D::~Track3D()
40 {
41         delete path;
42
43         layout.remove_track(*this);
44         layout.get_scene().remove(*this);
45
46         for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
47                 delete *i;
48 }
49
50 void Track3D::get_bounds(const Angle &angle, Vector &minp, Vector &maxp) const
51 {
52         type.get_bounds(angle-track.get_rotation(), minp, maxp);
53
54         Vector pos = rotated_vector(track.get_position(), -angle);
55         minp += pos;
56         maxp += pos;
57
58         float slope = track.get_slope();
59         if(slope>0)
60                 maxp.z += slope;
61         else
62                 minp.z += slope;
63 }
64
65 Vector Track3D::get_node() const
66 {
67         Vector minp;
68         Vector maxp;
69         type.get_bounds(Angle::zero(), minp, maxp);
70
71         return track.get_position()+rotated_vector((minp+maxp)/2.0f, track.get_rotation())+Vector(0, 0, 0.02);
72 }
73
74 GL::Matrix Track3D::create_matrix() const
75 {
76         GL::Matrix matrix;
77         matrix.translate(track.get_position());
78         matrix.rotate(track.get_rotation(), 0, 0, 1);
79         matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0);
80
81         return matrix;
82 }
83
84 void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
85 {
86         renderer.matrix_stack() *= create_matrix();
87 }
88
89 void Track3D::link_changed(unsigned i, Track *trk)
90 {
91         if(!trk || trk>&track)
92         {
93                 if(!endpoints[i])
94                         endpoints[i] = new Endpoint3D(*this, i);
95         }
96         else if(endpoints[i])
97         {
98                 delete endpoints[i];
99                 endpoints[i] = 0;
100         }
101 }
102
103 } // namespace R2C2