]> git.tdb.fi Git - r2c2.git/blob - source/3d/layout.cpp
Add editable terrain objects
[r2c2.git] / source / 3d / layout.cpp
1 #include "beamgate.h"
2 #include "layout.h"
3 #include "signal.h"
4 #include "terrain.h"
5 #include "track.h"
6 #include "utility.h"
7 #include "vehicle.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 Layout3D::Layout3D(Layout &l):
15         layout(l),
16         catalogue(layout.get_catalogue())
17 {
18         // South, 15° from zenith
19         sun.set_position(0, -0.259, 0.966, 0);
20         sun.set_diffuse(GL::Color(0.9));
21         lighting.set_ambient(GL::Color(0.4));
22         lighting.attach(0, sun);
23
24         layout.signal_object_added.connect(sigc::mem_fun(this, &Layout3D::object_added));
25         layout.signal_object_removed.connect(sigc::mem_fun(this, &Layout3D::object_removed));
26
27         const set<Object *> &lobjs = layout.get_all<Object>();
28         for(set<Object *>::iterator i=lobjs.begin(); i!=lobjs.end(); ++i)
29                 object_added(**i);
30 }
31
32 Layout3D::~Layout3D()
33 {
34         while(!utilities.empty())
35                 delete *utilities.begin();
36         while(!objects.empty())
37                 delete objects.begin()->second;
38 }
39
40 void Layout3D::get_bounds(Vector &minp, Vector &maxp) const
41 {
42         Geometry::BoundingBox<float, 3> bbox;
43
44         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
45         {
46                 const Shape *shape = i->second->get_object().get_shape();
47                 if(shape)
48                         bbox = bbox|shape->get_axis_aligned_bounding_box();
49         }
50
51         minp = bbox.get_minimum_point();
52         maxp = bbox.get_maximum_point();
53 }
54
55 void Layout3D::add(Object3D &o)
56 {
57         insert_unique(objects, &o.get_object(), &o);
58 }
59
60 Object3D &Layout3D::get_3d(Object &o) const
61 {
62         return *get_item(objects, &o);
63 }
64
65 void Layout3D::remove(Object3D &o)
66 {
67         objects.erase(&o.get_object());
68 }
69
70 void Layout3D::add(Utility3D &u)
71 {
72         utilities.insert(&u);
73 }
74
75 void Layout3D::remove(Utility3D &u)
76 {
77         utilities.erase(&u);
78 }
79
80 void Layout3D::object_added(Object &o)
81 {
82         if(Track *t = dynamic_cast<Track *>(&o))
83                 new Track3D(*this, *t);
84         else if(Signal *s = dynamic_cast<Signal *>(&o))
85                 new Signal3D(*this, *s);
86         else if(Vehicle *v = dynamic_cast<Vehicle *>(&o))
87                 new Vehicle3D(*this, *v);
88         else if(BeamGate *g = dynamic_cast<BeamGate *>(&o))
89                 new BeamGate3D(*this, *g);
90         else if(Terrain *r = dynamic_cast<Terrain *>(&o))
91                 new Terrain3D(*this, *r);
92 }
93
94 void Layout3D::object_removed(Object &o)
95 {
96         ObjectMap::iterator i = objects.find(&o);
97         if(i!=objects.end())
98                 delete i->second;
99 }
100
101 } // namespace R2C2