-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
+#include <msp/core/maputils.h>
+#include <msp/fs/stat.h>
#include <msp/gl/meshbuilder.h>
-#include "libmarklin/locotype.h"
+#include <msp/gl/object.h>
+#include <msp/gl/program.h>
+#include <msp/gl/technique.h>
#include "catalogue.h"
+#include "signaltype.h"
#include "tracktype.h"
#include "vehicletype.h"
using namespace std;
using namespace Msp;
-namespace Marklin {
+namespace R2C2 {
Catalogue3D::Catalogue3D(Catalogue &c):
- catalogue(c),
- endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
+ catalogue(c)
{
- catalogue.signal_track_added.connect(sigc::mem_fun(this, &Catalogue3D::track_added));
- catalogue.signal_loco_added.connect(sigc::mem_fun(this, &Catalogue3D::loco_added));
+ add_type<GL::Material>().creator(&Catalogue3D::create<GL::Material>);
+ add_type<GL::Mesh>().creator(&Catalogue3D::create<GL::Mesh>);
+ add_type<GL::Object>().creator(&Catalogue3D::create2<GL::Object>);
+ add_type<GL::Program>().creator(&Catalogue3D::create<GL::Program>);
+ add_type<GL::Technique>().creator(&Catalogue3D::create2<GL::Technique>);
- const map<unsigned, TrackType *> &trks = catalogue.get_tracks();
- for(map<unsigned, TrackType *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
- track_added(*i->second);
+ catalogue.signal_object_added.connect(sigc::mem_fun(this, &Catalogue3D::object_added));
- ballast_material.set_diffuse(GL::Color(0.25, 0.25, 0.25));
- rail_material.set_diffuse(GL::Color(0.85, 0.85, 0.85));
-
- build_endpoint_mesh();
+ const Catalogue::ObjectMap &objs = catalogue.get_all();
+ for(Catalogue::ObjectMap::const_iterator i=objs.begin(); i!=objs.end(); ++i)
+ object_added(*i->second);
}
Catalogue3D::~Catalogue3D()
{
- for(map<const TrackType *, TrackType3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
delete i->second;
}
-const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
+void Catalogue3D::object_added(const ObjectType &ot)
{
- map<const TrackType *, TrackType3D *>::const_iterator i = tracks.find(&tt);
- if(i==tracks.end())
- throw KeyError("Unknown track type");
-
- return *i->second;
+ if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
+ {
+ objects[&ot] = new TrackType3D(*this, *tt);
+ const TrackAppearance &appearance = tt->get_appearance();
+ if(!endpoint_meshes.count(&appearance))
+ build_endpoint_mesh(appearance);
+ }
+ else if(const SignalType *st = dynamic_cast<const SignalType *>(&ot))
+ objects[&ot] = new SignalType3D(*this, *st);
+ else if(const VehicleType *vt = dynamic_cast<const VehicleType *>(&ot))
+ objects[&ot] = new VehicleType3D(*this, *vt);
}
-const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const
+const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
{
- map<const VehicleType *, VehicleType3D *>::const_iterator i = vehicles.find(&vt);
- if(i==vehicles.end())
- throw KeyError("Unknown vehicle type");
-
- return *i->second;
+ return *get_item(objects, &ot);
}
-void Catalogue3D::track_added(const TrackType &track)
+void Catalogue3D::build_endpoint_mesh(const TrackAppearance &appearance)
{
- tracks[&track] = new TrackType3D(*this, track);
-}
+ const Profile &ballast_profile = appearance.get_ballast_profile();
+ const Vector &ballast_min = ballast_profile.get_min_coords();
+ const Vector &ballast_max = ballast_profile.get_max_coords();
-void Catalogue3D::loco_added(const LocoType &loco)
-{
- vehicles[&loco] = new VehicleType3D(*this, loco);
-}
-
-void Catalogue3D::build_endpoint_mesh()
-{
- const Profile &ballast_profile = catalogue.get_ballast_profile();
- const Point &ballast_min = ballast_profile.get_min_coords();
- const Point &ballast_max = ballast_profile.get_max_coords();
-
- const Profile &rail_profile = catalogue.get_rail_profile();
- const Point &rail_min = rail_profile.get_min_coords();
- const Point &rail_max = rail_profile.get_max_coords();
+ const Profile &rail_profile = appearance.get_rail_profile();
+ const Vector &rail_min = rail_profile.get_min_coords();
+ const Vector &rail_max = rail_profile.get_max_coords();
float gauge = catalogue.get_gauge();
float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
- GL::MeshBuilder bld(endpoint_mesh);
+ GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
+ GL::MeshBuilder bld(*mesh);
bld.normal(1, 0, 0);
bld.begin(GL::QUADS);
bld.vertex(0, width/2, 0);
bld.vertex(0, -width/2, height);
bld.vertex(0, -width/2, 0);
bld.end();
+
+ endpoint_meshes[&appearance] = mesh;
+}
+
+const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
+{
+ return *get_item(endpoint_meshes, &appearance);
+}
+
+FS::Path Catalogue3D::locate_file(const string &name)
+{
+ if(FS::exists(name))
+ return name;
+
+ FS::Path path = FS::Path("data")/name;
+ if(FS::exists(path))
+ return path;
+
+ throw runtime_error("Can't locate "+name);
+}
+
+template<typename T>
+T *Catalogue3D::create(const string &name)
+{
+ RefPtr<T> obj = new T;
+ DataFile::load(*obj, locate_file(name).str());
+ return obj.release();
+}
+
+template<typename T>
+T *Catalogue3D::create2(const string &name)
+{
+ RefPtr<T> obj = new T;
+ DataFile::load(*obj, locate_file(name).str(), *this);
+ return obj.release();
}
-} // namespace Marklin
+} // namespace R2C2