]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
Use generic ObjectTypes in Catalogue
[r2c2.git] / source / 3d / catalogue.cpp
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>
6 #include "catalogue.h"
7 #include "signaltype.h"
8 #include "tracktype.h"
9 #include "vehicletype.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 Catalogue3D::Catalogue3D(Catalogue &c):
17         catalogue(c),
18         endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
19 {
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>);
25
26         catalogue.signal_object_added.connect(sigc::mem_fun(this, &Catalogue3D::object_added));
27
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);
31
32         build_endpoint_mesh();
33 }
34
35 Catalogue3D::~Catalogue3D()
36 {
37         for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
38                 delete i->second;
39 }
40
41 void Catalogue3D::object_added(const ObjectType &ot)
42 {
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);
49 }
50
51 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
52 {
53         return *get_item(objects, &ot);
54 }
55
56 void Catalogue3D::build_endpoint_mesh()
57 {
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();
61
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();
65
66         float gauge = catalogue.get_gauge();
67
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;
70
71         GL::MeshBuilder bld(endpoint_mesh);
72         bld.normal(1, 0, 0);
73         bld.begin(GL::QUADS);
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);
78         bld.end();
79 }
80
81 FS::Path Catalogue3D::locate_file(const string &name)
82 {
83         if(FS::exists(name))
84                 return name;
85
86         FS::Path path = FS::Path("data")/name;
87         if(FS::exists(path))
88                 return path;
89
90         throw runtime_error("Can't locate "+name);
91 }
92
93 template<typename T>
94 T *Catalogue3D::create(const string &name)
95 {
96         RefPtr<T> obj = new T;
97         DataFile::load(*obj, locate_file(name).str());
98         return obj.release();
99 }
100
101 template<typename T>
102 T *Catalogue3D::create2(const string &name)
103 {
104         RefPtr<T> obj = new T;
105         DataFile::load(*obj, locate_file(name).str(), *this);
106         return obj.release();
107 }
108
109 } // namespace R2C2