]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
Add an overlay to display aspects of tracks
[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 "track.h"
15 #include "tracktype.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 namespace Marklin {
21
22 Track3D::Track3D(Layout3D &l, Track &t):
23         layout(l),
24         track(t),
25         type(layout.get_catalogue().get_track(track.get_type()))
26 {
27         layout.add_track(*this);
28         layout.get_scene().add(*this);
29
30         const vector<Endpoint> &type_eps = track.get_type().get_endpoints();
31         for(unsigned i=0; i<type_eps.size(); ++i)
32                 endpoints.push_back(new Endpoint3D(*this, i));
33 }
34
35 Track3D::~Track3D()
36 {
37         layout.remove_track(*this);
38         layout.get_scene().remove(*this);
39
40         for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
41                 delete *i;
42 }
43
44 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
45 {
46         type.get_bounds(angle-track.get_rotation(), minp, maxp);
47
48         float c = cos(-angle);
49         float s = sin(-angle);
50
51         const Point &pos = track.get_position();
52         minp.x += c*pos.x-s*pos.y;
53         maxp.x += c*pos.x-s*pos.y;
54         minp.y += s*pos.x+c*pos.y;
55         maxp.y += s*pos.x+c*pos.y;
56         minp.z += pos.z;
57         maxp.z += pos.z;
58
59         float slope = track.get_slope();
60         if(slope>0)
61                 maxp.z += slope;
62         else
63                 minp.z += slope;
64 }
65
66 void Track3D::render(const GL::Tag &tag) const
67 {
68         GL::PushMatrix push_mat;
69
70         const Point &pos = track.get_position();
71         float rot = track.get_rotation();
72
73         glTranslatef(pos.x, pos.y, pos.z);
74         glRotatef(rot*180/M_PI, 0, 0, 1);
75         glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
76
77         glPushName(reinterpret_cast<unsigned>(this));
78
79         type.render(tag);
80
81         glPopName();
82 }
83
84 } // namespace Marklin