]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/vehicle.cpp
Make some internal colors darker to match linear color space
[r2c2.git] / source / 3d / vehicle.cpp
index 37619ebd339461f104dc0bf54aeb6fcfe71e7a67..5843c17998363f391025ecda5ba744726dd9fcaf 100644 (file)
@@ -1,76 +1,86 @@
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2010  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"
 
 using namespace std;
 using namespace Msp;
 
-namespace Marklin {
+namespace R2C2 {
 
 Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v):
-       layout(l),
+       Object3D(l, v),
+       GL::ObjectInstance(*l.get_catalogue().get_3d(v.get_type()).get_body_object()),
        vehicle(v),
-       type(layout.get_catalogue().get_vehicle(vehicle.get_type()))
+       type(layout.get_catalogue().get_3d(vehicle.get_type()))
 {
-       layout.add_vehicle(*this);
+       unsigned n_axles = vehicle.get_type().get_axles().size();
+       for(unsigned i=0; i<n_axles; ++i)
+               if(type.get_axle_object(i))
+                       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))
+                       children.push_back(new Bogie3D(*this, i));
+
+       unsigned n_rods = vehicle.get_type().get_rods().size();
+       for(unsigned i=0; i<n_rods; ++i)
+               if(type.get_rod_object(i))
+                       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.remove_vehicle(*this);
        layout.get_scene().remove(*this);
+       for(vector<VehiclePart3D *>::iterator i=children.begin(); i!=children.end(); ++i)
+       {
+               layout.get_scene().remove(**i);
+               delete *i;
+       }
 }
 
-Point Vehicle3D::get_node() const
+Vector Vehicle3D::get_node() const
 {
-       Point p = vehicle.get_position();
-       return Point(p.x, p.y, p.z+0.01+vehicle.get_type().get_height());
+       Vector p = vehicle.get_position();
+       return Vector(p.x, p.y, p.z+0.01+vehicle.get_type().get_height());
 }
 
 bool Vehicle3D::is_visible() const
 {
-       return vehicle.get_track();
+       return vehicle.is_placed();
 }
 
-void Vehicle3D::render(const GL::Tag &tag) const
+void Vehicle3D::moved()
 {
-       if(!vehicle.get_track())
-               return;
+       Object3D::moved();
 
-       if(tag==0)
-       {
-               GL::PushMatrix push_mat;
+       for(vector<VehiclePart3D *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+               (*i)->update_matrix();
+}
 
-               const Point &pos = vehicle.get_position();
-               GL::translate(pos.x, pos.y, pos.z+0.01);
-               GL::rotate(vehicle.get_direction()*180/M_PI, 0, 0, 1);
+void Vehicle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
+{
+       if(!is_visible())
+               return;
 
-               if(type.get_body_object())
-                       type.get_body_object()->render(tag);
+       ObjectInstance::render(renderer, tag);
+}
 
-               const vector<VehicleType::Bogie> &bogies = vehicle.get_type().get_bogies();
-               for(unsigned i=0; i<bogies.size(); ++i)
-               {
-                       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);
-               }
-       }
+void Vehicle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const
+{
+       renderer.matrix_stack() *= matrix;
+       if(vehicle.get_type().get_rotate_object())
+               renderer.matrix_stack() *= GL::Matrix::rotation(Angle::half_turn(), 0, 0, 1);
 }
 
-} // namespace Marklin
+} // namespace R2C2