3 This file is part of R²C²
4 Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/misc.h>
11 #include <msp/gl/renderer.h>
12 #include "libr2c2/tracktype.h"
17 #include "tracktype.h"
24 Track3D::Track3D(Layout3D &l, Track &t):
25 GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
28 type(layout.get_catalogue().get_track(track.get_type())),
29 path(new Path3D(*this))
31 layout.add_track(*this);
32 layout.get_scene().add(*this);
34 const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
35 for(unsigned i=0; i<type_eps.size(); ++i)
36 endpoints.push_back(new Endpoint3D(*this, i));
43 layout.remove_track(*this);
44 layout.get_scene().remove(*this);
46 for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
50 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
52 type.get_bounds(angle-track.get_rotation(), minp, maxp);
54 float c = cos(-angle);
55 float s = sin(-angle);
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;
65 float slope = track.get_slope();
72 Point Track3D::get_node() const
74 const Point &pos = track.get_position();
77 type.get_bounds(0, minp, maxp);
78 float rot = track.get_rotation();
82 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
83 return Point(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02);
86 GL::Matrix Track3D::get_matrix() const
88 const Point &pos = track.get_position();
89 float rot = track.get_rotation();
92 matrix.translate(pos.x, pos.y, pos.z);
93 matrix.rotate(rot, 0, 0, 1);
94 matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0);
99 void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
101 renderer.matrix_stack() *= get_matrix();
102 glPushName(reinterpret_cast<unsigned>(this));
105 void Track3D::finish_render(Msp::GL::Renderer &, const GL::Tag &) const