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