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