]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
Rename Point to Vector
[r2c2.git] / source / 3d / track.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/misc.h>
11 #include <msp/gl/renderer.h>
12 #include "libr2c2/tracktype.h"
13 #include "endpoint.h"
14 #include "layout.h"
15 #include "path.h"
16 #include "track.h"
17 #include "tracktype.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 namespace R2C2 {
23
24 Track3D::Track3D(Layout3D &l, Track &t):
25         GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
26         layout(l),
27         track(t),
28         type(layout.get_catalogue().get_track(track.get_type())),
29         path(new Path3D(*this))
30 {
31         layout.add_track(*this);
32         layout.get_scene().add(*this);
33
34         const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
35         const vector<Track *> &links = track.get_links();
36         for(unsigned i=0; i<type_eps.size(); ++i)
37         {
38                 if(!links[i] || links[i]>&track)
39                         endpoints.push_back(new Endpoint3D(*this, i));
40                 else
41                         endpoints.push_back(0);
42         }
43
44         track.signal_link_changed.connect(sigc::mem_fun(this, &Track3D::link_changed));
45 }
46
47 Track3D::~Track3D()
48 {
49         delete path;
50
51         layout.remove_track(*this);
52         layout.get_scene().remove(*this);
53
54         for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
55                 delete *i;
56 }
57
58 void Track3D::get_bounds(float angle, Vector &minp, Vector &maxp) const
59 {
60         type.get_bounds(angle-track.get_rotation(), minp, maxp);
61
62         float c = cos(-angle);
63         float s = sin(-angle);
64
65         const Vector &pos = track.get_position();
66         minp.x += c*pos.x-s*pos.y;
67         maxp.x += c*pos.x-s*pos.y;
68         minp.y += s*pos.x+c*pos.y;
69         maxp.y += s*pos.x+c*pos.y;
70         minp.z += pos.z;
71         maxp.z += pos.z;
72
73         float slope = track.get_slope();
74         if(slope>0)
75                 maxp.z += slope;
76         else
77                 minp.z += slope;
78 }
79
80 Vector Track3D::get_node() const
81 {
82         const Vector &pos = track.get_position();
83         Vector minp;
84         Vector maxp;
85         type.get_bounds(0, minp, maxp);
86         float rot = track.get_rotation();
87         float c = cos(rot);
88         float s = sin(rot);
89
90         Vector center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
91         return Vector(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02);
92 }
93
94 GL::Matrix Track3D::get_matrix() const
95 {
96         const Vector &pos = track.get_position();
97         float rot = track.get_rotation();
98
99         GL::Matrix matrix;
100         matrix.translate(pos.x, pos.y, pos.z);
101         matrix.rotate(rot, 0, 0, 1);
102         matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0);
103
104         return matrix;
105 }
106
107 void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
108 {
109         renderer.matrix_stack() *= get_matrix();
110         glPushName(reinterpret_cast<unsigned>(this));
111 }
112
113 void Track3D::finish_render(Msp::GL::Renderer &, const GL::Tag &) const
114 {
115         glPopName();
116 }
117
118 void Track3D::link_changed(unsigned i, Track *trk)
119 {
120         if(!trk || trk>&track)
121         {
122                 if(!endpoints[i])
123                         endpoints[i] = new Endpoint3D(*this, i);
124         }
125         else if(endpoints[i])
126         {
127                 delete endpoints[i];
128                 endpoints[i] = 0;
129         }
130 }
131
132 } // namespace R2C2