]> git.tdb.fi Git - r2c2.git/blob - source/3d/tracktype.cpp
Split mesh generation from Track3D to TrackType3D
[r2c2.git] / source / 3d / tracktype.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <cmath>
10 #include <msp/gl/meshbuilder.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 Marklin::Point &p1, const Marklin::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         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))
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 Marklin {
73
74 TrackType3D::TrackType3D(const Catalogue3D &cat3d, const TrackType &tt):
75         ballast_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, GL::VERTEX3)),
76         rail_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, 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
85         const Profile &rail_profile = cat.get_rail_profile();
86         const Point &rail_min = rail_profile.get_min_coords();
87         const Point &rail_max = rail_profile.get_max_coords();
88
89         float gauge = cat.get_gauge();
90
91         {
92                 unsigned index = 0;
93                 GL::MeshBuilder bld(ballast_mesh);
94                 bld.color(0.25f, 0.25f, 0.25f);
95                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
96                         build_part(*i, ballast_profile, Point(0, -ballast_min.y), bld, index);
97         }
98
99         {
100                 unsigned index = 0;
101                 GL::MeshBuilder bld(rail_mesh);
102                 bld.color(0.85f, 0.85f, 0.85f);
103                 float y = ballast_max.y-ballast_min.y-rail_min.y;
104                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
105                         build_part(*i, rail_profile, Point(-gauge/2-rail_max.x, y), bld, index);
106                 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
107                         build_part(*i, rail_profile, Point(gauge/2-rail_min.x, y), bld, index);
108         }
109
110         min_z = max_z = border.front().z;
111         for(vector<Point>::iterator i=border.begin(); i!=border.end(); ++i)
112         {
113                 min_z = min(min_z, i->z);
114                 max_z = max(max_z, i->z);
115         }
116         border.erase(graham_scan(border.begin(), border.end()), border.end());
117 }
118
119 void TrackType3D::get_bounds(float angle, Point &minp, Point &maxp) const
120 {
121         float c = cos(-angle);
122         float s = sin(-angle);
123
124         minp = maxp = Point();
125         minp.z = min_z;
126         maxp.z = max_z;
127
128         for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
129         {
130                 float x = c*i->x-s*i->y;
131                 float y = s*i->x+c*i->y;
132
133                 minp.x = min(minp.x, x);
134                 minp.y = min(minp.y, y);
135                 maxp.x = max(maxp.x, x);
136                 maxp.y = max(maxp.y, y);
137         }
138 }
139
140 void TrackType3D::render() const
141 {
142         ballast_mesh.draw();
143         rail_mesh.draw();
144 }
145
146 void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Point &offset, GL::MeshBuilder &bld, unsigned &base_index)
147 {
148         unsigned nsegs = (part.radius ? static_cast<unsigned>(part.length*16)+1 : 1);
149         float plen = part.length;
150         if(part.radius)
151                 plen *= abs(part.radius);
152
153         unsigned n_points = profile.get_n_points();
154         for(unsigned i=0; i<=nsegs; ++i)
155         {
156                 float a = part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
157                 float c = cos(a);
158                 float s = sin(a);
159                 Point basep = part.get_point(i*plen/nsegs);
160
161                 Point p;
162                 for(unsigned j=0; j<n_points; ++j)
163                 {
164                         // TODO: smoothing - only duplicate vertex if the angle is large enough
165
166                         p = profile.get_point(j);
167                         p.z = basep.z+p.y+offset.y;
168                         p.y = basep.y-c*(p.x+offset.x);
169                         p.x = basep.x+s*(p.x+offset.x);
170                         if(j>0)
171                                 bld.vertex(p.x, p.y, p.z);
172
173                         if(j+1<n_points)
174                         {
175                                 Point n = profile.get_edge_normal(j);
176                                 bld.normal(s*n.x, -c*n.x, n.y);
177                                 bld.vertex(p.x, p.y, p.z);
178                         }
179
180                         border.push_back(p);
181                 }
182         }
183
184         for(unsigned i=0; i+1<n_points; ++i)
185         {
186                 bld.begin(GL::TRIANGLE_STRIP);
187                 for(unsigned j=0; j<=nsegs; ++j)
188                 {
189                         unsigned k = (j*(n_points-1)+i)*2;
190                         bld.element(base_index+k+1);
191                         bld.element(base_index+k);
192                 }
193                 bld.end();
194         }
195
196         base_index += (nsegs+1)*(n_points-1)*2;
197 }
198
199 } // namespace Marklin