]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
32857961d9ecc54134bb4f352c7136783f3098a6
[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         color(1, 1, 1)
27 {
28         layout.add_track(*this);
29         layout.get_scene().add(*this);
30
31         const vector<Endpoint> &type_eps = track.get_type().get_endpoints();
32         for(unsigned i=0; i<type_eps.size(); ++i)
33                 endpoints.push_back(new Endpoint3D(*this, i));
34 }
35
36 Track3D::~Track3D()
37 {
38         layout.remove_track(*this);
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 void Track3D::set_color(const Msp::GL::Color &c)
46 {
47         color = c;
48 }
49
50 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
51 {
52         type.get_bounds(angle-track.get_rotation(), minp, maxp);
53
54         float c = cos(-angle);
55         float s = sin(-angle);
56
57         const Point &pos = track.get_position();
58         minp.x += c*pos.x-s*pos.y;
59         maxp.x += c*pos.x-s*pos.y;
60         minp.y += s*pos.x+c*pos.y;
61         maxp.y += s*pos.x+c*pos.y;
62         minp.z += pos.z;
63         maxp.z += pos.z;
64
65         float slope = track.get_slope();
66         if(slope>0)
67                 maxp.z += slope;
68         else
69                 minp.z += slope;
70 }
71
72 void Track3D::render(const GL::Tag &tag) const
73 {
74         GL::PushMatrix push_mat;
75
76         const Point &pos = track.get_position();
77         float rot = track.get_rotation();
78
79         glTranslatef(pos.x, pos.y, pos.z);
80         glRotatef(rot*180/M_PI, 0, 0, 1);
81         glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
82
83         glPushName(reinterpret_cast<unsigned>(this));
84
85         type.render(tag);
86
87         glPopName();
88 }
89
90 } // namespace Marklin