3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/misc.h>
11 #include "libmarklin/tracktype.h"
16 #include "tracktype.h"
23 Track3D::Track3D(Layout3D &l, Track &t):
26 type(layout.get_catalogue().get_track(track.get_type())),
27 path(new Path3D(*this))
29 layout.add_track(*this);
30 layout.get_scene().add(*this);
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));
39 layout.remove_track(*this);
40 layout.get_scene().remove(*this);
42 for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
46 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
48 type.get_bounds(angle-track.get_rotation(), minp, maxp);
50 float c = cos(-angle);
51 float s = sin(-angle);
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;
61 float slope = track.get_slope();
68 Point Track3D::get_node() const
70 const Point &pos = track.get_position();
73 type.get_bounds(0, minp, maxp);
74 float rot = track.get_rotation();
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);
82 void Track3D::apply_matrix() const
84 const Point &pos = track.get_position();
85 float rot = track.get_rotation();
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);
92 void Track3D::render(const GL::Tag &tag) const
94 GL::PushMatrix push_mat;
98 glPushName(reinterpret_cast<unsigned>(this));
105 } // namespace Marklin