3 #include <msp/gl/technique.h>
12 bool compare_z(const R2C2::Vector &p1, const R2C2::Vector &p2)
17 template<typename Iter>
18 Iter graham_scan(Iter begin, Iter end)
20 // http://en.wikipedia.org/wiki/Graham_scan
22 // Find point with lowest X coordinate
23 R2C2::Vector lowest = *begin;
24 for(Iter i=begin; i!=end; ++i)
25 if(i->x<lowest.x || (i->x==lowest.x && i->y>lowest.y))
28 // Compute tangents and sort points
29 for(Iter i=begin; i!=end; ++i)
30 i->z = (i->x==lowest.x ? 1e5/(i->y-lowest.y-1) : (i->y-lowest.y)/(i->x-lowest.x));
31 sort(begin, end, compare_z);
33 for(Iter k=begin, i=k++, j=k++;; )
35 // Compute winding by cross product
36 float turn = (j->x-i->x)*(k->y-j->y) - (k->x-j->x)*(j->y-i->y);
40 // Right turn - throw the middle point away
41 // Special case for collinear vertices in the beginning
49 // Left turn - store the middle point and advance
55 // Cycle back to beginning and terminate after checking the last point
67 TrackType3D::TrackType3D(Catalogue3D &cat3d, const TrackType &tt):
72 const Catalogue &cat = cat3d.get_catalogue();
73 const vector<TrackPart> &parts = tt.get_parts();
75 const Profile &ballast_profile = cat.get_ballast_profile();
76 const Vector &ballast_min = ballast_profile.get_min_coords();
77 const Vector &ballast_max = ballast_profile.get_max_coords();
78 float ballast_h = ballast_max.y-ballast_min.y;
80 const Profile &rail_profile = cat.get_rail_profile();
81 const Vector &rail_min = rail_profile.get_min_coords();
82 const Vector &rail_max = rail_profile.get_max_coords();
83 float rail_h = rail_max.y-rail_min.y;
85 float gauge = cat.get_gauge();
87 string obj_name = tt.get_object();
90 object = &catalogue.get<GL::Object>(obj_name);
91 const GL::Mesh *m = object->get_mesh();
92 const GL::VertexArray &vertices = m->get_vertices();
93 int vertex_offs = vertices.get_format().offset(GL::VERTEX2);
96 for(unsigned i=0; i<vertices.size(); ++i)
98 const float *v = vertices[i]+vertex_offs;
99 border.push_back(Vector(v[0], v[1]));
105 mesh = new GL::Mesh((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3));
106 mesh->set_winding(&GL::WindingTest::counterclockwise());
107 GL::MeshBuilder bld(*mesh);
110 bld.texcoord(0.25, 0.5);
111 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
112 build_part(*i, ballast_profile, Vector(0, -ballast_min.y), false, bld, index);
114 bld.texcoord(0.75, 0.5);
115 float y = ballast_h-rail_min.y;
116 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
117 build_part(*i, rail_profile, Vector(-gauge/2, y), true, bld, index);
118 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
119 build_part(*i, rail_profile, Vector(gauge/2, y), false, bld, index);
121 object = new GL::Object;
122 object->set_mesh(mesh);
123 object->set_technique(&catalogue.get<GL::Technique>(cat.get_track_technique()));
126 unsigned paths = tt.get_paths();
127 for(unsigned i=0; paths; ++i, paths>>=1)
132 m = new GL::Mesh(GL::VERTEX3);
133 GL::MeshBuilder bld(*m);
135 for(vector<TrackPart>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
137 build_part(*j, cat.get_path_profile(), Vector(0, ballast_h+1.5*rail_h), false, bld, index);
139 path_meshes.push_back(m);
142 min_z = max_z = border.front().z;
143 for(vector<Vector>::iterator i=border.begin(); i!=border.end(); ++i)
145 min_z = min(min_z, i->z);
146 max_z = max(max_z, i->z);
148 border.erase(graham_scan(border.begin(), border.end()), border.end());
151 TrackType3D::~TrackType3D()
153 for(vector<GL::Mesh *>::iterator i=path_meshes.begin(); i!=path_meshes.end(); ++i)
157 void TrackType3D::get_bounds(float angle, Vector &minp, Vector &maxp) const
159 float c = cos(-angle);
160 float s = sin(-angle);
162 minp = maxp = Vector();
166 for(vector<Vector>::const_iterator i=border.begin(); i!=border.end(); ++i)
168 float x = c*i->x-s*i->y;
169 float y = s*i->x+c*i->y;
171 minp.x = min(minp.x, x);
172 minp.y = min(minp.y, y);
173 maxp.x = max(maxp.x, x);
174 maxp.y = max(maxp.y, y);
178 const GL::Mesh &TrackType3D::get_path_mesh(unsigned p) const
180 if(p>=path_meshes.size())
181 throw out_of_range("TrackType3D::get_path_mesh");
183 throw invalid_argument("TrackType3D::get_path_mesh");
184 return *path_meshes[p];
187 void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Vector &offset, bool mirror, GL::MeshBuilder &bld, unsigned &base_index)
189 float plen = part.get_length();
190 unsigned nsegs = (part.is_curved() ? static_cast<unsigned>(plen*32)+1 : 1);
192 unsigned n_vertices = profile.get_n_vertices();
193 for(unsigned i=0; i<=nsegs; ++i)
195 TrackPoint basep = part.get_point(i*plen/nsegs);
196 float c = cos(basep.dir);
197 float s = sin(basep.dir);
199 for(unsigned j=0; j<n_vertices; ++j)
201 const Profile::Vertex &v = profile.get_vertex(mirror ? n_vertices-1-j : j);
205 p.z = basep.pos.z+p.y+offset.y;
206 p.y = basep.pos.y-c*(p.x+offset.x);
207 p.x = basep.pos.x+s*(p.x+offset.x);
213 bld.normal(s*n.x, -c*n.x, n.y);
214 bld.vertex(p.x, p.y, p.z);
220 for(unsigned i=0; i+1<n_vertices; )
222 bld.begin(GL::TRIANGLE_STRIP);
223 for(unsigned j=0; j<=nsegs; ++j)
225 unsigned k = j*n_vertices+i;
226 bld.element(base_index+k+1);
227 bld.element(base_index+k);
232 if(!profile.get_vertex(i).smooth)
236 base_index += (nsegs+1)*n_vertices;