3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include <msp/gl/meshbuilder.h>
11 #include "catalogue.h"
12 #include "tracktype.h"
19 bool compare_z(const Marklin::Point &p1, const Marklin::Point &p2)
24 template<typename Iter>
25 Iter graham_scan(Iter begin, Iter end)
27 // http://en.wikipedia.org/wiki/Graham_scan
29 // Find point with lowest X coordinate
30 Marklin::Point lowest = *begin;
31 for(Iter i=begin; i!=end; ++i)
32 if(i->x<lowest.x || (i->x==lowest.x && i->y>lowest.y))
35 // Compute tangents and sort points
36 for(Iter i=begin; i!=end; ++i)
37 i->z = (i->x==lowest.x ? 1e5/(i->y-lowest.y-1) : (i->y-lowest.y)/(i->x-lowest.x));
38 sort(begin, end, compare_z);
40 for(Iter k=begin, i=k++, j=k++;; )
42 // Compute winding by cross product
43 float turn = (j->x-i->x)*(k->y-j->y) - (k->x-j->x)*(j->y-i->y);
47 // Right turn - throw the middle point away
48 // Special case for collinear vertices in the beginning
56 // Left turn - store the middle point and advance
62 // Cycle back to beginning and terminate after checking the last point
74 TrackType3D::TrackType3D(const Catalogue3D &cat3d, const TrackType &tt):
76 ballast_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, GL::VERTEX3)),
77 rail_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, GL::VERTEX3))
79 const Catalogue &cat = cat3d.get_catalogue();
80 const vector<TrackPart> &parts = tt.get_parts();
82 const Profile &ballast_profile = cat.get_ballast_profile();
83 const Point &ballast_min = ballast_profile.get_min_coords();
84 const Point &ballast_max = ballast_profile.get_max_coords();
85 float ballast_h = ballast_max.y-ballast_min.y;
87 const Profile &rail_profile = cat.get_rail_profile();
88 const Point &rail_min = rail_profile.get_min_coords();
89 const Point &rail_max = rail_profile.get_max_coords();
90 float rail_h = rail_max.y-rail_min.y;
92 float gauge = cat.get_gauge();
96 GL::MeshBuilder bld(ballast_mesh);
97 bld.color(0.25f, 0.25f, 0.25f);
98 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
99 build_part(*i, ballast_profile, Point(0, -ballast_min.y), bld, index);
104 GL::MeshBuilder bld(rail_mesh);
105 bld.color(0.85f, 0.85f, 0.85f);
106 float y = ballast_h-rail_min.y;
107 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
108 build_part(*i, rail_profile, Point(-gauge/2-rail_max.x, y), bld, index);
109 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
110 build_part(*i, rail_profile, Point(gauge/2-rail_min.x, y), bld, index);
113 unsigned paths = tt.get_paths();
114 for(unsigned i=0; paths; ++i, paths>>=1)
119 mesh = new GL::Mesh(GL::VERTEX3);
120 GL::MeshBuilder bld(*mesh);
122 for(vector<TrackPart>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
124 build_part(*j, cat.get_path_profile(), Point(0, ballast_h+1.5*rail_h), bld, index);
126 path_meshes.push_back(mesh);
129 min_z = max_z = border.front().z;
130 for(vector<Point>::iterator i=border.begin(); i!=border.end(); ++i)
132 min_z = min(min_z, i->z);
133 max_z = max(max_z, i->z);
135 border.erase(graham_scan(border.begin(), border.end()), border.end());
138 void TrackType3D::get_bounds(float angle, Point &minp, Point &maxp) const
140 float c = cos(-angle);
141 float s = sin(-angle);
143 minp = maxp = Point();
147 for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
149 float x = c*i->x-s*i->y;
150 float y = s*i->x+c*i->y;
152 minp.x = min(minp.x, x);
153 minp.y = min(minp.y, y);
154 maxp.x = max(maxp.x, x);
155 maxp.y = max(maxp.y, y);
159 const GL::Mesh &TrackType3D::get_path_mesh(unsigned p) const
161 if(p>=path_meshes.size() || !path_meshes[p])
162 throw InvalidParameterValue("Invalid path");
163 return *path_meshes[p];
166 void TrackType3D::render(const GL::Tag &tag) const
170 catalogue.get_ballast_material().bind();
172 catalogue.get_rail_material().bind();
177 void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Point &offset, GL::MeshBuilder &bld, unsigned &base_index)
179 float plen = part.get_length();
180 unsigned nsegs = (part.is_curved() ? static_cast<unsigned>(plen*16)+1 : 1);
182 unsigned n_points = profile.get_n_points();
183 for(unsigned i=0; i<=nsegs; ++i)
185 TrackPoint basep = part.get_point(i*plen/nsegs);
186 float c = cos(basep.dir);
187 float s = sin(basep.dir);
190 for(unsigned j=0; j<n_points; ++j)
192 // TODO: smoothing - only duplicate vertex if the angle is large enough
194 p = profile.get_point(j);
195 p.z = basep.pos.z+p.y+offset.y;
196 p.y = basep.pos.y-c*(p.x+offset.x);
197 p.x = basep.pos.x+s*(p.x+offset.x);
199 bld.vertex(p.x, p.y, p.z);
203 Point n = profile.get_edge_normal(j);
204 bld.normal(s*n.x, -c*n.x, n.y);
205 bld.vertex(p.x, p.y, p.z);
212 for(unsigned i=0; i+1<n_points; ++i)
214 bld.begin(GL::TRIANGLE_STRIP);
215 for(unsigned j=0; j<=nsegs; ++j)
217 unsigned k = (j*(n_points-1)+i)*2;
218 bld.element(base_index+k+1);
219 bld.element(base_index+k);
224 base_index += (nsegs+1)*(n_points-1)*2;
227 } // namespace Marklin