1 #include <msp/core/maputils.h>
2 #include <msp/gl/meshbuilder.h>
4 #include "signaltype.h"
6 #include "vehicletype.h"
13 Catalogue3D::Catalogue3D(Catalogue &c):
16 src.add_directory(".");
17 src.add_directory("data");
20 catalogue.signal_object_added.connect(sigc::mem_fun(this, &Catalogue3D::object_added));
22 const Catalogue::ObjectMap &objs = catalogue.get_all();
23 for(Catalogue::ObjectMap::const_iterator i=objs.begin(); i!=objs.end(); ++i)
24 object_added(*i->second);
27 Catalogue3D::~Catalogue3D()
29 for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
33 void Catalogue3D::object_added(const ObjectType &ot)
35 if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
37 objects[&ot] = new TrackType3D(*this, *tt);
38 const TrackAppearance &appearance = tt->get_appearance();
39 if(!endpoint_meshes.count(&appearance))
40 build_endpoint_mesh(appearance);
42 else if(const SignalType *st = dynamic_cast<const SignalType *>(&ot))
43 objects[&ot] = new SignalType3D(*this, *st);
44 else if(const VehicleType *vt = dynamic_cast<const VehicleType *>(&ot))
45 objects[&ot] = new VehicleType3D(*this, *vt);
48 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
50 return *get_item(objects, &ot);
53 void Catalogue3D::build_endpoint_mesh(const TrackAppearance &appearance)
55 const Profile &ballast_profile = appearance.get_ballast_profile();
56 const Vector &ballast_min = ballast_profile.get_min_coords();
57 const Vector &ballast_max = ballast_profile.get_max_coords();
59 const Profile &rail_profile = appearance.get_rail_profile();
60 const Vector &rail_min = rail_profile.get_min_coords();
61 const Vector &rail_max = rail_profile.get_max_coords();
63 float gauge = appearance.get_gauge();
65 float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
66 float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
68 GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
69 GL::MeshBuilder bld(*mesh);
72 bld.vertex(0, width/2, 0);
73 bld.vertex(0, width/2, height);
74 bld.vertex(0, -width/2, height);
75 bld.vertex(0, -width/2, 0);
78 endpoint_meshes[&appearance] = mesh;
81 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
83 return *get_item(endpoint_meshes, &appearance);