]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
Use GL::Resources and DirectorySource in Catalogue3D
[r2c2.git] / source / 3d / catalogue.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/gl/meshbuilder.h>
3 #include "catalogue.h"
4 #include "signaltype.h"
5 #include "tracktype.h"
6 #include "vehicletype.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 namespace R2C2 {
12
13 Catalogue3D::Catalogue3D(Catalogue &c):
14         catalogue(c)
15 {
16         src.add_directory(".");
17         src.add_directory("data");
18         add_source(src);
19
20         catalogue.signal_object_added.connect(sigc::mem_fun(this, &Catalogue3D::object_added));
21
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);
25 }
26
27 Catalogue3D::~Catalogue3D()
28 {
29         for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
30                 delete i->second;
31 }
32
33 void Catalogue3D::object_added(const ObjectType &ot)
34 {
35         if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
36         {
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);
41         }
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);
46 }
47
48 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
49 {
50         return *get_item(objects, &ot);
51 }
52
53 void Catalogue3D::build_endpoint_mesh(const TrackAppearance &appearance)
54 {
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();
58
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();
62
63         float gauge = catalogue.get_gauge();
64
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;
67
68         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
69         GL::MeshBuilder bld(*mesh);
70         bld.normal(1, 0, 0);
71         bld.begin(GL::QUADS);
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);
76         bld.end();
77
78         endpoint_meshes[&appearance] = mesh;
79 }
80
81 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
82 {
83         return *get_item(endpoint_meshes, &appearance);
84 }
85
86 } // namespace R2C2