]> git.tdb.fi Git - r2c2.git/commitdiff
Use event-based matrix updates for objects
authorMikko Rasa <tdb@tdb.fi>
Thu, 4 Jul 2013 19:33:42 +0000 (22:33 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 4 Jul 2013 19:33:42 +0000 (22:33 +0300)
12 files changed:
source/3d/object.cpp [new file with mode: 0644]
source/3d/object.h
source/3d/path.cpp
source/3d/signal.cpp
source/3d/signal.h
source/3d/track.cpp
source/3d/track.h
source/3d/vehicle.cpp
source/libr2c2/object.h
source/libr2c2/signal.cpp
source/libr2c2/track.cpp
source/libr2c2/vehicle.cpp

diff --git a/source/3d/object.cpp b/source/3d/object.cpp
new file mode 100644 (file)
index 0000000..fcbf213
--- /dev/null
@@ -0,0 +1,20 @@
+#include "object.h"
+
+using namespace Msp;
+
+namespace R2C2 {
+
+Object3D::Object3D(Object &o):
+       object(o)
+{
+       object.signal_moved.connect(sigc::mem_fun(this, &Object3D::moved));
+}
+
+void Object3D::moved()
+{
+       matrix = GL::Matrix::translation(object.get_position());
+       matrix.rotate(object.get_rotation(), 0, 0, 1);
+       matrix.rotate(object.get_tilt(), 0, -1, 0);
+}
+
+} // namespace R2C2
index aa6698799b4ba97c616a8c1e94423105d5d489f2..5696627f7e8e59a9285178dbdb43d6c13b939eea 100644 (file)
@@ -1,19 +1,28 @@
 #ifndef R2C2_3D_OBJECT_H_
 #define R2C2_3D_OBJECT_H_
 
+#include <sigc++/trackable.h>
+#include <msp/gl/matrix.h>
 #include "libr2c2/geometry.h"
+#include "libr2c2/object.h"
 
 namespace R2C2 {
 
-class Object3D
+class Object3D: public sigc::trackable
 {
 protected:
-       Object3D() { }
+       Object &object;
+       Msp::GL::Matrix matrix;
+
+       Object3D(Object &);
 public:
        virtual ~Object3D() { }
 
+       const Msp::GL::Matrix &get_matrix() const { return matrix; }
        virtual Vector get_node() const = 0;
        virtual bool is_visible() const = 0;
+protected:
+       virtual void moved();
 };
 
 } // namespace R2C2
index fe4a2467b57df59c64e9d3aea9f4396af922eccb..816631b8307a8f289f436e41c0f7e71d17b1f577 100644 (file)
@@ -71,9 +71,8 @@ void Path3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
                        return;
 
                GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
-               GL::Matrix matrix = track.create_matrix();
-               matrix.translate(0, 0, z_offs);
-               renderer.matrix_stack() *= matrix;
+               renderer.matrix_stack() *= track.Object3D::get_matrix();
+               renderer.matrix_stack() *= GL::Matrix::translation(0, 0, z_offs);
 
                glColor4f(color.r, color.g, color.b, color.a);
                for(unsigned i=0; mask; ++i, mask>>=1)
index 4f124679eaed5d6ffa29338abd1d60a46061dd46..8e0fea841aee940b62687bf03c1749fadbc807d8 100644 (file)
@@ -9,6 +9,7 @@ using namespace Msp;
 namespace R2C2 {
 
 Signal3D::Signal3D(Layout3D &l, Signal &s):
+       Object3D(s),
        GL::ObjectInstance(l.get_catalogue().get_signal(s.get_type()).get_object()),
        layout(l),
        signal(s)
@@ -23,10 +24,14 @@ Signal3D::~Signal3D()
        layout.get_scene().remove(*this);
 }
 
+Vector Signal3D::get_node() const
+{
+       return matrix*Vector(0, -0.035, 0.13);
+}
+
 void Signal3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const
 {
-       renderer.matrix_stack() *= GL::Matrix::translation(signal.get_position());
-       renderer.matrix_stack() *= GL::Matrix::rotation(signal.get_rotation(), 0, 0, 1);
+       renderer.matrix_stack() *= matrix;
        // XXX Use track gauge, configure signal side
        renderer.matrix_stack() *= GL::Matrix::translation(0, -0.035, 0);
 }
index 5777590f0fcf71069e07ce820ab599505fb5e8ce..dc4c5107b5f63b61adb616a3888555b4a883c71c 100644 (file)
@@ -3,12 +3,13 @@
 
 #include <msp/gl/objectinstance.h>
 #include "libr2c2/signal.h"
+#include "object.h"
 
 namespace R2C2 {
 
 class Layout3D;
 
-class Signal3D: public Msp::GL::ObjectInstance
+class Signal3D: public Object3D, public Msp::GL::ObjectInstance
 {
 private:
        Layout3D &layout;
@@ -18,6 +19,9 @@ public:
        Signal3D(Layout3D &, Signal &);
        ~Signal3D();
 
+       virtual Vector get_node() const;
+       virtual bool is_visible() const { return true; }
+
        Signal &get_signal() const { return signal; }
        virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const;
 };
index 92c15744e42f23fba386287146fa9a81336206e5..210791feeaf6c77fd0bab3ed96172ef3ce741963 100644 (file)
@@ -14,6 +14,7 @@ using namespace Msp;
 namespace R2C2 {
 
 Track3D::Track3D(Layout3D &l, Track &t):
+       Object3D(t),
        GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
        layout(l),
        track(t),
@@ -53,22 +54,12 @@ Vector Track3D::get_node() const
        const Vector &minp = bbox.get_minimum_point();
        const Vector &maxp = bbox.get_maximum_point();
 
-       return track.get_position()+rotated_vector((minp+maxp)/2.0f, track.get_rotation())+Vector(0, 0, 0.02);
-}
-
-GL::Matrix Track3D::create_matrix() const
-{
-       GL::Matrix matrix;
-       matrix.translate(track.get_position());
-       matrix.rotate(track.get_rotation(), 0, 0, 1);
-       matrix.rotate(track.get_tilt(), 0, -1, 0);
-
-       return matrix;
+       return matrix*((minp+maxp)/2.0f+Vector(0, 0, 0.02));
 }
 
 void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
 {
-       renderer.matrix_stack() *= create_matrix();
+       renderer.matrix_stack() *= matrix;
 }
 
 void Track3D::link_changed(unsigned i, Track *trk)
index 857b129c11fedbb85adc7796b16b50768bdeb3d7..cc06cf1593f24073c42ccf779480cc3b262ce36c 100644 (file)
@@ -16,7 +16,7 @@ class Layout3D;
 class Path3D;
 class TrackType3D;
 
-class Track3D: public Object3D, public Msp::GL::ObjectInstance, public sigc::trackable
+class Track3D: public Object3D, public Msp::GL::ObjectInstance
 {
 private:
        Layout3D &layout;
@@ -37,7 +37,6 @@ public:
        virtual Vector get_node() const;
        virtual bool is_visible() const { return true; }
 
-       Msp::GL::Matrix create_matrix() const;
        virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const;
 private:
        void link_changed(unsigned, Track *);
index 106be97d3c5be4c5ed49997362d314a4f2a412db..e33f643c8ddc69d710a63ef3019d988a593bab0e 100644 (file)
@@ -14,6 +14,7 @@ using namespace Msp;
 namespace R2C2 {
 
 Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v):
+       Object3D(v),
        GL::ObjectInstance(*l.get_catalogue().get_vehicle(v.get_type()).get_body_object()),
        layout(l),
        vehicle(v),
@@ -101,13 +102,9 @@ void Vehicle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
 
 void Vehicle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const
 {
-       GL::Matrix matrix;
-       matrix.translate(vehicle.get_position());
-       Angle rot = vehicle.get_rotation();
-       if(vehicle.get_type().get_rotate_object())
-               rot += Angle::half_turn();
-       matrix.rotate(rot, 0, 0, 1);
        renderer.matrix_stack() *= matrix;
+       if(vehicle.get_type().get_rotate_object())
+               renderer.matrix_stack() *= GL::Matrix::rotation(Angle::half_turn(), 0, 0, 1);
 }
 
 } // namespace R2C2
index c346b6de0c9b010199d11dd95bd9b9921e215934..f8e895848f7ef17e17c59c1f535962a7a2dff12b 100644 (file)
@@ -11,6 +11,9 @@ class Layout;
 
 class Object
 {
+public:
+       sigc::signal<void> signal_moved;
+
 protected:
        Layout &layout;
        Vector position;
index 38e11248793258641a6ef3557c247bb8406f3b09..c4a83b9a3cee6719c4afe5f65c406b572edb47c3 100644 (file)
@@ -55,6 +55,7 @@ void Signal::set_position(const Vector &p)
        position = p;
 
        update_location();
+       signal_moved.emit();
 }
 
 void Signal::update_location()
@@ -103,6 +104,7 @@ void Signal::set_rotation(const Angle &r)
        rotation = r;
 
        update_location();
+       signal_moved.emit();
 }
 
 unsigned Signal::get_n_snap_nodes() const
index 697e67843080ec68fba6898e3ad3eae81d3d60d4..43bacc3d8bc10f6a4653716064cbffb331e2ed0c 100644 (file)
@@ -69,12 +69,14 @@ Block &Track::get_block() const
 void Track::set_position(const Vector &p)
 {
        position = p;
+       signal_moved.emit();
        propagate_slope();
 }
 
 void Track::set_rotation(const Angle &r)
 {
        rotation = wrap_positive(r);
+       signal_moved.emit();
 }
 
 void Track::set_tilt(const Angle &t)
@@ -84,6 +86,7 @@ void Track::set_tilt(const Angle &t)
 
        tilt = t;
        slope = tan(tilt)*type.get_path_length(0);
+       signal_moved.emit();
        propagate_slope();
 }
 
@@ -125,6 +128,8 @@ void Track::check_slope()
                        position.z = epp.z-slope;
                }
        }
+
+       signal_moved.emit();
 }
 
 void Track::set_turnout_id(unsigned i)
@@ -369,12 +374,12 @@ Track::Loader::Loader(Track &t):
 
 void Track::Loader::position(float x, float y, float z)
 {
-       obj.position = Vector(x, y, z);
+       obj.set_position(Vector(x, y, z));
 }
 
 void Track::Loader::rotation(float r)
 {
-       obj.rotation = Angle::from_radians(r);
+       obj.set_rotation(Angle::from_radians(r));
 }
 
 void Track::Loader::sensor_id(unsigned id)
@@ -384,16 +389,12 @@ void Track::Loader::sensor_id(unsigned id)
 
 void Track::Loader::slope(float s)
 {
-       tilt(atan(s/obj.type.get_path_length(0)));
+       obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0)));
 }
 
 void Track::Loader::tilt(float t)
 {
-       if(obj.links.size()!=2)
-               return;
-
-       obj.tilt = Angle::from_radians(t);
-       obj.slope = tan(t)*obj.type.get_path_length(0);
+       obj.set_tilt(Angle::from_radians(t));
 }
 
 void Track::Loader::turnout_id(unsigned id)
index a9d5135e9d7806955148e62b8558bd854339372a..a8c3b48dc2f925e9b00812799f4f4afd0c77c95d 100644 (file)
@@ -206,6 +206,7 @@ void Vehicle::update_position()
        position = tp.pos;
        position.z += layout.get_catalogue().get_rail_elevation();
        rotation = tp.dir;
+       signal_moved.emit();
 }
 
 void Vehicle::update_position_from(const Vehicle &veh)