]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/track.cpp
Don't crash if a train has no router
[r2c2.git] / source / 3d / track.cpp
index 32857961d9ecc54134bb4f352c7136783f3098a6..549bc156ba6dce2de6babcb6db4889441ce4b221 100644 (file)
@@ -1,90 +1,73 @@
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
 #include <cmath>
 #include <msp/gl/matrix.h>
-#include <msp/gl/misc.h>
-#include "libmarklin/tracktype.h"
+#include <msp/gl/renderer.h>
+#include "libr2c2/tracktype.h"
 #include "endpoint.h"
 #include "layout.h"
+#include "path.h"
 #include "track.h"
 #include "tracktype.h"
 
 using namespace std;
 using namespace Msp;
 
-namespace Marklin {
+namespace R2C2 {
 
 Track3D::Track3D(Layout3D &l, Track &t):
-       layout(l),
+       Object3D(l, t),
+       GL::ObjectInstance(l.get_catalogue().get_3d(t.get_type()).get_object()),
        track(t),
-       type(layout.get_catalogue().get_track(track.get_type())),
-       color(1, 1, 1)
+       type(layout.get_catalogue().get_3d(track.get_type()))
 {
-       layout.add_track(*this);
        layout.get_scene().add(*this);
 
-       const vector<Endpoint> &type_eps = track.get_type().get_endpoints();
+       const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
+       const vector<Track *> &links = track.get_links();
        for(unsigned i=0; i<type_eps.size(); ++i)
-               endpoints.push_back(new Endpoint3D(*this, i));
+       {
+               if(!links[i] || links[i]>&track)
+                       endpoints.push_back(new Endpoint3D(*this, i));
+               else
+                       endpoints.push_back(0);
+       }
+
+       track.signal_link_changed.connect(sigc::mem_fun(this, &Track3D::link_changed));
 }
 
 Track3D::~Track3D()
 {
-       layout.remove_track(*this);
        layout.get_scene().remove(*this);
 
        for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
                delete *i;
 }
 
-void Track3D::set_color(const Msp::GL::Color &c)
+Vector Track3D::get_node() const
 {
-       color = c;
+       Geometry::BoundingBox<float, 3> bbox = track.get_type().get_shape()->get_axis_aligned_bounding_box();
+       const Vector &minp = bbox.get_minimum_point();
+       const Vector &maxp = bbox.get_maximum_point();
+
+       return matrix*((minp+maxp)/2.0f+Vector(0, 0, 0.02));
 }
 
-void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
+void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
 {
-       type.get_bounds(angle-track.get_rotation(), minp, maxp);
-
-       float c = cos(-angle);
-       float s = sin(-angle);
-
-       const Point &pos = track.get_position();
-       minp.x += c*pos.x-s*pos.y;
-       maxp.x += c*pos.x-s*pos.y;
-       minp.y += s*pos.x+c*pos.y;
-       maxp.y += s*pos.x+c*pos.y;
-       minp.z += pos.z;
-       maxp.z += pos.z;
-
-       float slope = track.get_slope();
-       if(slope>0)
-               maxp.z += slope;
-       else
-               minp.z += slope;
+       renderer.matrix_stack() *= matrix;
 }
 
-void Track3D::render(const GL::Tag &tag) const
+void Track3D::link_changed(unsigned i, Track *trk)
 {
-       GL::PushMatrix push_mat;
-
-       const Point &pos = track.get_position();
-       float rot = track.get_rotation();
-
-       glTranslatef(pos.x, pos.y, pos.z);
-       glRotatef(rot*180/M_PI, 0, 0, 1);
-       glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
-
-       glPushName(reinterpret_cast<unsigned>(this));
-
-       type.render(tag);
-
-       glPopName();
+       if(!trk || trk>&track)
+       {
+               if(!endpoints[i])
+                       endpoints[i] = new Endpoint3D(*this, i);
+       }
+       else if(endpoints[i])
+       {
+               delete endpoints[i];
+               endpoints[i] = 0;
+       }
 }
 
-} // namespace Marklin
+} // namespace R2C2