]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
2f4fc04ded6ee6923322691354ebcba03c595334
[r2c2.git] / source / 3d / catalogue.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gl/meshbuilder.h>
9 #include "catalogue.h"
10 #include "tracktype.h"
11 #include "vehicletype.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace Marklin {
17
18 Catalogue3D::Catalogue3D(Catalogue &c):
19         catalogue(c),
20         endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
21 {
22         catalogue.signal_track_added.connect(sigc::mem_fun(this, &Catalogue3D::track_added));
23         catalogue.signal_vehicle_added.connect(sigc::mem_fun(this, &Catalogue3D::vehicle_added));
24
25         const Catalogue::TrackMap &trks = catalogue.get_tracks();
26         for(Catalogue::TrackMap::const_iterator i=trks.begin(); i!=trks.end(); ++i)
27                 track_added(*i->second);
28
29         ballast_material.set_diffuse(GL::Color(0.25, 0.25, 0.25));
30         rail_material.set_diffuse(GL::Color(0.85, 0.85, 0.85));
31
32         build_endpoint_mesh();
33 }
34
35 Catalogue3D::~Catalogue3D()
36 {
37         for(map<const TrackType *, TrackType3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
38                 delete i->second;
39         for(map<const VehicleType *, VehicleType3D *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
40                 delete i->second;
41 }
42
43 const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
44 {
45         map<const TrackType *, TrackType3D *>::const_iterator i = tracks.find(&tt);
46         if(i==tracks.end())
47                 throw KeyError("Unknown track type");
48
49         return *i->second;
50 }
51
52 const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const
53 {
54         map<const VehicleType *, VehicleType3D *>::const_iterator i = vehicles.find(&vt);
55         if(i==vehicles.end())
56                 throw KeyError("Unknown vehicle type");
57
58         return *i->second;
59 }
60
61 void Catalogue3D::track_added(const TrackType &track)
62 {
63         tracks[&track] = new TrackType3D(*this, track);
64 }
65
66 void Catalogue3D::vehicle_added(const VehicleType &veh)
67 {
68         vehicles[&veh] = new VehicleType3D(*this, veh);
69 }
70
71 void Catalogue3D::build_endpoint_mesh()
72 {
73         const Profile &ballast_profile = catalogue.get_ballast_profile();
74         const Point &ballast_min = ballast_profile.get_min_coords();
75         const Point &ballast_max = ballast_profile.get_max_coords();
76
77         const Profile &rail_profile = catalogue.get_rail_profile();
78         const Point &rail_min = rail_profile.get_min_coords();
79         const Point &rail_max = rail_profile.get_max_coords();
80
81         float gauge = catalogue.get_gauge();
82
83         float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
84         float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
85
86         GL::MeshBuilder bld(endpoint_mesh);
87         bld.normal(1, 0, 0);
88         bld.begin(GL::QUADS);
89         bld.vertex(0, width/2, 0);
90         bld.vertex(0, width/2, height);
91         bld.vertex(0, -width/2, height);
92         bld.vertex(0, -width/2, 0);
93         bld.end();
94 }
95
96 } // namespace Marklin