3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include <msp/gl/misc.h>
11 #include "libmarklin/tracktype.h"
14 #include "tracktype.h"
21 Track3D::Track3D(Layout3D &l, Track &t):
24 type(layout.get_catalogue().get_track(track.get_type())),
28 void Track3D::set_color(const Msp::GL::Color &c)
33 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
35 type.get_bounds(angle-track.get_rotation(), minp, maxp);
37 float c = cos(-angle);
38 float s = sin(-angle);
40 const Point &pos = track.get_position();
41 minp.x += c*pos.x-s*pos.y;
42 maxp.x += c*pos.x-s*pos.y;
43 minp.y += s*pos.x+c*pos.y;
44 maxp.y += s*pos.x+c*pos.y;
46 float slope = track.get_slope();
53 void Track3D::render() const
57 glPushName(reinterpret_cast<unsigned>(this));
65 void Track3D::render_endpoints() const
67 prepare_render(false);
69 const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
70 for(unsigned i=0; i<endpoints.size(); ++i)
72 const Endpoint &ep = endpoints[i];
73 GL::set(GL_CULL_FACE, track.get_link(i));
75 glColor4f(0.5, 0, 1, 0.5);
77 glColor4f(1, 0, 0.5, 0.5);
79 float c = cos(ep.dir);
80 float s = sin(ep.dir);
81 float z = (i==1 ? track.get_slope() : 0);
84 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z);
85 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z);
86 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z+0.02);
87 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z+0.02);
91 if(abs(track.get_slope())>1e-4)
94 if(track.get_slope()>0)
97 p.z += track.get_slope();
100 p = endpoints[0].pos;
101 glBegin(GL_TRIANGLE_STRIP);
102 for(unsigned i=0; i<=16; ++i)
104 float c = cos(i*M_PI/8);
105 float s = sin(i*M_PI/8);
107 glVertex3f(p.x+c*0.01, p.y+s*0.01, p.z);
108 glVertex3f(p.x+c*0.01, p.y+s*0.01, -track.get_position().z);
116 void Track3D::render_path(int path) const
118 prepare_render(true);
122 if(path>=0 && static_cast<unsigned>(path)<path_seq.size())
123 glDrawElements(GL_QUADS, path_seq[path].size(), GL_UNSIGNED_INT, &path_seq[path][0]);
126 for(unsigned i=0; i<path_seq.size(); ++i)
127 glDrawElements(GL_QUADS, path_seq[i].size(), GL_UNSIGNED_INT, &path_seq[i][0]);
133 void Track3D::prepare_render(bool slope) const
135 const Point &pos = track.get_position();
136 float rot = track.get_rotation();
139 glTranslatef(pos.x, pos.y, pos.z);
140 glRotatef(rot*180/M_PI, 0, 0, 1);
142 glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
145 } // namespace Marklin