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