3 This file is part of R²C²
4 Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/fs/stat.h>
9 #include <msp/gl/meshbuilder.h>
10 #include <msp/gl/object.h>
11 #include <msp/gl/program.h>
12 #include <msp/gl/technique.h>
13 #include "catalogue.h"
14 #include "tracktype.h"
15 #include "vehicletype.h"
22 Catalogue3D::Catalogue3D(Catalogue &c):
24 endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
26 add_creator<GL::Material>(&Catalogue3D::create<GL::Material>);
27 add_creator<GL::Mesh>(&Catalogue3D::create<GL::Mesh>);
28 add_creator<GL::Object>(&Catalogue3D::create2<GL::Object>);
29 add_creator<GL::Program>(&Catalogue3D::create<GL::Program>);
30 add_creator<GL::Technique>(&Catalogue3D::create2<GL::Technique>);
32 catalogue.signal_track_added.connect(sigc::mem_fun(this, &Catalogue3D::track_added));
33 catalogue.signal_vehicle_added.connect(sigc::mem_fun(this, &Catalogue3D::vehicle_added));
35 const Catalogue::TrackMap &trks = catalogue.get_tracks();
36 for(Catalogue::TrackMap::const_iterator i=trks.begin(); i!=trks.end(); ++i)
37 track_added(*i->second);
39 build_endpoint_mesh();
42 Catalogue3D::~Catalogue3D()
44 for(map<const TrackType *, TrackType3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
46 for(map<const VehicleType *, VehicleType3D *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
50 const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
52 map<const TrackType *, TrackType3D *>::const_iterator i = tracks.find(&tt);
54 throw KeyError("Unknown track type");
59 const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const
61 map<const VehicleType *, VehicleType3D *>::const_iterator i = vehicles.find(&vt);
63 throw KeyError("Unknown vehicle type");
68 void Catalogue3D::track_added(const TrackType &track)
70 tracks[&track] = new TrackType3D(*this, track);
73 void Catalogue3D::vehicle_added(const VehicleType &veh)
75 vehicles[&veh] = new VehicleType3D(*this, veh);
78 void Catalogue3D::build_endpoint_mesh()
80 const Profile &ballast_profile = catalogue.get_ballast_profile();
81 const Vector &ballast_min = ballast_profile.get_min_coords();
82 const Vector &ballast_max = ballast_profile.get_max_coords();
84 const Profile &rail_profile = catalogue.get_rail_profile();
85 const Vector &rail_min = rail_profile.get_min_coords();
86 const Vector &rail_max = rail_profile.get_max_coords();
88 float gauge = catalogue.get_gauge();
90 float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
91 float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
93 GL::MeshBuilder bld(endpoint_mesh);
96 bld.vertex(0, width/2, 0);
97 bld.vertex(0, width/2, height);
98 bld.vertex(0, -width/2, height);
99 bld.vertex(0, -width/2, 0);
103 FS::Path Catalogue3D::locate_file(const string &name)
108 FS::Path path = FS::Path("data")/name;
112 throw Exception("Can't locate "+name);
116 T *Catalogue3D::create(const string &name)
118 RefPtr<T> obj = new T;
119 DataFile::load(*obj, locate_file(name).str());
120 return obj.release();
124 T *Catalogue3D::create2(const string &name)
126 RefPtr<T> obj = new T;
127 DataFile::load(*obj, locate_file(name).str(), *this);
128 return obj.release();