]> git.tdb.fi Git - r2c2.git/commitdiff
Update Track3D to use GL::Renderer
authorMikko Rasa <tdb@tdb.fi>
Tue, 11 Jan 2011 22:04:50 +0000 (22:04 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 11 Jan 2011 22:04:50 +0000 (22:04 +0000)
Break Vehicle3D into multiple classes, derived from GL::ObjectInstance

source/3d/axle.cpp [new file with mode: 0644]
source/3d/axle.h [new file with mode: 0644]
source/3d/bogie.cpp [new file with mode: 0644]
source/3d/bogie.h [new file with mode: 0644]
source/3d/rod.cpp [new file with mode: 0644]
source/3d/rod.h [new file with mode: 0644]
source/3d/track.cpp
source/3d/track.h
source/3d/vehicle.cpp
source/3d/vehicle.h
source/libr2c2/vehicle.cpp

diff --git a/source/3d/axle.cpp b/source/3d/axle.cpp
new file mode 100644 (file)
index 0000000..5123106
--- /dev/null
@@ -0,0 +1,60 @@
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2011  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/gl/matrix.h>
+#include <msp/gl/renderer.h>
+#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 (file)
index 0000000..55db1bb
--- /dev/null
@@ -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 <msp/gl/objectinstance.h>
+#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 (file)
index 0000000..de7d1bb
--- /dev/null
@@ -0,0 +1,46 @@
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2011  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/gl/matrix.h>
+#include <msp/gl/renderer.h>
+#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 (file)
index 0000000..a9b9d54
--- /dev/null
@@ -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 <msp/gl/objectinstance.h>
+#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 (file)
index 0000000..26d000d
--- /dev/null
@@ -0,0 +1,47 @@
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2011  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/gl/matrix.h>
+#include <msp/gl/renderer.h>
+#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 (file)
index 0000000..6a32510
--- /dev/null
@@ -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 <msp/gl/objectinstance.h>
+#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
index c8bf0e6309211c1308bd6a56bbba803934e37ac5..f59e61c236926dab6669849b061192ef556488f5 100644 (file)
@@ -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 <cmath>
 #include <msp/gl/matrix.h>
 #include <msp/gl/misc.h>
+#include <msp/gl/renderer.h>
 #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<unsigned>(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
index 757955af21ff111f7fdfe3094761b3858f66dcaa..e1dd29196e3146b36462c7a9bd271d135bb0572f 100644 (file)
@@ -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
index 8027a1999d3827ab14dad36ced4816703f070a34..ceded8bc04c8012af255f399763ada36074270cb 100644 (file)
@@ -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 <cmath>
 #include <msp/gl/matrix.h>
+#include <msp/gl/renderer.h>
+#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<n_axles; ++i)
+               if(type.get_axle_object(i))
+               {
+                       Axle3D *a = new Axle3D(*this, i);
+                       axles.push_back(a);
+                       layout.get_scene().add(*a);
+               }
+
+       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);
+
+                       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);
+                               }
+               }
+
+       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);
+               }
+
        layout.add_vehicle(*this);
        layout.get_scene().add(*this);
 }
@@ -29,6 +70,12 @@ Vehicle3D::~Vehicle3D()
 {
        layout.remove_vehicle(*this);
        layout.get_scene().remove(*this);
+       for(vector<Axle3D *>::iterator i=axles.begin(); i!=axles.end(); ++i)
+               delete *i;
+       for(vector<Bogie3D *>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
+               delete *i;
+       for(vector<Rod3D *>::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<VehicleType::Axle> &axles = vehicle.get_type().get_axles();
+               /*const vector<VehicleType::Axle> &axles = vehicle.get_type().get_axles();
                for(unsigned i=0; i<axles.size(); ++i)
                        if(const GL::Object *obj = type.get_axle_object(i))
                        {
@@ -89,9 +138,9 @@ void Vehicle3D::render(const GL::Tag &tag) const
                                GL::rotate(180, 0, 0, 1);
                        if(const GL::Object *obj = type.get_bogie_object(i))
                                obj->render(tag);
-               }
+               }*/
 
-               const vector<VehicleType::Rod> &rods = vehicle.get_type().get_rods();
+               /*const vector<VehicleType::Rod> &rods = vehicle.get_type().get_rods();
                for(unsigned i=0; i<rods.size(); ++i)
                        if(const GL::Object *obj = type.get_rod_object(i))
                        {
@@ -103,8 +152,17 @@ void Vehicle3D::render(const GL::Tag &tag) const
                                        GL::scale(1, -1, 1);
                                GL::rotate(angle*180/M_PI, 0, -1, 0);
                                obj->render(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
index 06e78ef4409130094fc922d9e0ff79d1b93352d2..84b10beb4c6bb1da3bbb514cf30c9a8cf6d59698 100644 (file)
@@ -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<Axle3D *> axles;
+       std::vector<Bogie3D *> bogies;
+       std::vector<Rod3D *> 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
index d52417a5c8ba0e99606b7ee754ca43cd5a2b2515..3fdc7a44c7a2722b7afa763dbf87aea182062a8f 100644 (file)
@@ -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()