]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
c02b7e6fddbc3ca64f860333d8fa06047187be51
[r2c2.git] / source / 3d / catalogue.cpp
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>
7 #include "catalogue.h"
8 #include "signaltype.h"
9 #include "tracktype.h"
10 #include "vehicletype.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace R2C2 {
16
17 Catalogue3D::Catalogue3D(Catalogue &c):
18         catalogue(c)
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::create2<GL::Object>);
23         add_type<GL::Program>().creator(&Catalogue3D::create<GL::Program>);
24         add_type<GL::Technique>().creator(&Catalogue3D::create2<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
33 Catalogue3D::~Catalogue3D()
34 {
35         for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
36                 delete i->second;
37 }
38
39 void Catalogue3D::object_added(const ObjectType &ot)
40 {
41         if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
42         {
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);
47         }
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);
52 }
53
54 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
55 {
56         return *get_item(objects, &ot);
57 }
58
59 void Catalogue3D::build_endpoint_mesh(const TrackAppearance &appearance)
60 {
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();
64
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();
68
69         float gauge = catalogue.get_gauge();
70
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;
73
74         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
75         GL::MeshBuilder bld(*mesh);
76         bld.normal(1, 0, 0);
77         bld.begin(GL::QUADS);
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);
82         bld.end();
83
84         endpoint_meshes[&appearance] = mesh;
85 }
86
87 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
88 {
89         return *get_item(endpoint_meshes, &appearance);
90 }
91
92 FS::Path Catalogue3D::locate_file(const string &name)
93 {
94         if(FS::exists(name))
95                 return name;
96
97         FS::Path path = FS::Path("data")/name;
98         if(FS::exists(path))
99                 return path;
100
101         throw runtime_error("Can't locate "+name);
102 }
103
104 template<typename T>
105 T *Catalogue3D::create(const string &name)
106 {
107         RefPtr<T> obj = new T;
108         DataFile::load(*obj, locate_file(name).str());
109         return obj.release();
110 }
111
112 template<typename T>
113 T *Catalogue3D::create2(const string &name)
114 {
115         RefPtr<T> obj = new T;
116         DataFile::load(*obj, locate_file(name).str(), *this);
117         return obj.release();
118 }
119
120 } // namespace R2C2