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