]> git.tdb.fi Git - r2c2.git/commitdiff
Create a common base class for Vehicle3D parts
authorMikko Rasa <tdb@tdb.fi>
Thu, 25 Jul 2013 14:24:26 +0000 (17:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 25 Jul 2013 14:24:26 +0000 (17:24 +0300)
source/3d/axle.cpp
source/3d/axle.h
source/3d/bogie.cpp
source/3d/bogie.h
source/3d/rod.cpp
source/3d/rod.h
source/3d/vehicle.cpp
source/3d/vehicle.h
source/3d/vehiclepart.cpp [new file with mode: 0644]
source/3d/vehiclepart.h [new file with mode: 0644]

index e52303a62e3deafe71a2fa42cc74f963ae9353f2..04c7668d8a97da1dbbb459411ce32b7ad0e96d9f 100644 (file)
@@ -1,5 +1,3 @@
-#include <msp/gl/matrix.h>
-#include <msp/gl/renderer.h>
 #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
index 2612528a253abfcd0590fa734fe79324a68a8fe5..86bb752e47b9c71805a2e35b6291790d89178bf9 100644 (file)
@@ -3,15 +3,13 @@
 
 #include <msp/gl/objectinstance.h>
 #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
index e00e373b57f575c249b8af810d39eee635a16579..24fa85d3e799b1c888894ffe882a49137c378510 100644 (file)
@@ -1,5 +1,3 @@
-#include <msp/gl/matrix.h>
-#include <msp/gl/renderer.h>
 #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
index 0dac988cb0f00aff67b884618be7248990635722..2b3ade3c645f8113fd6c4538a06525caddcd969c 100644 (file)
@@ -3,22 +3,19 @@
 
 #include <msp/gl/objectinstance.h>
 #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
index 5ae458484bd171844a1f6480e264c2985410250d..0e41f02614d6b2ef1b3790724fb54fe18f0f980e 100644 (file)
@@ -1,5 +1,3 @@
-#include <msp/gl/matrix.h>
-#include <msp/gl/renderer.h>
 #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
index 7d1403fb542de421639cc4578ea93f8ce6125ab1..8521468377f67b16136844a6af5f2b888da7db1c 100644 (file)
@@ -3,22 +3,19 @@
 
 #include <msp/gl/objectinstance.h>
 #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
index e4bab870f856434b1e7e79f9b6c54543986921ee..b7cec4439cd8ce42163eaf255d9b799136cf7f3a 100644 (file)
@@ -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<n_axles; ++i)
                if(type.get_fixed_axle_object(i))
-               {
-                       Axle3D *a = new Axle3D(*this, i);
-                       axles.push_back(a);
-                       layout.get_scene().add(*a);
-               }
+                       children.push_back(new Axle3D(*this, i));
 
        unsigned n_bogies = vehicle.get_type().get_bogies().size();
        for(unsigned i=0; i<n_bogies; ++i)
                if(type.get_bogie_object(i))
                {
-                       Bogie3D *b = new Bogie3D(*this, i);
-                       bogies.push_back(b);
-                       layout.get_scene().add(*b);
+                       children.push_back(new Bogie3D(*this, i));
 
                        n_axles = vehicle.get_type().get_bogie(i).axles.size();
                        for(unsigned j=0; j<n_axles; ++j)
                                if(type.get_bogie_axle_object(i, j))
-                               {
-                                       Axle3D *a = new Axle3D(*this, i, j);
-                                       axles.push_back(a);
-                                       layout.get_scene().add(*a);
-                               }
+                                       children.push_back(new Axle3D(*this, i, j));
                }
 
        unsigned n_rods = vehicle.get_type().get_rods().size();
        for(unsigned i=0; i<n_rods; ++i)
                if(type.get_rod_object(i))
-               {
-                       Rod3D *r = new Rod3D(*this, i);
-                       rods.push_back(r);
-                       layout.get_scene().add(*r);
-               }
+                       children.push_back(new Rod3D(*this, i));
 
        layout.get_scene().add(*this);
+       for(vector<VehiclePart3D *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+               layout.get_scene().add(**i);
 }
 
 Vehicle3D::~Vehicle3D()
 {
        layout.get_scene().remove(*this);
-       for(vector<Axle3D *>::iterator i=axles.begin(); i!=axles.end(); ++i)
-       {
-               layout.get_scene().remove(**i);
-               delete *i;
-       }
-       for(vector<Bogie3D *>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
-       {
-               layout.get_scene().remove(**i);
-               delete *i;
-       }
-       for(vector<Rod3D *>::iterator i=rods.begin(); i!=rods.end(); ++i)
+       for(vector<VehiclePart3D *>::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<VehiclePart3D *>::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())
index 4b145536f724f566e4e41d9919f201659c1f2ff6..27b16155f83eff7ea2041131205223613a386c80 100644 (file)
@@ -1,15 +1,14 @@
 #ifndef LIBR2C23D_VEHICLE_H_
 #define LIBR2C23D_VEHICLE_H_
 
+#include <msp/gl/objectinstance.h>
 #include <msp/gl/renderable.h>
 #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<Axle3D *> axles;
-       std::vector<Bogie3D *> bogies;
-       std::vector<Rod3D *> rods;
+       std::vector<VehiclePart3D *> 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 (file)
index 0000000..1676ed0
--- /dev/null
@@ -0,0 +1,26 @@
+#include <msp/gl/renderer.h>
+#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 (file)
index 0000000..14b37ad
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef R2C2_3D_VEHICLEPART_H_
+#define R2C2_3D_VEHICLEPART_H_
+
+#include <msp/gl/objectinstance.h>
+#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