]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
935219ed75e268347ea654e20d9aa6f56e7215e3
[r2c2.git] / source / 3d / catalogue.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/object.h>
4 #include <msp/gl/program.h>
5 #include <msp/gl/technique.h>
6 #include "catalogue.h"
7 #include "tracktype.h"
8 #include "vehicletype.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace R2C2 {
14
15 Catalogue3D::Catalogue3D(Catalogue &c):
16         catalogue(c),
17         endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
18 {
19         add_type<GL::Material>().creator(&Catalogue3D::create<GL::Material>);
20         add_type<GL::Mesh>().creator(&Catalogue3D::create<GL::Mesh>);
21         add_type<GL::Object>().creator(&Catalogue3D::create<GL::Object>);
22         add_type<GL::Program>().creator(&Catalogue3D::create<GL::Program>);
23         add_type<GL::Technique>().creator(&Catalogue3D::create<GL::Technique>);
24
25         catalogue.signal_track_added.connect(sigc::mem_fun(this, &Catalogue3D::track_added));
26         catalogue.signal_vehicle_added.connect(sigc::mem_fun(this, &Catalogue3D::vehicle_added));
27
28         const Catalogue::TrackMap &trks = catalogue.get_tracks();
29         for(Catalogue::TrackMap::const_iterator i=trks.begin(); i!=trks.end(); ++i)
30                 track_added(*i->second);
31
32         build_endpoint_mesh();
33 }
34
35 Catalogue3D::~Catalogue3D()
36 {
37         for(map<const TrackType *, TrackType3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
38                 delete i->second;
39         for(map<const VehicleType *, VehicleType3D *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
40                 delete i->second;
41 }
42
43 const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
44 {
45         return *get_item(tracks, &tt);
46 }
47
48 const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const
49 {
50         return *get_item(vehicles, &vt);
51 }
52
53 void Catalogue3D::track_added(const TrackType &track)
54 {
55         tracks[&track] = new TrackType3D(*this, track);
56 }
57
58 void Catalogue3D::vehicle_added(const VehicleType &veh)
59 {
60         vehicles[&veh] = new VehicleType3D(*this, veh);
61 }
62
63 void Catalogue3D::build_endpoint_mesh()
64 {
65         const Profile &ballast_profile = catalogue.get_ballast_profile();
66         const Vector &ballast_min = ballast_profile.get_min_coords();
67         const Vector &ballast_max = ballast_profile.get_max_coords();
68
69         const Profile &rail_profile = catalogue.get_rail_profile();
70         const Vector &rail_min = rail_profile.get_min_coords();
71         const Vector &rail_max = rail_profile.get_max_coords();
72
73         float gauge = catalogue.get_gauge();
74
75         float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
76         float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
77
78         GL::MeshBuilder bld(endpoint_mesh);
79         bld.normal(1, 0, 0);
80         bld.begin(GL::QUADS);
81         bld.vertex(0, width/2, 0);
82         bld.vertex(0, width/2, height);
83         bld.vertex(0, -width/2, height);
84         bld.vertex(0, -width/2, 0);
85         bld.end();
86 }
87
88 FS::Path Catalogue3D::locate_file(const string &name)
89 {
90         if(FS::exists(name))
91                 return name;
92
93         FS::Path path = FS::Path("data")/name;
94         if(FS::exists(path))
95                 return path;
96
97         throw runtime_error("Can't locate "+name);
98 }
99
100 template<typename T>
101 T *Catalogue3D::create(const string &name)
102 {
103         RefPtr<T> obj = new T;
104         DataFile::load(*obj, locate_file(name).str());
105         return obj.release();
106 }
107
108 template<typename T>
109 T *Catalogue3D::create2(const string &name)
110 {
111         RefPtr<T> obj = new T;
112         DataFile::load(*obj, locate_file(name).str(), *this);
113         return obj.release();
114 }
115
116 } // namespace R2C2