]> git.tdb.fi Git - r2c2.git/commitdiff
Set correct Z coordinate to vehicle position
authorMikko Rasa <tdb@tdb.fi>
Sat, 9 Oct 2010 10:40:50 +0000 (10:40 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 9 Oct 2010 10:40:50 +0000 (10:40 +0000)
Render vehicle axles

source/3d/vehicle.cpp
source/3d/vehicletype.cpp
source/3d/vehicletype.h
source/libmarklin/catalogue.cpp
source/libmarklin/catalogue.h
source/libmarklin/profile.h
source/libmarklin/vehicle.cpp
source/libmarklin/vehicletype.cpp
source/libmarklin/vehicletype.h

index 37619ebd339461f104dc0bf54aeb6fcfe71e7a67..7e4814b008b03a64b588479792959329d7254cf9 100644 (file)
@@ -52,11 +52,20 @@ void Vehicle3D::render(const GL::Tag &tag) const
                GL::PushMatrix push_mat;
 
                const Point &pos = vehicle.get_position();
-               GL::translate(pos.x, pos.y, pos.z+0.01);
+               GL::translate(pos.x, pos.y, pos.z);
                GL::rotate(vehicle.get_direction()*180/M_PI, 0, 0, 1);
 
-               if(type.get_body_object())
-                       type.get_body_object()->render(tag);
+               if(const GL::Object *obj = type.get_body_object())
+                       obj->render(tag);
+
+               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))
+                       {
+                               GL::PushMatrix push_mat2;
+                               GL::translate(axles[i].position, 0, axles[i].wheel_dia/2);
+                               obj->render(tag);
+                       }
 
                const vector<VehicleType::Bogie> &bogies = vehicle.get_type().get_bogies();
                for(unsigned i=0; i<bogies.size(); ++i)
@@ -64,11 +73,20 @@ void Vehicle3D::render(const GL::Tag &tag) const
                        GL::PushMatrix push_mat2;
                        GL::translate(bogies[i].position, 0, 0);
                        float angle = vehicle.get_bogie_direction(i)*180/M_PI;
-                       if(bogies[i].rotate_object)
-                               angle += 180;
                        GL::rotate(angle, 0, 0, 1);
-                       if(type.get_bogie_object(i))
-                               type.get_bogie_object(i)->render(tag);
+
+                       for(unsigned j=0; j<bogies[i].axles.size(); ++j)
+                               if(const GL::Object *obj = type.get_bogie_axle_object(i, j))
+                               {
+                                       GL::PushMatrix push_mat3;
+                                       GL::translate(bogies[i].axles[j].position, 0, bogies[i].axles[j].wheel_dia/2);
+                                       obj->render(tag);
+                               }
+
+                       if(bogies[i].rotate_object)
+                               GL::rotate(180, 0, 0, 1);
+                       if(const GL::Object *obj = type.get_bogie_object(i))
+                               obj->render(tag);
                }
        }
 }
index 91c123866de22888bd67e8ed725bc92e6b080741..be6b985be828df81ed6bdf8a1288af638686dc81 100644 (file)
@@ -17,12 +17,22 @@ namespace Marklin {
 
 VehicleType3D::VehicleType3D(Catalogue3D &, const VehicleType &vt):
        body_object(0),
-       bogie_objects(vt.get_bogies().size())
+       axle_objects(1)
 {
        body_object = get_object(vt.get_object());
 
-       for(unsigned i=0; i<bogie_objects.size(); ++i)
-               bogie_objects[i] = get_object(vt.get_bogies()[i].object);
+       const vector<VehicleType::Axle> &axles = vt.get_axles();
+       for(vector<VehicleType::Axle>::const_iterator i=axles.begin(); i!=axles.end(); ++i)
+               axle_objects[0].push_back(get_object(i->object));
+
+       const vector<VehicleType::Bogie> &bogies = vt.get_bogies();
+       for(vector<VehicleType::Bogie>::const_iterator i=bogies.begin(); i!=bogies.end(); ++i)
+       {
+               bogie_objects.push_back(get_object(i->object));
+               axle_objects.push_back(vector<GL::Object *>());
+               for(vector<VehicleType::Axle>::const_iterator j=i->axles.begin(); j!=i->axles.end(); ++j)
+                       axle_objects.back().push_back(get_object(j->object));
+       }
 }
 
 VehicleType3D::~VehicleType3D()
@@ -31,9 +41,11 @@ VehicleType3D::~VehicleType3D()
                delete i->second;
 }
 
-const GL::Object *VehicleType3D::get_axle_object(unsigned) const
+const GL::Object *VehicleType3D::get_axle_object(unsigned i) const
 {
-       return 0;
+       if(i>=axle_objects[0].size())
+               throw InvalidParameterValue("Axle index out of range");
+       return axle_objects[0][i];
 }
 
 const GL::Object *VehicleType3D::get_bogie_object(unsigned i) const
@@ -43,9 +55,13 @@ const GL::Object *VehicleType3D::get_bogie_object(unsigned i) const
        return bogie_objects[i];
 }
 
-const GL::Object *VehicleType3D::get_bogie_axle_object(unsigned, unsigned) const
+const GL::Object *VehicleType3D::get_bogie_axle_object(unsigned i, unsigned j) const
 {
-       return 0;
+       if(i>=bogie_objects.size())
+               throw InvalidParameterValue("Bogie index out of range");
+       if(j>=axle_objects[i+1].size())
+               throw InvalidParameterValue("Axle index out of range");
+       return axle_objects[i+1][j];
 }
 
 GL::Object *VehicleType3D::get_object(const string &name)
index c5c491a7fe63c9973024b2aba908fd7c1670a306..faa34aca4e8e5a936273b5207399ba2246ef47cd 100644 (file)
@@ -22,6 +22,7 @@ private:
        std::map<std::string, Msp::GL::Object *> objects;
        Msp::GL::Object *body_object;
        std::vector<Msp::GL::Object *> bogie_objects;
+       std::vector<std::vector<Msp::GL::Object *> > axle_objects;
 
 public:
        VehicleType3D(Catalogue3D &, const VehicleType &);
index fca6045fc38da36c5613758fd205f95c1a5f791a..beab69a62ae387cf37b54745518472ac14ee0ef9 100644 (file)
@@ -30,6 +30,11 @@ Catalogue::~Catalogue()
                delete i->second;
 }
 
+float Catalogue::get_rail_elevation() const
+{
+       return ballast_profile.get_height()+rail_profile.get_height();
+}
+
 void Catalogue::add_track(TrackType &track)
 {
        if(tracks.count(track.get_article_number()))
index d529cfc07969e18f8c17983359aadc5cd543e45b..9a21c5ebadcc15c3e8f57b4257be9293af18891a 100644 (file)
@@ -54,6 +54,7 @@ public:
 
        float get_scale() const { return scale; }
        float get_gauge() const { return gauge; }
+       float get_rail_elevation() const;
        const Profile &get_rail_profile() const { return rail_profile; }
        const Profile &get_ballast_profile() const { return ballast_profile; }
        const Profile &get_path_profile() const { return path_profile; }
index 92b2dfc20918b6e2d8b24dd2c64f341225bb5136..b1082e8192e8be471810338988f287f679d9c1de 100644 (file)
@@ -36,6 +36,8 @@ public:
        const Point &get_point(unsigned) const;
        const Point &get_min_coords() const { return min_coords; }
        const Point &get_max_coords() const { return max_coords; }
+       float get_width() const { return max_coords.x-min_coords.x; }
+       float get_height() const { return max_coords.y-min_coords.y; }
        Point get_edge_normal(unsigned) const;
 };
 
index ac03de16a4ce7ed46ff87e5b04bdc40e8d79f6aa..75afa4bba78f9c10c03c4fb3254875d49770d9d1 100644 (file)
@@ -156,6 +156,7 @@ void Vehicle::update_position()
                check_sensor(type.get_back_axle_offset(), back_sensor);
 
        position = tp.pos;
+       position.z += layout.get_catalogue().get_rail_elevation();
        direction = tp.dir;
 }
 
index c206108331e95bf05a7ae6bee035c342180fd51e..639415f640317bc87ad4e2299deb36f7c3198a94 100644 (file)
@@ -119,6 +119,7 @@ void VehicleType::Loader::width(float w)
 VehicleType::Axle::Loader::Loader(Axle &a):
        DataFile::ObjectLoader<Axle>(a)
 {
+       add("object",         &Axle::object);
        add("position",       &Loader::position);
        add("powered",        &Axle::powered);
        add("wheel_diameter", &Loader::wheel_diameter);
index 082e3bc1e2167e3a66a0e10147b677b94028e210..9ef1c5ec9d22798620674d486588789cecb24e07 100644 (file)
@@ -42,6 +42,7 @@ public:
                float position;
                float wheel_dia;
                bool powered;
+               std::string object;
 
                Axle();
        };