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