-#include <msp/gl/matrix.h>
-#include <msp/gl/renderer.h>
#include "axle.h"
#include "vehicle.h"
#include "vehicletype.h"
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)
{
matrix.translate(axle.type->position, 0, axle.type->wheel_dia/2);
matrix.rotate(axle.angle, 0, 1, 0);
-
- renderer.matrix_stack() *= matrix;
}
} // namespace R2C2
#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;
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
-#include <msp/gl/matrix.h>
-#include <msp/gl/renderer.h>
#include "bogie.h"
#include "vehicle.h"
#include "vehicletype.h"
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
#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
-#include <msp/gl/matrix.h>
-#include <msp/gl/renderer.h>
#include "rod.h"
#include "vehicle.h"
#include "vehicletype.h"
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
#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
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;
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())
#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
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 &);
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;
};
--- /dev/null
+#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
--- /dev/null
+#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