]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/layout.cpp
Improve graphics quality with some shaders and effects
[r2c2.git] / source / 3d / layout.cpp
index db419d3639065c2e76a574702ebfe062085b6eae..29e6bb40daeb4a45dd686871bae173122eebf4dd 100644 (file)
@@ -1,6 +1,9 @@
+#include "beamgate.h"
 #include "layout.h"
 #include "signal.h"
+#include "terrain.h"
 #include "track.h"
+#include "utility.h"
 #include "vehicle.h"
 
 using namespace std;
@@ -13,7 +16,9 @@ Layout3D::Layout3D(Layout &l):
        catalogue(layout.get_catalogue())
 {
        // South, 15° from zenith
-       sun.set_position(0, -0.259, 0.966, 0);
+       sun.set_position(GL::Vector4(0, -0.259, 0.966, 0));
+       sun.set_diffuse(GL::Color(0.7));
+       lighting.set_ambient(GL::Color(0.2));
        lighting.attach(0, sun);
 
        layout.signal_object_added.connect(sigc::mem_fun(this, &Layout3D::object_added));
@@ -26,27 +31,18 @@ Layout3D::Layout3D(Layout &l):
 
 Layout3D::~Layout3D()
 {
+       while(!utilities.empty())
+               delete *utilities.begin();
        while(!objects.empty())
                delete objects.begin()->second;
 }
 
-void Layout3D::get_bounds(Vector &minp, Vector &maxp) const
-{
-       Geometry::BoundingBox<float, 3> bbox;
-
-       for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
-               bbox = bbox|i->second->get_object().get_type().get_shape()->get_axis_aligned_bounding_box();
-
-       minp = bbox.get_minimum_point();
-       maxp = bbox.get_maximum_point();
-}
-
 void Layout3D::add(Object3D &o)
 {
        insert_unique(objects, &o.get_object(), &o);
 }
 
-Object3D &Layout3D::get(Object &o) const
+Object3D &Layout3D::get_3d(Object &o) const
 {
        return *get_item(objects, &o);
 }
@@ -56,6 +52,34 @@ void Layout3D::remove(Object3D &o)
        objects.erase(&o.get_object());
 }
 
+void Layout3D::add(Utility3D &u)
+{
+       utilities.insert(&u);
+}
+
+void Layout3D::remove(Utility3D &u)
+{
+       utilities.erase(&u);
+}
+
+void Layout3D::tick()
+{
+       Time::TimeDelta t = layout.get_clock().get_current_time();
+       Angle time_of_day = Angle::from_turns(t/Time::day);
+       Angle latitude = Angle::from_degrees(52.5);
+       Angle axial_tilt = Angle::from_degrees(23.45);
+       Transform trans = Transform::rotation(Angle::quarter_turn()-latitude, Vector(-1, 0, 0))*
+               Transform::rotation(Angle::half_turn()-time_of_day, Vector(0, 0, 1))*
+               Transform::rotation(axial_tilt, Vector(-1, 0, 0));
+       Vector sun_dir = trans.transform_linear(Vector(0, -1, 0));
+       Vector diff = Vector(sun.get_position())-sun_dir;
+       if(diff.norm()>0.0025f)
+               sun.set_position(GL::Vector4(sun_dir, 0.0f));
+       // TODO replace these very unscientific guesses with correct formulas
+       lighting.set_ambient(GL::Color(pow(0.2f*(sun_dir.z+1.0f), 2.0f)));
+       sun.set_diffuse(GL::Color(0.7f*max(sun_dir.z, 0.0f)));
+}
+
 void Layout3D::object_added(Object &o)
 {
        if(Track *t = dynamic_cast<Track *>(&o))
@@ -64,6 +88,10 @@ void Layout3D::object_added(Object &o)
                new Signal3D(*this, *s);
        else if(Vehicle *v = dynamic_cast<Vehicle *>(&o))
                new Vehicle3D(*this, *v);
+       else if(BeamGate *g = dynamic_cast<BeamGate *>(&o))
+               new BeamGate3D(*this, *g);
+       else if(Terrain *r = dynamic_cast<Terrain *>(&o))
+               new Terrain3D(*this, *r);
 }
 
 void Layout3D::object_removed(Object &o)