]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
Compute Z bounds for Track3D
[r2c2.git] / source / 3d / track.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <GL/gl.h>
10 #include <msp/gl/misc.h>
11 #include "libmarklin/tracktype.h"
12 #include "track.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace Marklin {
18
19 Track3D::Track3D(Track &t, unsigned q):
20         track(t),
21         color(1, 1, 1),
22         varray((GL::NORMAL3, GL::VERTEX3)),
23         quality(q)
24 {
25         build_object();
26 }
27
28 void Track3D::set_color(const Msp::GL::Color &c)
29 {
30         color = c;
31 }
32
33 void Track3D::set_quality(unsigned q)
34 {
35         quality = q;
36         build_object();
37 }
38
39 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
40 {
41         const Point &pos = track.get_position();
42         float rot = track.get_rotation();
43         float slope = track.get_slope();
44
45         float c = cos(-angle);
46         float s = sin(-angle);
47
48         minp.x = maxp.x = c*pos.x-s*pos.y;
49         minp.y = maxp.y = s*pos.x+c*pos.y;
50         minp.z = pos.z+min(slope, 0.0f);
51         maxp.z = pos.z+0.01+max(slope, 0.0f);
52
53         float c2 = cos(rot-angle);
54         float s2 = sin(rot-angle);
55
56         for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
57         {
58                 float x = c*pos.x-s*pos.y + c2*i->x-s2*i->y;
59                 float y = s*pos.x+c*pos.y + s2*i->x+c2*i->y;
60
61                 minp.x = min(minp.x, x);
62                 minp.y = min(minp.y, y);
63                 maxp.x = max(maxp.x, x);
64                 maxp.y = max(maxp.y, y);
65         }
66 }
67
68 void Track3D::render() const
69 {
70         prepare_render(true);
71
72         glPushName(reinterpret_cast<unsigned>(this));
73
74         varray.apply();
75         glColor4f(0.25*color.r, 0.25*color.g, 0.25*color.b, 1);
76         glDrawElements(GL_QUADS, base_seq.size(), GL_UNSIGNED_INT, &base_seq[0]);
77         if(quality>1)
78         {
79                 glColor4f(0.85*color.r, 0.85*color.g, 0.85*color.b, 1);
80                 glDrawElements(GL_QUADS, rail_seq.size(), GL_UNSIGNED_INT, &rail_seq[0]);
81         }
82
83         glPopName();
84         glPopMatrix();
85 }
86
87 void Track3D::render_endpoints() const
88 {
89         prepare_render(false);
90
91         const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
92         for(unsigned i=0; i<endpoints.size(); ++i)
93         {
94                 const Endpoint &ep = endpoints[i];
95                 GL::set(GL_CULL_FACE, track.get_link(i));
96                 if(track.get_link(i))
97                         glColor4f(0.5, 0, 1, 0.5);
98                 else
99                         glColor4f(1, 0, 0.5, 0.5);
100
101                 float c = cos(ep.dir);
102                 float s = sin(ep.dir);
103                 float z = (i==1 ? track.get_slope() : 0);
104
105                 glBegin(GL_QUADS);
106                 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z);
107                 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z);
108                 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z+0.02);
109                 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z+0.02);
110                 glEnd();
111         }
112
113         if(abs(track.get_slope())>1e-4)
114         {
115                 Point p;
116                 if(track.get_slope()>0)
117                 {
118                         p = endpoints[1].pos;
119                         p.z += track.get_slope();
120                 }
121                 else
122                         p = endpoints[0].pos;
123                 glBegin(GL_TRIANGLE_STRIP);
124                 for(unsigned i=0; i<=16; ++i)
125                 {
126                         float c = cos(i*M_PI/8);
127                         float s = sin(i*M_PI/8);
128                         glNormal3f(c, s, 0);
129                         glVertex3f(p.x+c*0.01, p.y+s*0.01, p.z);
130                         glVertex3f(p.x+c*0.01, p.y+s*0.01, -track.get_position().z);
131                 }
132                 glEnd();
133         }
134
135         glPopMatrix();
136 }
137
138 void Track3D::render_path(int path) const
139 {
140         prepare_render(true);
141
142         varray.apply();
143         if(path>=0 && static_cast<unsigned>(path)<path_seq.size())
144                 glDrawElements(GL_QUADS, path_seq[path].size(), GL_UNSIGNED_INT, &path_seq[path][0]);
145         else
146         {
147                 for(unsigned i=0; i<path_seq.size(); ++i)
148                         glDrawElements(GL_QUADS, path_seq[i].size(), GL_UNSIGNED_INT, &path_seq[i][0]);
149         }
150
151         glPopMatrix();
152 }
153
154 void Track3D::prepare_render(bool slope) const
155 {
156         const Point &pos = track.get_position();
157         float rot = track.get_rotation();
158
159         glPushMatrix();
160         glTranslatef(pos.x, pos.y, pos.z);
161         glRotatef(rot*180/M_PI, 0, 0, 1);
162         if(slope)
163                 glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
164 }
165
166 void Track3D::build_object()
167 {
168         varray.clear();
169         GL::VertexArrayBuilder builder(varray);
170
171         base_seq.clear();
172         rail_seq.clear();
173         path_seq.clear();
174         path_seq.resize(track.get_type().get_n_paths());
175
176         const vector<TrackPart> &parts = track.get_type().get_parts();
177         unsigned index = 0;
178         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
179                 build_part(*i, builder, index);
180 }
181
182 void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index)
183 {
184         static vector<Point> profile;
185         if(profile.empty())
186         {
187                 profile.push_back(Point(0, -0.02, 0));
188                 profile.push_back(Point(0, -0.014, 0.008));
189                 profile.push_back(Point(0, -0.014, 0.008));
190                 profile.push_back(Point(0, 0.014, 0.008));
191                 profile.push_back(Point(0, 0.014, 0.008));
192                 profile.push_back(Point(0, 0.02, 0));
193                 for(unsigned i=0; i<2; ++i)
194                 {
195                         profile.push_back(Point(0, -0.009+i*0.017, 0.008));
196                         profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
197                         profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
198                         profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
199                         profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
200                         profile.push_back(Point(0, -0.008+i*0.017, 0.008));
201                 }
202                 profile.push_back(Point(0, -0.002, 0.012));
203                 profile.push_back(Point(0, 0.002, 0.012));
204         }
205         static unsigned psize = profile.size();
206
207         unsigned nsegs = (part.radius ? static_cast<unsigned>(part.length*(1<<quality))+1 : 1);
208         float plen = part.length;
209         if(part.radius)
210                 plen *= abs(part.radius);
211
212         for(unsigned i=0; i<=nsegs; ++i)
213         {
214                 float a = part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
215                 float c = cos(a);
216                 float s = sin(a);
217                 Point p = part.get_point(i*plen/nsegs);
218
219                 for(unsigned j=0; j<psize; ++j)
220                 {
221                         unsigned k = j&~1;
222                         float dy = profile[k+1].y-profile[k].y;
223                         float dz = profile[k+1].z-profile[k].z;
224                         float d = sqrt(dy*dy+dz*dz);
225                         va_builder.normal(s*dz/d, -c*dz/d, dy/d);
226
227                         Point v(p.x+c*profile[j].x-s*profile[j].y, p.y+c*profile[j].y+s*profile[j].x, profile[j].z);
228                         va_builder.vertex(v.x, v.y, v.z);
229                         if(profile[j].z==0)
230                                 border.push_back(v);
231                 }
232         }
233
234         for(unsigned i=0; i<nsegs; ++i)
235         {
236                 for(unsigned j=0; j<3; ++j)
237                 {
238                         base_seq.push_back(base_index+i*psize+j*2);
239                         base_seq.push_back(base_index+(i+1)*psize+j*2);
240                         base_seq.push_back(base_index+(i+1)*psize+1+j*2);
241                         base_seq.push_back(base_index+i*psize+1+j*2);
242                 }
243                 for(unsigned j=3; j<9; ++j)
244                 {
245                         rail_seq.push_back(base_index+i*psize+j*2);
246                         rail_seq.push_back(base_index+(i+1)*psize+j*2);
247                         rail_seq.push_back(base_index+(i+1)*psize+1+j*2);
248                         rail_seq.push_back(base_index+i*psize+1+j*2);
249                 }
250                 path_seq[part.path].push_back(base_index+i*psize+18);
251                 path_seq[part.path].push_back(base_index+(i+1)*psize+18);
252                 path_seq[part.path].push_back(base_index+(i+1)*psize+19);
253                 path_seq[part.path].push_back(base_index+i*psize+19);
254         }
255
256         base_index += (nsegs+1)*psize;
257 }
258
259 } // namespace Marklin