1 #include <msp/core/maputils.h>
2 #include <msp/fs/stat.h>
3 #include <msp/gl/meshbuilder.h>
4 #include <msp/gl/object.h>
5 #include <msp/gl/program.h>
6 #include <msp/gl/technique.h>
8 #include "signaltype.h"
10 #include "vehicletype.h"
17 Catalogue3D::Catalogue3D(Catalogue &c):
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::create2<GL::Object>);
23 add_type<GL::Program>().creator(&Catalogue3D::create<GL::Program>);
24 add_type<GL::Technique>().creator(&Catalogue3D::create2<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);
33 Catalogue3D::~Catalogue3D()
35 for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
39 void Catalogue3D::object_added(const ObjectType &ot)
41 if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
43 objects[&ot] = new TrackType3D(*this, *tt);
44 const TrackAppearance &appearance = tt->get_appearance();
45 if(!endpoint_meshes.count(&appearance))
46 build_endpoint_mesh(appearance);
48 else if(const SignalType *st = dynamic_cast<const SignalType *>(&ot))
49 objects[&ot] = new SignalType3D(*this, *st);
50 else if(const VehicleType *vt = dynamic_cast<const VehicleType *>(&ot))
51 objects[&ot] = new VehicleType3D(*this, *vt);
54 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
56 return *get_item(objects, &ot);
59 void Catalogue3D::build_endpoint_mesh(const TrackAppearance &appearance)
61 const Profile &ballast_profile = appearance.get_ballast_profile();
62 const Vector &ballast_min = ballast_profile.get_min_coords();
63 const Vector &ballast_max = ballast_profile.get_max_coords();
65 const Profile &rail_profile = appearance.get_rail_profile();
66 const Vector &rail_min = rail_profile.get_min_coords();
67 const Vector &rail_max = rail_profile.get_max_coords();
69 float gauge = catalogue.get_gauge();
71 float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
72 float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
74 GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
75 GL::MeshBuilder bld(*mesh);
78 bld.vertex(0, width/2, 0);
79 bld.vertex(0, width/2, height);
80 bld.vertex(0, -width/2, height);
81 bld.vertex(0, -width/2, 0);
84 endpoint_meshes[&appearance] = mesh;
87 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
89 return *get_item(endpoint_meshes, &appearance);
92 FS::Path Catalogue3D::locate_file(const string &name)
97 FS::Path path = FS::Path("data")/name;
101 throw runtime_error("Can't locate "+name);
105 T *Catalogue3D::create(const string &name)
107 RefPtr<T> obj = new T;
108 DataFile::load(*obj, locate_file(name).str());
109 return obj.release();
113 T *Catalogue3D::create2(const string &name)
115 RefPtr<T> obj = new T;
116 DataFile::load(*obj, locate_file(name).str(), *this);
117 return obj.release();