3 This file is part of R²C²
4 Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include <msp/gl/technique.h>
11 #include "catalogue.h"
12 #include "tracktype.h"
19 bool compare_z(const R2C2::Vector &p1, const R2C2::Vector &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 R2C2::Vector 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(Catalogue3D &cat3d, const TrackType &tt):
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 Vector &ballast_min = ballast_profile.get_min_coords();
84 const Vector &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 Vector &rail_min = rail_profile.get_min_coords();
89 const Vector &rail_max = rail_profile.get_max_coords();
90 float rail_h = rail_max.y-rail_min.y;
92 float gauge = cat.get_gauge();
94 string obj_name = tt.get_object();
97 object = catalogue.get<GL::Object>(obj_name);
102 mesh = new GL::Mesh((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3));
103 mesh->set_winding(&GL::WindingTest::counterclockwise());
104 GL::MeshBuilder bld(*mesh);
107 bld.texcoord(0.25, 0.5);
108 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
109 build_part(*i, ballast_profile, Vector(0, -ballast_min.y), false, bld, index);
111 bld.texcoord(0.75, 0.5);
112 float y = ballast_h-rail_min.y;
113 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
114 build_part(*i, rail_profile, Vector(-gauge/2, y), true, bld, index);
115 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
116 build_part(*i, rail_profile, Vector(gauge/2, y), false, bld, index);
118 object = new GL::Object;
119 object->set_mesh(mesh);
120 object->set_technique(catalogue.get<GL::Technique>(cat.get_track_technique()));
123 unsigned paths = tt.get_paths();
124 for(unsigned i=0; paths; ++i, paths>>=1)
129 m = new GL::Mesh(GL::VERTEX3);
130 GL::MeshBuilder bld(*m);
132 for(vector<TrackPart>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
134 build_part(*j, cat.get_path_profile(), Vector(0, ballast_h+1.5*rail_h), false, bld, index);
136 path_meshes.push_back(m);
139 min_z = max_z = border.front().z;
140 for(vector<Vector>::iterator i=border.begin(); i!=border.end(); ++i)
142 min_z = min(min_z, i->z);
143 max_z = max(max_z, i->z);
145 border.erase(graham_scan(border.begin(), border.end()), border.end());
148 TrackType3D::~TrackType3D()
150 for(vector<GL::Mesh *>::iterator i=path_meshes.begin(); i!=path_meshes.end(); ++i)
154 void TrackType3D::get_bounds(float angle, Vector &minp, Vector &maxp) const
156 float c = cos(-angle);
157 float s = sin(-angle);
159 minp = maxp = Vector();
163 for(vector<Vector>::const_iterator i=border.begin(); i!=border.end(); ++i)
165 float x = c*i->x-s*i->y;
166 float y = s*i->x+c*i->y;
168 minp.x = min(minp.x, x);
169 minp.y = min(minp.y, y);
170 maxp.x = max(maxp.x, x);
171 maxp.y = max(maxp.y, y);
175 const GL::Mesh &TrackType3D::get_path_mesh(unsigned p) const
177 if(p>=path_meshes.size() || !path_meshes[p])
178 throw InvalidParameterValue("Invalid path");
179 return *path_meshes[p];
182 void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Vector &offset, bool mirror, GL::MeshBuilder &bld, unsigned &base_index)
184 float plen = part.get_length();
185 unsigned nsegs = (part.is_curved() ? static_cast<unsigned>(plen*32)+1 : 1);
187 unsigned n_vertices = profile.get_n_vertices();
188 for(unsigned i=0; i<=nsegs; ++i)
190 TrackPoint basep = part.get_point(i*plen/nsegs);
191 float c = cos(basep.dir);
192 float s = sin(basep.dir);
194 for(unsigned j=0; j<n_vertices; ++j)
196 const Profile::Vertex &v = profile.get_vertex(mirror ? n_vertices-1-j : j);
200 p.z = basep.pos.z+p.y+offset.y;
201 p.y = basep.pos.y-c*(p.x+offset.x);
202 p.x = basep.pos.x+s*(p.x+offset.x);
208 bld.normal(s*n.x, -c*n.x, n.y);
209 bld.vertex(p.x, p.y, p.z);
215 for(unsigned i=0; i+1<n_vertices; )
217 bld.begin(GL::TRIANGLE_STRIP);
218 for(unsigned j=0; j<=nsegs; ++j)
220 unsigned k = j*n_vertices+i;
221 bld.element(base_index+k+1);
222 bld.element(base_index+k);
227 if(!profile.get_vertex(i).smooth)
231 base_index += (nsegs+1)*n_vertices;