3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include <msp/gl/misc.h>
11 #include "libmarklin/tracktype.h"
19 Track3D::Track3D(Track &t, unsigned q):
22 varray((GL::NORMAL3, GL::VERTEX3)),
28 void Track3D::set_color(const Msp::GL::Color &c)
33 void Track3D::set_quality(unsigned q)
39 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
41 const Point &pos = track.get_position();
42 float rot = track.get_rotation();
44 float c = cos(-angle);
45 float s = sin(-angle);
47 minp.x = maxp.x = c*pos.x-s*pos.y;
48 minp.y = maxp.y = s*pos.x+c*pos.y;
50 float c2 = cos(rot-angle);
51 float s2 = sin(rot-angle);
53 for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
55 float x = c*pos.x-s*pos.y + c2*i->x-s2*i->y;
56 float y = s*pos.x+c*pos.y + s2*i->x+c2*i->y;
58 minp.x = min(minp.x, x);
59 minp.y = min(minp.y, y);
60 maxp.x = max(maxp.x, x);
61 maxp.y = max(maxp.y, y);
65 void Track3D::render() const
69 glPushName(reinterpret_cast<unsigned>(this));
72 glColor4f(0.25*color.r, 0.25*color.g, 0.25*color.b, 1);
73 glDrawElements(GL_QUADS, base_seq.size(), GL_UNSIGNED_INT, &base_seq[0]);
76 glColor4f(0.85*color.r, 0.85*color.g, 0.85*color.b, 1);
77 glDrawElements(GL_QUADS, rail_seq.size(), GL_UNSIGNED_INT, &rail_seq[0]);
84 void Track3D::render_endpoints() const
88 const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
89 for(unsigned i=0; i<endpoints.size(); ++i)
91 const Endpoint &ep = endpoints[i];
92 GL::set(GL_CULL_FACE, track.get_link(i));
94 glColor4f(0.5, 0, 1, 0.5);
96 glColor4f(1, 0, 0.5, 0.5);
98 float c = cos(ep.dir);
99 float s = sin(ep.dir);
102 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, 0);
103 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, 0);
104 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, 0.02);
105 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, 0.02);
112 void Track3D::render_route(int route) const
117 if(route>=0 && static_cast<unsigned>(route)<route_seq.size())
118 glDrawElements(GL_QUADS, route_seq[route].size(), GL_UNSIGNED_INT, &route_seq[route][0]);
121 for(unsigned i=0; i<route_seq.size(); ++i)
122 glDrawElements(GL_QUADS, route_seq[i].size(), GL_UNSIGNED_INT, &route_seq[i][0]);
128 void Track3D::prepare_render() const
130 const Point &pos = track.get_position();
131 float rot = track.get_rotation();
134 glTranslatef(pos.x, pos.y, pos.z);
135 glRotatef(rot*180/M_PI, 0, 0, 1);
138 void Track3D::build_object()
141 GL::VertexArrayBuilder builder(varray);
146 route_seq.resize(track.get_type().get_n_routes());
148 const vector<TrackPart> &parts = track.get_type().get_parts();
150 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
151 build_part(*i, builder, index);
154 void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index)
156 static vector<Point> profile;
159 profile.push_back(Point(0, -0.02, 0));
160 profile.push_back(Point(0, -0.014, 0.008));
161 profile.push_back(Point(0, -0.014, 0.008));
162 profile.push_back(Point(0, 0.014, 0.008));
163 profile.push_back(Point(0, 0.014, 0.008));
164 profile.push_back(Point(0, 0.02, 0));
165 for(unsigned i=0; i<2; ++i)
167 profile.push_back(Point(0, -0.009+i*0.017, 0.008));
168 profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
169 profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
170 profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
171 profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
172 profile.push_back(Point(0, -0.008+i*0.017, 0.008));
174 profile.push_back(Point(0, -0.002, 0.012));
175 profile.push_back(Point(0, 0.002, 0.012));
177 static unsigned psize = profile.size();
179 unsigned nsegs = (part.radius ? static_cast<unsigned>(part.length*(1<<quality))+1 : 1);
180 float plen = part.length;
182 plen *= abs(part.radius);
184 for(unsigned i=0; i<=nsegs; ++i)
186 float a = part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
189 Point p = part.get_point(i*plen/nsegs);
191 for(unsigned j=0; j<psize; ++j)
194 float dy = profile[k+1].y-profile[k].y;
195 float dz = profile[k+1].z-profile[k].z;
196 float d = sqrt(dy*dy+dz*dz);
197 va_builder.normal(s*dz/d, -c*dz/d, dy/d);
199 Point v(p.x+c*profile[j].x-s*profile[j].y, p.y+c*profile[j].y+s*profile[j].x, profile[j].z+i*track.get_slope()/nsegs);
200 va_builder.vertex(v.x, v.y, v.z);
206 for(unsigned i=0; i<nsegs; ++i)
208 for(unsigned j=0; j<3; ++j)
210 base_seq.push_back(base_index+i*psize+j*2);
211 base_seq.push_back(base_index+(i+1)*psize+j*2);
212 base_seq.push_back(base_index+(i+1)*psize+1+j*2);
213 base_seq.push_back(base_index+i*psize+1+j*2);
215 for(unsigned j=3; j<9; ++j)
217 rail_seq.push_back(base_index+i*psize+j*2);
218 rail_seq.push_back(base_index+(i+1)*psize+j*2);
219 rail_seq.push_back(base_index+(i+1)*psize+1+j*2);
220 rail_seq.push_back(base_index+i*psize+1+j*2);
222 route_seq[part.route].push_back(base_index+i*psize+18);
223 route_seq[part.route].push_back(base_index+(i+1)*psize+18);
224 route_seq[part.route].push_back(base_index+(i+1)*psize+19);
225 route_seq[part.route].push_back(base_index+i*psize+19);
228 base_index += (nsegs+1)*psize;
231 } // namespace Marklin