From: Mikko Rasa Date: Thu, 4 Jul 2013 19:33:42 +0000 (+0300) Subject: Use event-based matrix updates for objects X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f23c8d8cbc4e72b45e3a719b2cf974ce35d054e9;p=r2c2.git Use event-based matrix updates for objects --- diff --git a/source/3d/object.cpp b/source/3d/object.cpp new file mode 100644 index 0000000..fcbf213 --- /dev/null +++ b/source/3d/object.cpp @@ -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 diff --git a/source/3d/object.h b/source/3d/object.h index aa66987..5696627 100644 --- a/source/3d/object.h +++ b/source/3d/object.h @@ -1,19 +1,28 @@ #ifndef R2C2_3D_OBJECT_H_ #define R2C2_3D_OBJECT_H_ +#include +#include #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 diff --git a/source/3d/path.cpp b/source/3d/path.cpp index fe4a246..816631b 100644 --- a/source/3d/path.cpp +++ b/source/3d/path.cpp @@ -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) diff --git a/source/3d/signal.cpp b/source/3d/signal.cpp index 4f12467..8e0fea8 100644 --- a/source/3d/signal.cpp +++ b/source/3d/signal.cpp @@ -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); } diff --git a/source/3d/signal.h b/source/3d/signal.h index 5777590..dc4c510 100644 --- a/source/3d/signal.h +++ b/source/3d/signal.h @@ -3,12 +3,13 @@ #include #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; }; diff --git a/source/3d/track.cpp b/source/3d/track.cpp index 92c1574..210791f 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -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) diff --git a/source/3d/track.h b/source/3d/track.h index 857b129..cc06cf1 100644 --- a/source/3d/track.h +++ b/source/3d/track.h @@ -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 *); diff --git a/source/3d/vehicle.cpp b/source/3d/vehicle.cpp index 106be97..e33f643 100644 --- a/source/3d/vehicle.cpp +++ b/source/3d/vehicle.cpp @@ -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 diff --git a/source/libr2c2/object.h b/source/libr2c2/object.h index c346b6d..f8e8958 100644 --- a/source/libr2c2/object.h +++ b/source/libr2c2/object.h @@ -11,6 +11,9 @@ class Layout; class Object { +public: + sigc::signal signal_moved; + protected: Layout &layout; Vector position; diff --git a/source/libr2c2/signal.cpp b/source/libr2c2/signal.cpp index 38e1124..c4a83b9 100644 --- a/source/libr2c2/signal.cpp +++ b/source/libr2c2/signal.cpp @@ -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 diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 697e678..43bacc3 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -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) diff --git a/source/libr2c2/vehicle.cpp b/source/libr2c2/vehicle.cpp index a9d5135..a8c3b48 100644 --- a/source/libr2c2/vehicle.cpp +++ b/source/libr2c2/vehicle.cpp @@ -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)