]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
Improve graphics quality with some shaders and effects
[r2c2.git] / source / 3d / catalogue.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/gl/meshbuilder.h>
3 #include "libr2c2/trackappearance.h"
4 #include "catalogue.h"
5 #include "signaltype.h"
6 #include "tracktype.h"
7 #include "vehicletype.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 Catalogue3D::Catalogue3D(Catalogue &c):
15         catalogue(c)
16 {
17         const list<DataFile::CollectionSource *> &src = catalogue.get_sources();
18         for(list<DataFile::CollectionSource *>::const_iterator i=src.begin(); i!=src.end(); ++i)
19                 add_source(**i);
20         catalogue.signal_source_added.connect(sigc::mem_fun(static_cast<DataFile::Collection *>(this), &Catalogue3D::add_source));
21
22         set_srgb_conversion(true);
23 }
24
25 Catalogue3D::~Catalogue3D()
26 {
27         for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
28                 delete i->second;
29 }
30
31 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot)
32 {
33         ObjectMap::iterator i = objects.find(&ot);
34         if(i!=objects.end())
35                 return *i->second;
36
37         ObjectType3D *ot3d = 0;
38         if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
39                 ot3d = new TrackType3D(*this, *tt);
40         else if(const SignalType *st = dynamic_cast<const SignalType *>(&ot))
41                 ot3d = new SignalType3D(*this, *st);
42         else if(const VehicleType *vt = dynamic_cast<const VehicleType *>(&ot))
43                 ot3d = new VehicleType3D(*this, *vt);
44         else
45                 throw key_error(&ot);
46
47         objects[&ot] = ot3d;
48         return *ot3d;
49 }
50
51 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
52 {
53         return *get_item(objects, &ot);
54 }
55
56 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance)
57 {
58         EndpointMap::iterator i = endpoint_meshes.find(&appearance);
59         if(i!=endpoint_meshes.end())
60                 return *i->second;
61
62         const Profile &ballast_profile = appearance.get_ballast_profile();
63         const Vector &ballast_min = ballast_profile.get_min_coords();
64         const Vector &ballast_max = ballast_profile.get_max_coords();
65
66         const Profile &rail_profile = appearance.get_rail_profile();
67         const Vector &rail_min = rail_profile.get_min_coords();
68         const Vector &rail_max = rail_profile.get_max_coords();
69
70         float gauge = appearance.get_gauge();
71
72         float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
73         float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
74
75         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
76         GL::MeshBuilder bld(*mesh);
77         bld.normal(1, 0, 0);
78         bld.begin(GL::QUADS);
79         bld.vertex(0, width/2, 0);
80         bld.vertex(0, width/2, height);
81         bld.vertex(0, -width/2, height);
82         bld.vertex(0, -width/2, 0);
83         bld.end();
84
85         endpoint_meshes[&appearance] = mesh;
86         return *mesh;
87 }
88
89 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
90 {
91         return *get_item(endpoint_meshes, &appearance);
92 }
93
94 } // namespace R2C2