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