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>
7 #include "signaltype.h"
9 #include "vehicletype.h"
16 Catalogue3D::Catalogue3D(Catalogue &c):
18 endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
20 add_type<GL::Material>().creator(&Catalogue3D::create<GL::Material>);
21 add_type<GL::Mesh>().creator(&Catalogue3D::create<GL::Mesh>);
22 add_type<GL::Object>().creator(&Catalogue3D::create<GL::Object>);
23 add_type<GL::Program>().creator(&Catalogue3D::create<GL::Program>);
24 add_type<GL::Technique>().creator(&Catalogue3D::create<GL::Technique>);
26 catalogue.signal_object_added.connect(sigc::mem_fun(this, &Catalogue3D::object_added));
28 const Catalogue::ObjectMap &objs = catalogue.get_all();
29 for(Catalogue::ObjectMap::const_iterator i=objs.begin(); i!=objs.end(); ++i)
30 object_added(*i->second);
32 build_endpoint_mesh();
35 Catalogue3D::~Catalogue3D()
37 for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
41 void Catalogue3D::object_added(const ObjectType &ot)
43 if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
44 objects[&ot] = new TrackType3D(*this, *tt);
45 else if(const SignalType *st = dynamic_cast<const SignalType *>(&ot))
46 objects[&ot] = new SignalType3D(*this, *st);
47 else if(const VehicleType *vt = dynamic_cast<const VehicleType *>(&ot))
48 objects[&ot] = new VehicleType3D(*this, *vt);
51 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
53 return *get_item(objects, &ot);
56 void Catalogue3D::build_endpoint_mesh()
58 const Profile &ballast_profile = catalogue.get_ballast_profile();
59 const Vector &ballast_min = ballast_profile.get_min_coords();
60 const Vector &ballast_max = ballast_profile.get_max_coords();
62 const Profile &rail_profile = catalogue.get_rail_profile();
63 const Vector &rail_min = rail_profile.get_min_coords();
64 const Vector &rail_max = rail_profile.get_max_coords();
66 float gauge = catalogue.get_gauge();
68 float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
69 float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
71 GL::MeshBuilder bld(endpoint_mesh);
74 bld.vertex(0, width/2, 0);
75 bld.vertex(0, width/2, height);
76 bld.vertex(0, -width/2, height);
77 bld.vertex(0, -width/2, 0);
81 FS::Path Catalogue3D::locate_file(const string &name)
86 FS::Path path = FS::Path("data")/name;
90 throw runtime_error("Can't locate "+name);
94 T *Catalogue3D::create(const string &name)
96 RefPtr<T> obj = new T;
97 DataFile::load(*obj, locate_file(name).str());
102 T *Catalogue3D::create2(const string &name)
104 RefPtr<T> obj = new T;
105 DataFile::load(*obj, locate_file(name).str(), *this);
106 return obj.release();