]> git.tdb.fi Git - r2c2.git/blob - source/3d/tracktype.cpp
a60c9a6f7611425c737fb09c2e69ec3f6b8d717e
[r2c2.git] / source / 3d / tracktype.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010-2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <cmath>
10 #include <msp/gl/technique.h>
11 #include "catalogue.h"
12 #include "tracktype.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace {
18
19 bool compare_z(const R2C2::Vector &p1, const R2C2::Vector &p2)
20 {
21         return p1.z<p2.z;
22 }
23
24 template<typename Iter>
25 Iter graham_scan(Iter begin, Iter end)
26 {
27         // http://en.wikipedia.org/wiki/Graham_scan
28
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))
33                         lowest = *i;
34
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);
39
40         for(Iter k=begin, i=k++, j=k++;; )
41         {
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);
44
45                 if(turn<1e-5)
46                 {
47                         // Right turn - throw the middle point away
48                         // Special case for collinear vertices in the beginning
49                         if(i==begin)
50                                 j = k++;
51                         else
52                                 j = i--;
53                 }
54                 else
55                 {
56                         // Left turn - store the middle point and advance
57                         if(++i!=j)
58                                 *i = *j;
59                         j = k++;
60                 }
61
62                 // Cycle back to beginning and terminate after checking the last point
63                 if(k==end)
64                         k = begin;
65                 else if(j==begin)
66                         return ++i;
67         }
68 }
69
70 }
71
72 namespace R2C2 {
73
74 TrackType3D::TrackType3D(Catalogue3D &cat3d, const TrackType &tt):
75         catalogue(cat3d),
76         mesh(0),
77         object(0)
78 {
79         const Catalogue &cat = cat3d.get_catalogue();
80         const vector<TrackPart> &parts = tt.get_parts();
81
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;
86
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;
91
92         float gauge = cat.get_gauge();
93
94         string obj_name = tt.get_object();
95         if(!obj_name.empty())
96         {
97                 object = catalogue.get<GL::Object>(obj_name);
98                 const GL::Mesh *m = object->get_mesh();
99                 const GL::VertexArray &vertices = m->get_vertices();
100                 int vertex_offs = vertices.get_format().offset(GL::VERTEX2);
101                 if(vertex_offs>=0)
102                 {
103                         for(unsigned i=0; i<vertices.size(); ++i)
104                         {
105                                 const float *v = vertices[i]+vertex_offs;
106                                 border.push_back(Vector(v[0], v[1]));
107                         }
108                 }
109         }
110         else
111         {
112                 mesh = new GL::Mesh((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3));
113                 mesh->set_winding(&GL::WindingTest::counterclockwise());
114                 GL::MeshBuilder bld(*mesh);
115
116                 unsigned index = 0;
117                 bld.texcoord(0.25, 0.5);
118                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
119                         build_part(*i, ballast_profile, Vector(0, -ballast_min.y), false, bld, index);
120
121                 bld.texcoord(0.75, 0.5);
122                 float y = ballast_h-rail_min.y;
123                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
124                         build_part(*i, rail_profile, Vector(-gauge/2, y), true, bld, index);
125                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
126                         build_part(*i, rail_profile, Vector(gauge/2, y), false, bld, index);
127
128                 object = new GL::Object;
129                 object->set_mesh(mesh);
130                 object->set_technique(catalogue.get<GL::Technique>(cat.get_track_technique()));
131         }
132  
133         unsigned paths = tt.get_paths();
134         for(unsigned i=0; paths; ++i, paths>>=1)
135         {
136                 GL::Mesh *m = 0;
137                 if(paths&1)
138                 {
139                         m = new GL::Mesh(GL::VERTEX3);
140                         GL::MeshBuilder bld(*m);
141                         unsigned index = 0;
142                         for(vector<TrackPart>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
143                                 if(j->get_path()==i)
144                                         build_part(*j, cat.get_path_profile(), Vector(0, ballast_h+1.5*rail_h), false, bld, index);
145                 }
146                 path_meshes.push_back(m);
147         }
148
149         min_z = max_z = border.front().z;
150         for(vector<Vector>::iterator i=border.begin(); i!=border.end(); ++i)
151         {
152                 min_z = min(min_z, i->z);
153                 max_z = max(max_z, i->z);
154         }
155         border.erase(graham_scan(border.begin(), border.end()), border.end());
156 }
157
158 TrackType3D::~TrackType3D()
159 {
160         for(vector<GL::Mesh *>::iterator i=path_meshes.begin(); i!=path_meshes.end(); ++i)
161                 delete *i;
162 }
163
164 void TrackType3D::get_bounds(float angle, Vector &minp, Vector &maxp) const
165 {
166         float c = cos(-angle);
167         float s = sin(-angle);
168
169         minp = maxp = Vector();
170         minp.z = min_z;
171         maxp.z = max_z;
172
173         for(vector<Vector>::const_iterator i=border.begin(); i!=border.end(); ++i)
174         {
175                 float x = c*i->x-s*i->y;
176                 float y = s*i->x+c*i->y;
177
178                 minp.x = min(minp.x, x);
179                 minp.y = min(minp.y, y);
180                 maxp.x = max(maxp.x, x);
181                 maxp.y = max(maxp.y, y);
182         }
183 }
184
185 const GL::Mesh &TrackType3D::get_path_mesh(unsigned p) const
186 {
187         if(p>=path_meshes.size() || !path_meshes[p])
188                 throw InvalidParameterValue("Invalid path");
189         return *path_meshes[p];
190 }
191
192 void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Vector &offset, bool mirror, GL::MeshBuilder &bld, unsigned &base_index)
193 {
194         float plen = part.get_length();
195         unsigned nsegs = (part.is_curved() ? static_cast<unsigned>(plen*32)+1 : 1);
196
197         unsigned n_vertices = profile.get_n_vertices();
198         for(unsigned i=0; i<=nsegs; ++i)
199         {
200                 TrackPoint basep = part.get_point(i*plen/nsegs);
201                 float c = cos(basep.dir);
202                 float s = sin(basep.dir);
203
204                 for(unsigned j=0; j<n_vertices; ++j)
205                 {
206                         const Profile::Vertex &v = profile.get_vertex(mirror ? n_vertices-1-j : j);
207                         Vector p = v.pos;
208                         if(mirror)
209                                 p.x = -p.x;
210                         p.z = basep.pos.z+p.y+offset.y;
211                         p.y = basep.pos.y-c*(p.x+offset.x);
212                         p.x = basep.pos.x+s*(p.x+offset.x);
213
214                         Vector n = v.normal;
215                         if(mirror)
216                                 n.x = -n.x;
217
218                         bld.normal(s*n.x, -c*n.x, n.y);
219                         bld.vertex(p.x, p.y, p.z);
220
221                         border.push_back(p);
222                 }
223         }
224
225         for(unsigned i=0; i+1<n_vertices; )
226         {
227                 bld.begin(GL::TRIANGLE_STRIP);
228                 for(unsigned j=0; j<=nsegs; ++j)
229                 {
230                         unsigned k = j*n_vertices+i;
231                         bld.element(base_index+k+1);
232                         bld.element(base_index+k);
233                 }
234                 bld.end();
235
236                 ++i;
237                 if(!profile.get_vertex(i).smooth)
238                         ++i;
239         }
240
241         base_index += (nsegs+1)*n_vertices;
242 }
243
244 } // namespace R2C2