]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
Reimplement path display
[r2c2.git] / source / 3d / track.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 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 "libmarklin/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 Marklin {
22
23 Track3D::Track3D(Layout3D &l, Track &t):
24         layout(l),
25         track(t),
26         type(layout.get_catalogue().get_track(track.get_type())),
27         path(new Path3D(*this))
28 {
29         layout.add_track(*this);
30         layout.get_scene().add(*this);
31
32         const vector<Endpoint> &type_eps = track.get_type().get_endpoints();
33         for(unsigned i=0; i<type_eps.size(); ++i)
34                 endpoints.push_back(new Endpoint3D(*this, i));
35 }
36
37 Track3D::~Track3D()
38 {
39         layout.remove_track(*this);
40         layout.get_scene().remove(*this);
41
42         for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
43                 delete *i;
44 }
45
46 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
47 {
48         type.get_bounds(angle-track.get_rotation(), minp, maxp);
49
50         float c = cos(-angle);
51         float s = sin(-angle);
52
53         const Point &pos = track.get_position();
54         minp.x += c*pos.x-s*pos.y;
55         maxp.x += c*pos.x-s*pos.y;
56         minp.y += s*pos.x+c*pos.y;
57         maxp.y += s*pos.x+c*pos.y;
58         minp.z += pos.z;
59         maxp.z += pos.z;
60
61         float slope = track.get_slope();
62         if(slope>0)
63                 maxp.z += slope;
64         else
65                 minp.z += slope;
66 }
67
68 Point Track3D::get_node() const
69 {
70         const Point &pos = track.get_position();
71         Point minp;
72         Point maxp;
73         type.get_bounds(0, minp, maxp);
74         float rot = track.get_rotation();
75         float c = cos(rot);
76         float s = sin(rot);
77
78         Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
79         return Point(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02);
80 }
81
82 void Track3D::apply_matrix() const
83 {
84         const Point &pos = track.get_position();
85         float rot = track.get_rotation();
86
87         glTranslatef(pos.x, pos.y, pos.z);
88         glRotatef(rot*180/M_PI, 0, 0, 1);
89         glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
90 }
91
92 void Track3D::render(const GL::Tag &tag) const
93 {
94         GL::PushMatrix push_mat;
95
96         apply_matrix();
97
98         glPushName(reinterpret_cast<unsigned>(this));
99
100         type.render(tag);
101
102         glPopName();
103 }
104
105 } // namespace Marklin