From 4b7008978ce5b67e7e1db14b4a359e4ab1881bbd Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 11 Jan 2011 22:04:50 +0000 Subject: [PATCH] Update Track3D to use GL::Renderer Break Vehicle3D into multiple classes, derived from GL::ObjectInstance --- source/3d/axle.cpp | 60 ++++++++++++++++++++++++++++++ source/3d/axle.h | 35 ++++++++++++++++++ source/3d/bogie.cpp | 46 +++++++++++++++++++++++ source/3d/bogie.h | 33 +++++++++++++++++ source/3d/rod.cpp | 47 +++++++++++++++++++++++ source/3d/rod.h | 33 +++++++++++++++++ source/3d/track.cpp | 11 +++--- source/3d/track.h | 6 +-- source/3d/vehicle.cpp | 76 +++++++++++++++++++++++++++++++++----- source/3d/vehicle.h | 14 +++++-- source/libr2c2/vehicle.cpp | 6 +-- 11 files changed, 343 insertions(+), 24 deletions(-) create mode 100644 source/3d/axle.cpp create mode 100644 source/3d/axle.h create mode 100644 source/3d/bogie.cpp create mode 100644 source/3d/bogie.h create mode 100644 source/3d/rod.cpp create mode 100644 source/3d/rod.h diff --git a/source/3d/axle.cpp b/source/3d/axle.cpp new file mode 100644 index 0000000..5123106 --- /dev/null +++ b/source/3d/axle.cpp @@ -0,0 +1,60 @@ +/* $Id$ + +This file is part of R²C² +Copyright © 2011 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include +#include +#include "axle.h" +#include "vehicle.h" +#include "vehicletype.h" + +using namespace Msp; + +namespace R2C2 { + +Axle3D::Axle3D(const Vehicle3D &v, unsigned a): + GL::ObjectInstance(*v.get_type().get_axle_object(a)), + vehicle(v.get_vehicle()), + bogie(0), + axle(vehicle.get_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)), + axle(bogie->axles[a]) +{ } + +void Axle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const +{ + if(!vehicle.get_track()) + return; + + ObjectInstance::render(renderer, tag); +} + +void Axle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const +{ + GL::Matrix matrix; + + const Point &pos = vehicle.get_position(); + matrix.translate(pos.x, pos.y, pos.z); + matrix.rotate(vehicle.get_direction(), 0, 0, 1); + + if(bogie) + { + matrix.translate(bogie->type->position, 0, 0); + matrix.rotate(bogie->direction, 0, 0, 1); + } + + 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 new file mode 100644 index 0000000..55db1bb --- /dev/null +++ b/source/3d/axle.h @@ -0,0 +1,35 @@ +/* $Id$ + +This file is part of R²C² +Copyright © 2011 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#ifndef LIBR2C23D_AXLE_H_ +#define LIBR2C23D_AXLE_H_ + +#include +#include "libr2c2/vehicle.h" + +namespace R2C2 { + +class Vehicle3D; + +class Axle3D: public Msp::GL::ObjectInstance +{ +private: + const Vehicle &vehicle; + const Vehicle::Bogie *bogie; + const Vehicle::Axle &axle; + +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; +}; + +} // namespace R2C2 + +#endif diff --git a/source/3d/bogie.cpp b/source/3d/bogie.cpp new file mode 100644 index 0000000..de7d1bb --- /dev/null +++ b/source/3d/bogie.cpp @@ -0,0 +1,46 @@ +/* $Id$ + +This file is part of R²C² +Copyright © 2011 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include +#include +#include "bogie.h" +#include "vehicle.h" +#include "vehicletype.h" + +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)) +{ } + +void Bogie3D::render(Msp::GL::Renderer &renderer, const GL::Tag &tag) const +{ + if(!vehicle.get_track()) + return; + + ObjectInstance::render(renderer, tag); +} + +void Bogie3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const +{ + GL::Matrix matrix; + + const Point &pos = vehicle.get_position(); + matrix.translate(pos.x, pos.y, pos.z); + matrix.rotate(vehicle.get_direction(), 0, 0, 1); + + matrix.translate(bogie.type->position, 0, 0); + matrix.rotate(bogie.direction, 0, 0, 1); + + renderer.matrix_stack() *= matrix; +} + +} // namespace R2C2 diff --git a/source/3d/bogie.h b/source/3d/bogie.h new file mode 100644 index 0000000..a9b9d54 --- /dev/null +++ b/source/3d/bogie.h @@ -0,0 +1,33 @@ +/* $Id$ + +This file is part of R²C² +Copyright © 2011 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#ifndef LIBR2C23D_BOGIE_H_ +#define LIBR2C23D_BOGIE_H_ + +#include +#include "libr2c2/vehicle.h" + +namespace R2C2 { + +class Vehicle3D; + +class Bogie3D: public Msp::GL::ObjectInstance +{ +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; +}; + +} // namespace R2C2 + +#endif diff --git a/source/3d/rod.cpp b/source/3d/rod.cpp new file mode 100644 index 0000000..26d000d --- /dev/null +++ b/source/3d/rod.cpp @@ -0,0 +1,47 @@ +/* $Id$ + +This file is part of R²C² +Copyright © 2011 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include +#include +#include "rod.h" +#include "vehicle.h" +#include "vehicletype.h" + +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)) +{ } + +void Rod3D::render(GL::Renderer &renderer, const GL::Tag &tag) const +{ + if(!vehicle.get_track()) + return; + + ObjectInstance::render(renderer, tag); +} + +void Rod3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const +{ + GL::Matrix matrix; + const Point &pos = vehicle.get_position(); + matrix.translate(pos.x, pos.y, pos.z); + matrix.rotate(vehicle.get_direction(), 0, 0, 1); + + matrix.translate(rod.position.x, rod.position.y, rod.position.z); + 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 new file mode 100644 index 0000000..6a32510 --- /dev/null +++ b/source/3d/rod.h @@ -0,0 +1,33 @@ +/* $Id$ + +This file is part of R²C² +Copyright © 2011 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#ifndef LIBR2C23D_ROD_H_ +#define LIBR2C23D_ROD_H_ + +#include +#include "libr2c2/vehicle.h" + +namespace R2C2 { + +class Vehicle3D; + +class Rod3D: public Msp::GL::ObjectInstance +{ +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; +}; + +} // namespace R2C2 + +#endif diff --git a/source/3d/track.cpp b/source/3d/track.cpp index c8bf0e6..f59e61c 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -1,13 +1,14 @@ /* $Id$ This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include #include #include +#include #include "libr2c2/tracktype.h" #include "endpoint.h" #include "layout.h" @@ -95,17 +96,15 @@ GL::Matrix Track3D::get_matrix() const return matrix; } -void Track3D::setup_render(const GL::Tag &) const +void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const { - GL::MatrixStack::modelview().push(); - GL::MatrixStack::modelview() *= get_matrix(); + renderer.matrix_stack() *= get_matrix(); glPushName(reinterpret_cast(this)); } -void Track3D::finish_render(const GL::Tag &) const +void Track3D::finish_render(Msp::GL::Renderer &, const GL::Tag &) const { glPopName(); - GL::MatrixStack::modelview().pop(); } } // namespace R2C2 diff --git a/source/3d/track.h b/source/3d/track.h index 757955a..e1dd291 100644 --- a/source/3d/track.h +++ b/source/3d/track.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -45,8 +45,8 @@ public: virtual bool is_visible() const { return true; } Msp::GL::Matrix get_matrix() const; - virtual void setup_render(const Msp::GL::Tag &) const; - virtual void finish_render(const Msp::GL::Tag &) const; + virtual void setup_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; + virtual void finish_render(Msp::GL::Renderer &, const Msp::GL::Tag &) const; }; } // namespace R2C2 diff --git a/source/3d/vehicle.cpp b/source/3d/vehicle.cpp index 8027a19..ceded8b 100644 --- a/source/3d/vehicle.cpp +++ b/source/3d/vehicle.cpp @@ -1,13 +1,17 @@ /* $Id$ This file is part of R²C² -Copyright © 2010 Mikkosoft Productions, Mikko Rasa +Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include #include +#include +#include "axle.h" +#include "bogie.h" #include "layout.h" +#include "rod.h" #include "vehicle.h" #include "vehicletype.h" @@ -17,10 +21,47 @@ using namespace Msp; namespace R2C2 { Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v): + GL::ObjectInstance(*l.get_catalogue().get_vehicle(v.get_type()).get_body_object()), layout(l), vehicle(v), type(layout.get_catalogue().get_vehicle(vehicle.get_type())) { + unsigned n_axles = vehicle.get_type().get_axles().size(); + for(unsigned i=0; i::iterator i=axles.begin(); i!=axles.end(); ++i) + delete *i; + for(vector::iterator i=bogies.begin(); i!=bogies.end(); ++i) + delete *i; + for(vector::iterator i=rods.begin(); i!=rods.end(); ++i) + delete *i; } Point Vehicle3D::get_node() const @@ -42,12 +89,14 @@ bool Vehicle3D::is_visible() const return vehicle.get_track(); } -void Vehicle3D::render(const GL::Tag &tag) const +void Vehicle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const { if(!vehicle.get_track()) return; - if(tag==0) + ObjectInstance::render(renderer, tag); + + /*if(tag==0) { GL::PushMatrix push_mat; @@ -56,9 +105,9 @@ void Vehicle3D::render(const GL::Tag &tag) const GL::rotate(vehicle.get_direction()*180/M_PI, 0, 0, 1); if(const GL::Object *obj = type.get_body_object()) - obj->render(tag); + obj->render(tag);*/ - const vector &axles = vehicle.get_type().get_axles(); + /*const vector &axles = vehicle.get_type().get_axles(); for(unsigned i=0; irender(tag); - } + }*/ - const vector &rods = vehicle.get_type().get_rods(); + /*const vector &rods = vehicle.get_type().get_rods(); for(unsigned i=0; irender(tag); - } - } + }*/ + //} +} + +void Vehicle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const +{ + GL::Matrix matrix; + const Point &pos = vehicle.get_position(); + matrix.translate(pos.x, pos.y, pos.z); + matrix.rotate(vehicle.get_direction(), 0, 0, 1); + renderer.matrix_stack() *= matrix; } } // namespace R2C2 diff --git a/source/3d/vehicle.h b/source/3d/vehicle.h index 06e78ef..84b10be 100644 --- a/source/3d/vehicle.h +++ b/source/3d/vehicle.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of R²C² -Copyright © 2010 Mikkosoft Productions, Mikko Rasa +Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -14,26 +14,34 @@ Distributed under the GPL namespace R2C2 { +class Axle3D; +class Bogie3D; class Layout3D; +class Rod3D; class VehicleType3D; -class Vehicle3D: public Object3D, public Msp::GL::Renderable +class Vehicle3D: public Object3D, public Msp::GL::ObjectInstance { private: Layout3D &layout; Vehicle &vehicle; const VehicleType3D &type; + std::vector axles; + std::vector bogies; + std::vector rods; public: Vehicle3D(Layout3D &, Vehicle &); ~Vehicle3D(); Vehicle &get_vehicle() const { return vehicle; } + const VehicleType3D &get_type() const { return type; } virtual Point get_node() const; virtual bool is_visible() const; - virtual void render(const Msp::GL::Tag &) const; + 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 diff --git a/source/libr2c2/vehicle.cpp b/source/libr2c2/vehicle.cpp index d52417a..3fdc7a4 100644 --- a/source/libr2c2/vehicle.cpp +++ b/source/libr2c2/vehicle.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of R²C² -Copyright © 2010 Mikkosoft Productions, Mikko Rasa +Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -29,11 +29,11 @@ Vehicle::Vehicle(Layout &l, const VehicleType &t): front_sensor(0), back_sensor(0) { - layout.add_vehicle(*this); - axles.assign(type.get_axles().begin(), type.get_axles().end()); bogies.assign(type.get_bogies().begin(), type.get_bogies().end()); rods.assign(type.get_rods().begin(), type.get_rods().end()); + + layout.add_vehicle(*this); } Vehicle::~Vehicle() -- 2.45.2