]> git.tdb.fi Git - r2c2.git/blob - source/3d/vehicletype.cpp
Rendering improvements
[r2c2.git] / source / 3d / vehicletype.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gl/meshbuilder.h>
9 #include <msp/gl/vector.h>
10 #include "catalogue.h"
11 #include "vehicletype.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace Marklin {
17
18 VehicleType3D::VehicleType3D(Catalogue3D &, const VehicleType &vt):
19         body_object(0),
20         axle_objects(1)
21 {
22         body_object = get_object(vt.get_object());
23
24         const vector<VehicleType::Axle> &axles = vt.get_axles();
25         for(vector<VehicleType::Axle>::const_iterator i=axles.begin(); i!=axles.end(); ++i)
26                 axle_objects[0].push_back(get_object(i->object));
27
28         const vector<VehicleType::Bogie> &bogies = vt.get_bogies();
29         for(vector<VehicleType::Bogie>::const_iterator i=bogies.begin(); i!=bogies.end(); ++i)
30         {
31                 bogie_objects.push_back(get_object(i->object));
32                 axle_objects.push_back(vector<GL::Object *>());
33                 for(vector<VehicleType::Axle>::const_iterator j=i->axles.begin(); j!=i->axles.end(); ++j)
34                         axle_objects.back().push_back(get_object(j->object));
35         }
36 }
37
38 VehicleType3D::~VehicleType3D()
39 {
40         for(map<string, GL::Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
41                 delete i->second;
42 }
43
44 const GL::Object *VehicleType3D::get_axle_object(unsigned i) const
45 {
46         if(i>=axle_objects[0].size())
47                 throw InvalidParameterValue("Axle index out of range");
48         return axle_objects[0][i];
49 }
50
51 const GL::Object *VehicleType3D::get_bogie_object(unsigned i) const
52 {
53         if(i>=bogie_objects.size())
54                 throw InvalidParameterValue("Bogie index out of range");
55         return bogie_objects[i];
56 }
57
58 const GL::Object *VehicleType3D::get_bogie_axle_object(unsigned i, unsigned j) const
59 {
60         if(i>=bogie_objects.size())
61                 throw InvalidParameterValue("Bogie index out of range");
62         if(j>=axle_objects[i+1].size())
63                 throw InvalidParameterValue("Axle index out of range");
64         return axle_objects[i+1][j];
65 }
66
67 GL::Object *VehicleType3D::get_object(const string &name)
68 {
69         if(name.empty())
70                 return 0;
71
72         GL::Object *&ptr = objects[name];
73         if(!ptr)
74         {
75                 ptr = new GL::Object;
76                 DataFile::load(*ptr, name);
77         }
78         return ptr;
79 }
80
81 } // namespace Marklin