]> git.tdb.fi Git - r2c2.git/blob - source/3d/vehicletype.cpp
162b2c42110a4cd1d67100e43c5fe04e01124ee2
[r2c2.git] / source / 3d / vehicletype.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010-2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gl/meshbuilder.h>
9 #include <msp/gl/technique.h>
10 #include <msp/gl/texture2d.h>
11 #include <msp/gl/vector.h>
12 #include "catalogue.h"
13 #include "vehicletype.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 namespace {
19
20 template<typename T>
21 T get(const map<string, string> &params, const string &key, T def = T())
22 {
23         map<string, string>::const_iterator i = params.find(key);
24         if(i==params.end())
25                 return def;
26
27         return lexical_cast<T>(i->second);
28 }
29
30 }
31
32 namespace R2C2 {
33
34 VehicleType3D::VehicleType3D(Catalogue3D &c, const VehicleType &t):
35         catalogue(c),
36         type(t),
37         body_object(0),
38         axle_objects(1)
39 {
40         body_object = get_object(type.get_object());
41
42         const vector<VehicleType::Axle> &axles = type.get_fixed_axles();
43         for(vector<VehicleType::Axle>::const_iterator i=axles.begin(); i!=axles.end(); ++i)
44                 axle_objects[0].push_back(get_object(i->object));
45
46         const vector<VehicleType::Bogie> &bogies = type.get_bogies();
47         for(vector<VehicleType::Bogie>::const_iterator i=bogies.begin(); i!=bogies.end(); ++i)
48         {
49                 bogie_objects.push_back(get_object(i->object));
50                 axle_objects.push_back(vector<GL::Object *>());
51                 for(vector<VehicleType::Axle>::const_iterator j=i->axles.begin(); j!=i->axles.end(); ++j)
52                         axle_objects.back().push_back(get_object(j->object));
53         }
54
55         const vector<VehicleType::Rod> &rods = type.get_rods();
56         for(vector<VehicleType::Rod>::const_iterator i=rods.begin(); i!=rods.end(); ++i)
57                 rod_objects.push_back(get_object(i->object));
58 }
59
60 VehicleType3D::~VehicleType3D()
61 {
62         for(map<string, GL::Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
63                 delete i->second;
64 }
65
66 const GL::Object *VehicleType3D::get_fixed_axle_object(unsigned i) const
67 {
68         if(i>=axle_objects[0].size())
69                 throw InvalidParameterValue("Axle index out of range");
70         return axle_objects[0][i];
71 }
72
73 const GL::Object *VehicleType3D::get_bogie_object(unsigned i) const
74 {
75         if(i>=bogie_objects.size())
76                 throw InvalidParameterValue("Bogie index out of range");
77         return bogie_objects[i];
78 }
79
80 const GL::Object *VehicleType3D::get_bogie_axle_object(unsigned i, unsigned j) const
81 {
82         if(i>=bogie_objects.size())
83                 throw InvalidParameterValue("Bogie index out of range");
84         if(j>=axle_objects[i+1].size())
85                 throw InvalidParameterValue("Axle index out of range");
86         return axle_objects[i+1][j];
87 }
88
89 const GL::Object *VehicleType3D::get_rod_object(unsigned i) const
90 {
91         if(i>=rod_objects.size())
92                 throw InvalidParameterValue("Rod index out of range");
93         return rod_objects[i];
94 }
95
96 GL::Object *VehicleType3D::get_object(const string &name)
97 {
98         if(name.empty())
99                 return 0;
100
101         GL::Object *&ptr = objects[name];
102         if(!ptr)
103         {
104                 if(name[0]==':')
105                 {
106                         string::size_type colon = name.find(':', 1);
107                         string kind = name.substr(1, colon-1);
108
109                         map<string, string> params;
110                         params["length"] = lexical_cast(type.get_length()*1000);
111                         params["width"] = lexical_cast(type.get_width()*1000);
112                         params["height"] = lexical_cast(type.get_height()*1000);
113                         if(colon!=string::npos)
114                         {
115                                 string::size_type start = colon+1;
116                                 while(1)
117                                 {
118                                         string::size_type equals = name.find('=', start);
119                                         string::size_type comma = name.find(',', start);
120                                         if(equals<comma)
121                                                 params[name.substr(start, equals-start)] = name.substr(equals+1, comma-equals-1);
122                                         else
123                                                 params[name.substr(start, comma-start)] = string();
124
125                                         if(comma==string::npos)
126                                                 break;
127                                         start = comma+1;
128                                 }
129                         }
130
131                         GL::Mesh *mesh = 0;
132                         if(kind=="openwagon")
133                                 mesh = create_open_wagon(params);
134                         else if(kind=="coveredwagon")
135                                 mesh = create_covered_wagon(params);
136                         else if(kind=="flatwagon")
137                                 mesh = create_flat_wagon(params);
138
139                         if(mesh)
140                         {
141                                 ptr = new GL::Object;
142                                 ptr->set_mesh(mesh);
143                                 ptr->set_technique(create_technique(params));
144                         }
145                 }
146                 else
147                         return catalogue.get<GL::Object>(name);
148         }
149         return ptr;
150 }
151
152 GL::Technique *VehicleType3D::create_technique(const map<string, string> &params)
153 {
154         string color_str = get<string>(params, "color", "808080");
155         unsigned color = lexical_cast<unsigned>(color_str, "x");
156         string color2_str = get<string>(params, "color2", color_str);
157         unsigned color2 = lexical_cast<unsigned>(color2_str, "x");
158
159         GL::Technique *tech = new GL::Technique;
160         GL::RenderPass &pass = tech->add_pass(GL::Tag());
161         GL::Material *mat = new GL::Material;
162         mat->set_diffuse(GL::Color(1));
163         pass.set_material(mat);
164         GL::Texture2D *tex = new GL::Texture2D;
165         tex->storage(GL::RGB, 2, 1);
166         tex->set_min_filter(GL::NEAREST);
167         tex->set_mag_filter(GL::NEAREST);
168         unsigned char data[6] = { color>>16, color>>8, color, color2>>16, color2>>8, color2 };
169         tex->image(0, GL::RGB, GL::UNSIGNED_BYTE, data);
170         pass.set_texture(0, tex);
171
172         return tech;
173 }
174
175 GL::Mesh *VehicleType3D::create_open_wagon(const map<string, string> &params)
176 {
177         float length = get<float>(params, "length")/1000;
178         float width = get<float>(params, "width")/1000;
179         float height = get<float>(params, "height")/1000;
180         float clearance = get<float>(params, "clearance", 900*catalogue.get_catalogue().get_scale())/1000;
181         float border = get<float>(params, "border", 40*catalogue.get_catalogue().get_scale())/1000;
182
183         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3));
184         GL::MeshBuilder bld(*mesh);
185
186         for(unsigned i=0; i<16; ++i)
187         {
188                 float x = length/2;
189                 float y = width/2;
190                 if(i>=8)
191                 {
192                         x -= border;
193                         y -= border;
194                 }
195                 if((i+1)%4<2)
196                         x = -x;
197                 if(i%4<2)
198                         y = -y;
199
200                 float z = height;
201                 if(i<4)
202                         z = clearance;
203                 else if(i>=12)
204                         z = clearance+0.1*catalogue.get_catalogue().get_scale();
205
206                 bld.texcoord((i>=12 ? 0.75 : 0.25), 0.5);
207                 bld.normal(0, 0, (i<4 ? -1 : 1));
208                 bld.vertex(x, y, z);
209                 bld.texcoord((i>=8 ? 0.75 : 0.25), 0.5);
210                 bld.normal(((x<0)==(i<8) ? -1 : 1), 0, 0);
211                 bld.vertex(x, y, z);
212                 bld.normal(0, ((y<0)==(i<8) ? -1 : 1), 0);
213                 bld.vertex(x, y, z);
214         }
215
216         bld.begin(GL::QUADS);
217         for(unsigned i=0; i<3; ++i)
218                 for(unsigned j=0; j<4; ++j)
219                 {
220                         unsigned k = (i==1 ? 0 : 2-j%2);
221                         bld.element((i*4+j)*3+k);
222                         bld.element((i*4+(j+1)%4)*3+k);
223                         bld.element(((i+1)*4+(j+1)%4)*3+k);
224                         bld.element(((i+1)*4+j)*3+k);
225                 }
226         for(unsigned i=4; i--;)
227                 bld.element(i*3);
228         for(unsigned i=0; i<4; ++i)
229                 bld.element((12+i)*3);
230         bld.end();
231
232         return mesh;
233 }
234
235 GL::Mesh *VehicleType3D::create_covered_wagon(const map<string, string> &params)
236 {
237         float length = get<float>(params, "length")/1000;
238         float width = get<float>(params, "width")/1000;
239         float height = get<float>(params, "height")/1000;
240         float clearance = get<float>(params, "clearance", 900*catalogue.get_catalogue().get_scale())/1000;
241         float roof = get<float>(params, "roof", width*250)/1000;
242
243         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3));
244         GL::MeshBuilder bld(*mesh);
245
246         for(unsigned i=0; i<12; ++i)
247         {
248                 float x = length/2;
249                 float y = width/2;
250                 if(i>=8)
251                         y /= 3;
252                 if((i+1)%4<2)
253                         x = -x;
254                 if(i%4<2)
255                         y = -y;
256
257                 float z = height;
258                 if(i<4)
259                         z = clearance;
260                 else if(i<8)
261                         z -= roof;
262
263                 if(i<4)
264                 {
265                         bld.normal(0, 0, -1);
266                         bld.texcoord(0.25, 0.5);
267                 }
268                 else
269                 {
270                         float a = atan2(roof/(i>=8 ? 3 : 1), width/2);
271                         if(y<0)
272                                 a = -a;
273                         bld.normal(0, sin(a), cos(a));
274                         bld.texcoord(0.75, 0.5);
275                 }
276                 bld.vertex(x, y, z);
277
278                 bld.texcoord(0.25, 0.5);
279                 bld.normal((x<0 ? -1 : 1), 0, 0);
280                 bld.vertex(x, y, z);
281                 bld.normal(0, (y<0 ? -1 : 1), 0);
282                 bld.vertex(x, y, z);
283         }
284
285         bld.begin(GL::QUADS);
286         for(unsigned i=0; i<2; ++i)
287                 for(unsigned j=0; j<4; ++j)
288                 {
289                         unsigned k = ((i==1 && j%2==0) ? 0 : 2-j%2);
290                         bld.element((i*4+j)*3+k);
291                         bld.element((i*4+(j+1)%4)*3+k);
292                         bld.element(((i+1)*4+(j+1)%4)*3+k);
293                         bld.element(((i+1)*4+j)*3+k);
294                 }
295         for(unsigned i=4; i--;)
296                 bld.element(i*3);
297         for(unsigned i=0; i<4; ++i)
298                 bld.element((8+i)*3);
299         bld.end();
300
301         return mesh;
302 }
303
304 GL::Mesh *VehicleType3D::create_flat_wagon(const map<string, string> &params)
305 {
306         float length = get<float>(params, "length")/1000;
307         float width = get<float>(params, "width")/1000;
308         float height = get<float>(params, "height")/1000;
309         float clearance = get<float>(params, "clearance", 900*catalogue.get_catalogue().get_scale())/1000;
310
311         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::TEXCOORD2, GL::VERTEX3));
312         GL::MeshBuilder bld(*mesh);
313
314         for(unsigned i=0; i<8; ++i)
315         {
316                 float x = length/2;
317                 float y = width/2;
318                 if((i+1)%4<2)
319                         x = -x;
320                 if(i%4<2)
321                         y = -y;
322
323                 float z = (i<4 ? clearance : height);
324
325                 bld.texcoord((i>=4 ? 0.75 : 0.25), 0.5);
326                 bld.normal(0, 0, (i<4 ? -1 : 1));
327                 bld.vertex(x, y, z);
328                 bld.texcoord(0.25, 0.5);
329                 bld.normal(((x<0)==(i<8) ? -1 : 1), 0, 0);
330                 bld.vertex(x, y, z);
331                 bld.normal(0, ((y<0)==(i<8) ? -1 : 1), 0);
332                 bld.vertex(x, y, z);
333         }
334
335         bld.begin(GL::QUADS);
336         for(unsigned i=0; i<4; ++i)
337         {
338                 unsigned j = 2-i%2;
339                 bld.element(i*3+j);
340                 bld.element(((i+1)%4)*3+j);
341                 bld.element((4+(i+1)%4)*3+j);
342                 bld.element((4+i)*3+j);
343         }
344         for(unsigned i=4; i--;)
345                 bld.element(i*3);
346         for(unsigned i=0; i<4; ++i)
347                 bld.element((4+i)*3);
348         bld.end();
349
350         return mesh;
351 }
352
353 } // namespace R2C2