14 Layout3D::Layout3D(Layout &l):
16 catalogue(layout.get_catalogue())
18 // South, 15° from zenith
19 sun.set_position(GL::Vector4(0, -0.259, 0.966, 0));
20 sun.set_diffuse(GL::Color(0.7));
21 lighting.set_ambient(GL::Color(0.2));
22 lighting.attach(0, sun);
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));
27 const set<Object *> &lobjs = layout.get_all<Object>();
28 for(set<Object *>::iterator i=lobjs.begin(); i!=lobjs.end(); ++i)
34 while(!utilities.empty())
35 delete *utilities.begin();
36 while(!objects.empty())
37 delete objects.begin()->second;
40 void Layout3D::add(Object3D &o)
42 insert_unique(objects, &o.get_object(), &o);
45 Object3D &Layout3D::get_3d(Object &o) const
47 return *get_item(objects, &o);
50 void Layout3D::remove(Object3D &o)
52 objects.erase(&o.get_object());
55 void Layout3D::add(Utility3D &u)
60 void Layout3D::remove(Utility3D &u)
67 Time::TimeDelta t = layout.get_clock().get_current_time();
68 Angle time_of_day = Angle::from_turns(t/Time::day);
69 Angle latitude = Angle::from_degrees(52.5);
70 Angle axial_tilt = Angle::from_degrees(23.45);
71 Transform trans = Transform::rotation(Angle::quarter_turn()-latitude, Vector(-1, 0, 0))*
72 Transform::rotation(Angle::half_turn()-time_of_day, Vector(0, 0, 1))*
73 Transform::rotation(axial_tilt, Vector(-1, 0, 0));
74 Vector sun_dir = trans.transform_linear(Vector(0, -1, 0));
75 Vector diff = Vector(sun.get_position())-sun_dir;
76 if(diff.norm()>0.0025f)
77 sun.set_position(GL::Vector4(sun_dir, 0.0f));
78 // TODO replace these very unscientific guesses with correct formulas
79 lighting.set_ambient(GL::Color(pow(0.2f*(sun_dir.z+1.0f), 2.0f)));
80 sun.set_diffuse(GL::Color(0.7f*max(sun_dir.z, 0.0f)));
83 void Layout3D::object_added(Object &o)
85 if(Track *t = dynamic_cast<Track *>(&o))
86 new Track3D(*this, *t);
87 else if(Signal *s = dynamic_cast<Signal *>(&o))
88 new Signal3D(*this, *s);
89 else if(Vehicle *v = dynamic_cast<Vehicle *>(&o))
90 new Vehicle3D(*this, *v);
91 else if(BeamGate *g = dynamic_cast<BeamGate *>(&o))
92 new BeamGate3D(*this, *g);
93 else if(Terrain *r = dynamic_cast<Terrain *>(&o))
94 new Terrain3D(*this, *r);
97 void Layout3D::object_removed(Object &o)
99 ObjectMap::iterator i = objects.find(&o);