From: Mikko Rasa Date: Thu, 25 Jul 2013 14:24:26 +0000 (+0300) Subject: Create a common base class for Vehicle3D parts X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b0d402f30489b0618d21281781e1b8f683ed567b;p=r2c2.git Create a common base class for Vehicle3D parts --- diff --git a/source/3d/axle.cpp b/source/3d/axle.cpp index e52303a..04c7668 100644 --- a/source/3d/axle.cpp +++ b/source/3d/axle.cpp @@ -1,5 +1,3 @@ -#include -#include #include "axle.h" #include "vehicle.h" #include "vehicletype.h" @@ -9,33 +7,20 @@ using namespace Msp; namespace R2C2 { Axle3D::Axle3D(const Vehicle3D &v, unsigned a): - GL::ObjectInstance(*v.get_type().get_fixed_axle_object(a)), - vehicle(v.get_vehicle()), + VehiclePart3D(v, *v.get_type().get_fixed_axle_object(a)), bogie(0), - axle(vehicle.get_fixed_axle(a)) + axle(vehicle.get_vehicle().get_fixed_axle(a)) { } Axle3D::Axle3D(const Vehicle3D &v, unsigned b, unsigned a): - GL::ObjectInstance(*v.get_type().get_bogie_axle_object(b, a)), - vehicle(v.get_vehicle()), - bogie(&vehicle.get_bogie(b)), + VehiclePart3D(v, *v.get_type().get_bogie_axle_object(b, a)), + bogie(&vehicle.get_vehicle().get_bogie(b)), axle(bogie->axles[a]) { } -void Axle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const +void Axle3D::update_matrix() { - if(!vehicle.get_track()) - return; - - ObjectInstance::render(renderer, tag); -} - -void Axle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const -{ - GL::Matrix matrix; - - matrix.translate(vehicle.get_position()); - matrix.rotate(vehicle.get_rotation(), 0, 0, 1); + matrix = vehicle.Object3D::get_matrix(); if(bogie) { @@ -45,8 +30,6 @@ void Axle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const matrix.translate(axle.type->position, 0, axle.type->wheel_dia/2); matrix.rotate(axle.angle, 0, 1, 0); - - renderer.matrix_stack() *= matrix; } } // namespace R2C2 diff --git a/source/3d/axle.h b/source/3d/axle.h index 2612528..86bb752 100644 --- a/source/3d/axle.h +++ b/source/3d/axle.h @@ -3,15 +3,13 @@ #include #include "libr2c2/vehicle.h" +#include "vehiclepart.h" namespace R2C2 { -class Vehicle3D; - -class Axle3D: public Msp::GL::ObjectInstance +class Axle3D: public VehiclePart3D { private: - const Vehicle &vehicle; const Vehicle::Bogie *bogie; const Vehicle::Axle &axle; @@ -19,8 +17,7 @@ public: Axle3D(const Vehicle3D &, unsigned); Axle3D(const Vehicle3D &, unsigned, unsigned); - virtual void render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; - virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; + virtual void update_matrix(); }; } // namespace R2C2 diff --git a/source/3d/bogie.cpp b/source/3d/bogie.cpp index e00e373..24fa85d 100644 --- a/source/3d/bogie.cpp +++ b/source/3d/bogie.cpp @@ -1,5 +1,3 @@ -#include -#include #include "bogie.h" #include "vehicle.h" #include "vehicletype.h" @@ -9,33 +7,19 @@ using namespace Msp; namespace R2C2 { Bogie3D::Bogie3D(const Vehicle3D &v, unsigned b): - GL::ObjectInstance(*v.get_type().get_bogie_object(b)), - vehicle(v.get_vehicle()), - bogie(vehicle.get_bogie(b)) + VehiclePart3D(v, *v.get_type().get_bogie_object(b)), + bogie(vehicle.get_vehicle().get_bogie(b)) { } -void Bogie3D::render(Msp::GL::Renderer &renderer, const GL::Tag &tag) const +void Bogie3D::update_matrix() { - if(!vehicle.get_track()) - return; - - ObjectInstance::render(renderer, tag); -} - -void Bogie3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const -{ - GL::Matrix matrix; - - matrix.translate(vehicle.get_position()); - matrix.rotate(vehicle.get_rotation(), 0, 0, 1); + matrix = vehicle.Object3D::get_matrix(); matrix.translate(bogie.type->position, 0, 0); Angle dir = bogie.direction; if(bogie.type->rotate_object) dir += Angle::half_turn(); matrix.rotate(dir, 0, 0, 1); - - renderer.matrix_stack() *= matrix; } } // namespace R2C2 diff --git a/source/3d/bogie.h b/source/3d/bogie.h index 0dac988..2b3ade3 100644 --- a/source/3d/bogie.h +++ b/source/3d/bogie.h @@ -3,22 +3,19 @@ #include #include "libr2c2/vehicle.h" +#include "vehiclepart.h" namespace R2C2 { -class Vehicle3D; - -class Bogie3D: public Msp::GL::ObjectInstance +class Bogie3D: public VehiclePart3D { private: - const Vehicle &vehicle; const Vehicle::Bogie &bogie; public: Bogie3D(const Vehicle3D &, unsigned); - virtual void render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; - virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; + virtual void update_matrix(); }; } // namespace R2C2 diff --git a/source/3d/rod.cpp b/source/3d/rod.cpp index 5ae4584..0e41f02 100644 --- a/source/3d/rod.cpp +++ b/source/3d/rod.cpp @@ -1,5 +1,3 @@ -#include -#include #include "rod.h" #include "vehicle.h" #include "vehicletype.h" @@ -9,31 +7,18 @@ using namespace Msp; namespace R2C2 { Rod3D::Rod3D(const Vehicle3D &v, unsigned r): - GL::ObjectInstance(*v.get_type().get_rod_object(r)), - vehicle(v.get_vehicle()), - rod(vehicle.get_rod(r)) + VehiclePart3D(v, *v.get_type().get_rod_object(r)), + rod(vehicle.get_vehicle().get_rod(r)) { } -void Rod3D::render(GL::Renderer &renderer, const GL::Tag &tag) const +void Rod3D::update_matrix() { - if(!vehicle.get_track()) - return; - - ObjectInstance::render(renderer, tag); -} - -void Rod3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const -{ - GL::Matrix matrix; - matrix.translate(vehicle.get_position()); - matrix.rotate(vehicle.get_rotation(), 0, 0, 1); + matrix = vehicle.Object3D::get_matrix(); matrix.translate(rod.position); if(rod.type->mirror_object) matrix.scale(1, -1, 1); matrix.rotate(rod.angle, 0, -1, 0); - - renderer.matrix_stack() *= matrix; } } // namespace R2C2 diff --git a/source/3d/rod.h b/source/3d/rod.h index 7d1403f..8521468 100644 --- a/source/3d/rod.h +++ b/source/3d/rod.h @@ -3,22 +3,19 @@ #include #include "libr2c2/vehicle.h" +#include "vehiclepart.h" namespace R2C2 { -class Vehicle3D; - -class Rod3D: public Msp::GL::ObjectInstance +class Rod3D: public VehiclePart3D { private: - const Vehicle &vehicle; const Vehicle::Rod &rod; public: Rod3D(const Vehicle3D &, unsigned); - virtual void render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; - virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; + virtual void update_matrix(); }; } // namespace R2C2 diff --git a/source/3d/vehicle.cpp b/source/3d/vehicle.cpp index e4bab87..b7cec44 100644 --- a/source/3d/vehicle.cpp +++ b/source/3d/vehicle.cpp @@ -22,56 +22,34 @@ Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v): unsigned n_axles = vehicle.get_type().get_fixed_axles().size(); for(unsigned i=0; i::const_iterator i=children.begin(); i!=children.end(); ++i) + layout.get_scene().add(**i); } Vehicle3D::~Vehicle3D() { layout.get_scene().remove(*this); - for(vector::iterator i=axles.begin(); i!=axles.end(); ++i) - { - layout.get_scene().remove(**i); - delete *i; - } - for(vector::iterator i=bogies.begin(); i!=bogies.end(); ++i) - { - layout.get_scene().remove(**i); - delete *i; - } - for(vector::iterator i=rods.begin(); i!=rods.end(); ++i) + for(vector::iterator i=children.begin(); i!=children.end(); ++i) { layout.get_scene().remove(**i); delete *i; @@ -89,6 +67,14 @@ bool Vehicle3D::is_visible() const return vehicle.get_track(); } +void Vehicle3D::moved() +{ + Object3D::moved(); + + for(vector::const_iterator i=children.begin(); i!=children.end(); ++i) + (*i)->update_matrix(); +} + void Vehicle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const { if(!vehicle.get_track()) diff --git a/source/3d/vehicle.h b/source/3d/vehicle.h index 4b14553..27b1615 100644 --- a/source/3d/vehicle.h +++ b/source/3d/vehicle.h @@ -1,15 +1,14 @@ #ifndef LIBR2C23D_VEHICLE_H_ #define LIBR2C23D_VEHICLE_H_ +#include #include #include "libr2c2/vehicle.h" #include "object.h" namespace R2C2 { -class Axle3D; -class Bogie3D; -class Rod3D; +class VehiclePart3D; class VehicleType3D; class Vehicle3D: public Object3D, public Msp::GL::ObjectInstance @@ -17,9 +16,7 @@ class Vehicle3D: public Object3D, public Msp::GL::ObjectInstance private: Vehicle &vehicle; const VehicleType3D &type; - std::vector axles; - std::vector bogies; - std::vector rods; + std::vector children; public: Vehicle3D(Layout3D &, Vehicle &); @@ -30,7 +27,10 @@ public: virtual Vector get_node() const; virtual bool is_visible() const; +private: + virtual void moved(); +public: virtual void render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; }; diff --git a/source/3d/vehiclepart.cpp b/source/3d/vehiclepart.cpp new file mode 100644 index 0000000..1676ed0 --- /dev/null +++ b/source/3d/vehiclepart.cpp @@ -0,0 +1,26 @@ +#include +#include "vehiclepart.h" + +using namespace Msp; + +namespace R2C2 { + +VehiclePart3D::VehiclePart3D(const Vehicle3D &v, const GL::Object &o): + GL::ObjectInstance(o), + vehicle(v) +{ } + +void VehiclePart3D::render(GL::Renderer &renderer, const GL::Tag &tag) const +{ + if(!vehicle.is_visible()) + return; + + ObjectInstance::render(renderer, tag); +} + +void VehiclePart3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const +{ + renderer.matrix_stack() *= matrix; +} + +} // namespace R2C2 diff --git a/source/3d/vehiclepart.h b/source/3d/vehiclepart.h new file mode 100644 index 0000000..14b37ad --- /dev/null +++ b/source/3d/vehiclepart.h @@ -0,0 +1,28 @@ +#ifndef R2C2_3D_VEHICLEPART_H_ +#define R2C2_3D_VEHICLEPART_H_ + +#include +#include "vehicle.h" + +namespace R2C2 { + +class Vehicle3D; + +class VehiclePart3D: public Msp::GL::ObjectInstance +{ +protected: + const Vehicle3D &vehicle; + Msp::GL::Matrix matrix; + + VehiclePart3D(const Vehicle3D &, const Msp::GL::Object &); + +public: + virtual void update_matrix() = 0; + + virtual void render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; + virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; +}; + +} // namespace R2C2 + +#endif