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>
8 #include "vehicletype.h"
15 Catalogue3D::Catalogue3D(Catalogue &c):
17 endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
19 add_creator<GL::Material>(&Catalogue3D::create<GL::Material>);
20 add_creator<GL::Mesh>(&Catalogue3D::create<GL::Mesh>);
21 add_creator<GL::Object>(&Catalogue3D::create2<GL::Object>);
22 add_creator<GL::Program>(&Catalogue3D::create<GL::Program>);
23 add_creator<GL::Technique>(&Catalogue3D::create2<GL::Technique>);
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));
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);
32 build_endpoint_mesh();
35 Catalogue3D::~Catalogue3D()
37 for(map<const TrackType *, TrackType3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
39 for(map<const VehicleType *, VehicleType3D *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
43 const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
45 return *get_item(tracks, &tt);
48 const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const
50 return *get_item(vehicles, &vt);
53 void Catalogue3D::track_added(const TrackType &track)
55 tracks[&track] = new TrackType3D(*this, track);
58 void Catalogue3D::vehicle_added(const VehicleType &veh)
60 vehicles[&veh] = new VehicleType3D(*this, veh);
63 void Catalogue3D::build_endpoint_mesh()
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();
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();
73 float gauge = catalogue.get_gauge();
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;
78 GL::MeshBuilder bld(endpoint_mesh);
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);
88 FS::Path Catalogue3D::locate_file(const string &name)
93 FS::Path path = FS::Path("data")/name;
97 throw Exception("Can't locate "+name);
101 T *Catalogue3D::create(const string &name)
103 RefPtr<T> obj = new T;
104 DataFile::load(*obj, locate_file(name).str());
105 return obj.release();
109 T *Catalogue3D::create2(const string &name)
111 RefPtr<T> obj = new T;
112 DataFile::load(*obj, locate_file(name).str(), *this);
113 return obj.release();