]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
Correctly handle slope when snapping tracks
[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
44         float c = cos(-angle);
45         float s = sin(-angle);
46
47         minp.x = maxp.x = c*pos.x-s*pos.y;
48         minp.y = maxp.y = s*pos.x+c*pos.y;
49
50         float c2 = cos(rot-angle);
51         float s2 = sin(rot-angle);
52
53         for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
54         {
55                 float x = c*pos.x-s*pos.y + c2*i->x-s2*i->y;
56                 float y = s*pos.x+c*pos.y + s2*i->x+c2*i->y;
57
58                 minp.x = min(minp.x, x);
59                 minp.y = min(minp.y, y);
60                 maxp.x = max(maxp.x, x);
61                 maxp.y = max(maxp.y, y);
62         }
63 }
64
65 void Track3D::render() const
66 {
67         prepare_render(true);
68
69         glPushName(reinterpret_cast<unsigned>(this));
70
71         varray.apply();
72         glColor4f(0.25*color.r, 0.25*color.g, 0.25*color.b, 1);
73         glDrawElements(GL_QUADS, base_seq.size(), GL_UNSIGNED_INT, &base_seq[0]);
74         if(quality>1)
75         {
76                 glColor4f(0.85*color.r, 0.85*color.g, 0.85*color.b, 1);
77                 glDrawElements(GL_QUADS, rail_seq.size(), GL_UNSIGNED_INT, &rail_seq[0]);
78         }
79
80         glPopName();
81         glPopMatrix();
82 }
83
84 void Track3D::render_endpoints() const
85 {
86         prepare_render(false);
87
88         const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
89         for(unsigned i=0; i<endpoints.size(); ++i)
90         {
91                 const Endpoint &ep = endpoints[i];
92                 GL::set(GL_CULL_FACE, track.get_link(i));
93                 if(track.get_link(i))
94                         glColor4f(0.5, 0, 1, 0.5);
95                 else
96                         glColor4f(1, 0, 0.5, 0.5);
97
98                 float c = cos(ep.dir);
99                 float s = sin(ep.dir);
100                 float z = (i==1 ? track.get_slope() : 0);
101
102                 glBegin(GL_QUADS);
103                 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z);
104                 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z);
105                 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z+0.02);
106                 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z+0.02);
107                 glEnd();
108         }
109
110         if(abs(track.get_slope())>1e-4)
111         {
112                 Point p;
113                 if(track.get_slope()>0)
114                 {
115                         p = endpoints[1].pos;
116                         p.z += track.get_slope();
117                 }
118                 else
119                         p = endpoints[0].pos;
120                 glBegin(GL_TRIANGLE_STRIP);
121                 for(unsigned i=0; i<=16; ++i)
122                 {
123                         float c = cos(i*M_PI/8);
124                         float s = sin(i*M_PI/8);
125                         glNormal3f(c, s, 0);
126                         glVertex3f(p.x+c*0.01, p.y+s*0.01, p.z);
127                         glVertex3f(p.x+c*0.01, p.y+s*0.01, -track.get_position().z);
128                 }
129                 glEnd();
130         }
131
132         glPopMatrix();
133 }
134
135 void Track3D::render_route(int route) const
136 {
137         prepare_render(true);
138
139         varray.apply();
140         if(route>=0 && static_cast<unsigned>(route)<route_seq.size())
141                 glDrawElements(GL_QUADS, route_seq[route].size(), GL_UNSIGNED_INT, &route_seq[route][0]);
142         else
143         {
144                 for(unsigned i=0; i<route_seq.size(); ++i)
145                         glDrawElements(GL_QUADS, route_seq[i].size(), GL_UNSIGNED_INT, &route_seq[i][0]);
146         }
147
148         glPopMatrix();
149 }
150
151 void Track3D::prepare_render(bool slope) const
152 {
153         const Point &pos = track.get_position();
154         float rot = track.get_rotation();
155
156         glPushMatrix();
157         glTranslatef(pos.x, pos.y, pos.z);
158         glRotatef(rot*180/M_PI, 0, 0, 1);
159         if(slope)
160                 glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
161 }
162
163 void Track3D::build_object()
164 {
165         varray.clear();
166         GL::VertexArrayBuilder builder(varray);
167
168         base_seq.clear();
169         rail_seq.clear();
170         route_seq.clear();
171         route_seq.resize(track.get_type().get_n_routes());
172
173         const vector<TrackPart> &parts = track.get_type().get_parts();
174         unsigned index = 0;
175         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
176                 build_part(*i, builder, index);
177 }
178
179 void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index)
180 {
181         static vector<Point> profile;
182         if(profile.empty())
183         {
184                 profile.push_back(Point(0, -0.02, 0));
185                 profile.push_back(Point(0, -0.014, 0.008));
186                 profile.push_back(Point(0, -0.014, 0.008));
187                 profile.push_back(Point(0, 0.014, 0.008));
188                 profile.push_back(Point(0, 0.014, 0.008));
189                 profile.push_back(Point(0, 0.02, 0));
190                 for(unsigned i=0; i<2; ++i)
191                 {
192                         profile.push_back(Point(0, -0.009+i*0.017, 0.008));
193                         profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
194                         profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
195                         profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
196                         profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
197                         profile.push_back(Point(0, -0.008+i*0.017, 0.008));
198                 }
199                 profile.push_back(Point(0, -0.002, 0.012));
200                 profile.push_back(Point(0, 0.002, 0.012));
201         }
202         static unsigned psize = profile.size();
203
204         unsigned nsegs = (part.radius ? static_cast<unsigned>(part.length*(1<<quality))+1 : 1);
205         float plen = part.length;
206         if(part.radius)
207                 plen *= abs(part.radius);
208
209         for(unsigned i=0; i<=nsegs; ++i)
210         {
211                 float a = part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
212                 float c = cos(a);
213                 float s = sin(a);
214                 Point p = part.get_point(i*plen/nsegs);
215
216                 for(unsigned j=0; j<psize; ++j)
217                 {
218                         unsigned k = j&~1;
219                         float dy = profile[k+1].y-profile[k].y;
220                         float dz = profile[k+1].z-profile[k].z;
221                         float d = sqrt(dy*dy+dz*dz);
222                         va_builder.normal(s*dz/d, -c*dz/d, dy/d);
223
224                         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);
225                         va_builder.vertex(v.x, v.y, v.z);
226                         if(profile[j].z==0)
227                                 border.push_back(v);
228                 }
229         }
230
231         for(unsigned i=0; i<nsegs; ++i)
232         {
233                 for(unsigned j=0; j<3; ++j)
234                 {
235                         base_seq.push_back(base_index+i*psize+j*2);
236                         base_seq.push_back(base_index+(i+1)*psize+j*2);
237                         base_seq.push_back(base_index+(i+1)*psize+1+j*2);
238                         base_seq.push_back(base_index+i*psize+1+j*2);
239                 }
240                 for(unsigned j=3; j<9; ++j)
241                 {
242                         rail_seq.push_back(base_index+i*psize+j*2);
243                         rail_seq.push_back(base_index+(i+1)*psize+j*2);
244                         rail_seq.push_back(base_index+(i+1)*psize+1+j*2);
245                         rail_seq.push_back(base_index+i*psize+1+j*2);
246                 }
247                 route_seq[part.route].push_back(base_index+i*psize+18);
248                 route_seq[part.route].push_back(base_index+(i+1)*psize+18);
249                 route_seq[part.route].push_back(base_index+(i+1)*psize+19);
250                 route_seq[part.route].push_back(base_index+i*psize+19);
251         }
252
253         base_index += (nsegs+1)*psize;
254 }
255
256 } // namespace Marklin