]> git.tdb.fi Git - r2c2.git/blob - source/3d/tracktype.cpp
Render tracks through GL::Objects
[r2c2.git] / source / 3d / tracktype.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010 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::Point &p1, const R2C2::Point &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::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))
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((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3))
77 {
78         const Catalogue &cat = cat3d.get_catalogue();
79         const vector<TrackPart> &parts = tt.get_parts();
80
81         const Profile &ballast_profile = cat.get_ballast_profile();
82         const Point &ballast_min = ballast_profile.get_min_coords();
83         const Point &ballast_max = ballast_profile.get_max_coords();
84         float ballast_h = ballast_max.y-ballast_min.y;
85
86         const Profile &rail_profile = cat.get_rail_profile();
87         const Point &rail_min = rail_profile.get_min_coords();
88         const Point &rail_max = rail_profile.get_max_coords();
89         float rail_h = rail_max.y-rail_min.y;
90
91         float gauge = cat.get_gauge();
92
93         {
94                 unsigned index = 0;
95                 GL::MeshBuilder bld(mesh);
96                 bld.texcoord(0.25, 0.5);
97                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
98                         build_part(*i, ballast_profile, Point(0, -ballast_min.y), bld, index);
99
100                 bld.texcoord(0.75, 0.5);
101                 float y = ballast_h-rail_min.y;
102                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
103                         build_part(*i, rail_profile, Point(-gauge/2-rail_max.x, y), bld, index);
104                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
105                         build_part(*i, rail_profile, Point(gauge/2-rail_min.x, y), bld, index);
106         }
107
108         object.set_mesh(&mesh);
109         object.set_technique(catalogue.get<GL::Technique>(cat.get_track_technique()));
110  
111         unsigned paths = tt.get_paths();
112         for(unsigned i=0; paths; ++i, paths>>=1)
113         {
114                 GL::Mesh *m = 0;
115                 if(paths&1)
116                 {
117                         m = new GL::Mesh(GL::VERTEX3);
118                         GL::MeshBuilder bld(*m);
119                         unsigned index = 0;
120                         for(vector<TrackPart>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
121                                 if(j->get_path()==i)
122                                         build_part(*j, cat.get_path_profile(), Point(0, ballast_h+1.5*rail_h), bld, index);
123                 }
124                 path_meshes.push_back(m);
125         }
126
127         min_z = max_z = border.front().z;
128         for(vector<Point>::iterator i=border.begin(); i!=border.end(); ++i)
129         {
130                 min_z = min(min_z, i->z);
131                 max_z = max(max_z, i->z);
132         }
133         border.erase(graham_scan(border.begin(), border.end()), border.end());
134 }
135
136 TrackType3D::~TrackType3D()
137 {
138         for(vector<GL::Mesh *>::iterator i=path_meshes.begin(); i!=path_meshes.end(); ++i)
139                 delete *i;
140 }
141
142 void TrackType3D::get_bounds(float angle, Point &minp, Point &maxp) const
143 {
144         float c = cos(-angle);
145         float s = sin(-angle);
146
147         minp = maxp = Point();
148         minp.z = min_z;
149         maxp.z = max_z;
150
151         for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
152         {
153                 float x = c*i->x-s*i->y;
154                 float y = s*i->x+c*i->y;
155
156                 minp.x = min(minp.x, x);
157                 minp.y = min(minp.y, y);
158                 maxp.x = max(maxp.x, x);
159                 maxp.y = max(maxp.y, y);
160         }
161 }
162
163 const GL::Mesh &TrackType3D::get_path_mesh(unsigned p) const
164 {
165         if(p>=path_meshes.size() || !path_meshes[p])
166                 throw InvalidParameterValue("Invalid path");
167         return *path_meshes[p];
168 }
169
170 void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Point &offset, GL::MeshBuilder &bld, unsigned &base_index)
171 {
172         float plen = part.get_length();
173         unsigned nsegs = (part.is_curved() ? static_cast<unsigned>(plen*32)+1 : 1);
174
175         unsigned n_points = profile.get_n_points();
176         for(unsigned i=0; i<=nsegs; ++i)
177         {
178                 TrackPoint basep = part.get_point(i*plen/nsegs);
179                 float c = cos(basep.dir);
180                 float s = sin(basep.dir);
181
182                 Point p;
183                 for(unsigned j=0; j<n_points; ++j)
184                 {
185                         // TODO: smoothing - only duplicate vertex if the angle is large enough
186
187                         p = profile.get_point(j);
188                         p.z = basep.pos.z+p.y+offset.y;
189                         p.y = basep.pos.y-c*(p.x+offset.x);
190                         p.x = basep.pos.x+s*(p.x+offset.x);
191                         if(j>0)
192                                 bld.vertex(p.x, p.y, p.z);
193
194                         if(j+1<n_points)
195                         {
196                                 Point n = profile.get_edge_normal(j);
197                                 bld.normal(s*n.x, -c*n.x, n.y);
198                                 bld.vertex(p.x, p.y, p.z);
199                         }
200
201                         border.push_back(p);
202                 }
203         }
204
205         for(unsigned i=0; i+1<n_points; ++i)
206         {
207                 bld.begin(GL::TRIANGLE_STRIP);
208                 for(unsigned j=0; j<=nsegs; ++j)
209                 {
210                         unsigned k = (j*(n_points-1)+i)*2;
211                         bld.element(base_index+k+1);
212                         bld.element(base_index+k);
213                 }
214                 bld.end();
215         }
216
217         base_index += (nsegs+1)*(n_points-1)*2;
218 }
219
220 } // namespace R2C2