]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
210791feeaf6c77fd0bab3ed96172ef3ce741963
[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         Object3D(t),
18         GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
19         layout(l),
20         track(t),
21         type(layout.get_catalogue().get_track(track.get_type())),
22         path(new Path3D(*this))
23 {
24         layout.add_track(*this);
25         layout.get_scene().add(*this);
26
27         const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
28         const vector<Track *> &links = track.get_links();
29         for(unsigned i=0; i<type_eps.size(); ++i)
30         {
31                 if(!links[i] || links[i]>&track)
32                         endpoints.push_back(new Endpoint3D(*this, i));
33                 else
34                         endpoints.push_back(0);
35         }
36
37         track.signal_link_changed.connect(sigc::mem_fun(this, &Track3D::link_changed));
38 }
39
40 Track3D::~Track3D()
41 {
42         delete path;
43
44         layout.remove_track(*this);
45         layout.get_scene().remove(*this);
46
47         for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
48                 delete *i;
49 }
50
51 Vector Track3D::get_node() const
52 {
53         Geometry::BoundingBox<float, 3> bbox = track.get_type().get_shape()->get_axis_aligned_bounding_box();
54         const Vector &minp = bbox.get_minimum_point();
55         const Vector &maxp = bbox.get_maximum_point();
56
57         return matrix*((minp+maxp)/2.0f+Vector(0, 0, 0.02));
58 }
59
60 void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
61 {
62         renderer.matrix_stack() *= matrix;
63 }
64
65 void Track3D::link_changed(unsigned i, Track *trk)
66 {
67         if(!trk || trk>&track)
68         {
69                 if(!endpoints[i])
70                         endpoints[i] = new Endpoint3D(*this, i);
71         }
72         else if(endpoints[i])
73         {
74                 delete endpoints[i];
75                 endpoints[i] = 0;
76         }
77 }
78
79 } // namespace R2C2