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