]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
Use GL::Renderables and a Pipeline for rendering
[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
12 using namespace std;
13 using namespace Msp;
14
15 namespace Marklin {
16
17 Catalogue3D::Catalogue3D(const Catalogue &c):
18         catalogue(c),
19         endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
20 {
21         const map<unsigned, TrackType *> &trks = catalogue.get_tracks();
22         for(map<unsigned, TrackType *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
23                 tracks[i->second] = new TrackType3D(*this, *i->second);
24
25         ballast_material.set_diffuse(GL::Color(0.25, 0.25, 0.25));
26         rail_material.set_diffuse(GL::Color(0.85, 0.85, 0.85));
27
28         build_endpoint_mesh();
29 }
30
31 const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
32 {
33         map<const TrackType *, TrackType3D *>::const_iterator i = tracks.find(&tt);
34         if(i==tracks.end())
35                 throw KeyError("Unknown track type");
36
37         return *i->second;
38 }
39
40 void Catalogue3D::build_endpoint_mesh()
41 {
42         const Profile &ballast_profile = catalogue.get_ballast_profile();
43         const Point &ballast_min = ballast_profile.get_min_coords();
44         const Point &ballast_max = ballast_profile.get_max_coords();
45
46         const Profile &rail_profile = catalogue.get_rail_profile();
47         const Point &rail_min = rail_profile.get_min_coords();
48         const Point &rail_max = rail_profile.get_max_coords();
49
50         float gauge = catalogue.get_gauge();
51
52         float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
53         float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
54
55         GL::MeshBuilder bld(endpoint_mesh);
56         bld.normal(1, 0, 0);
57         bld.begin(GL::QUADS);
58         bld.vertex(0, width/2, 0);
59         bld.vertex(0, width/2, height);
60         bld.vertex(0, -width/2, height);
61         bld.vertex(0, -width/2, 0);
62         bld.end();
63 }
64
65 } // namespace Marklin