]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/track.cpp
Rename Point to Vector
[r2c2.git] / source / 3d / track.cpp
index 3241852f1ed32c606ad8c0dd07ea6a97e34684db..8b36b194220c0c3175da4c322e9b846e5112b656 100644 (file)
@@ -1,39 +1,53 @@
 /* $Id$
 
-This file is part of the MSP Märklin suite
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
+This file is part of R²C²
+Copyright © 2006-2011 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):
+       GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
        layout(l),
        track(t),
-       type(layout.get_catalogue().get_track(track.get_type()))
+       type(layout.get_catalogue().get_track(track.get_type())),
+       path(new Path3D(*this))
 {
        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()
 {
+       delete path;
+
        layout.remove_track(*this);
        layout.get_scene().remove(*this);
 
@@ -41,14 +55,14 @@ Track3D::~Track3D()
                delete *i;
 }
 
-void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
+void Track3D::get_bounds(float angle, Vector &minp, Vector &maxp) const
 {
        type.get_bounds(angle-track.get_rotation(), minp, maxp);
 
        float c = cos(-angle);
        float s = sin(-angle);
 
-       const Point &pos = track.get_position();
+       const Vector &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;
@@ -63,22 +77,56 @@ void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
                minp.z += slope;
 }
 
-void Track3D::render(const GL::Tag &tag) const
+Vector Track3D::get_node() const
 {
-       GL::PushMatrix push_mat;
+       const Vector &pos = track.get_position();
+       Vector minp;
+       Vector maxp;
+       type.get_bounds(0, minp, maxp);
+       float rot = track.get_rotation();
+       float c = cos(rot);
+       float s = sin(rot);
+
+       Vector center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
+       return Vector(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02);
+}
 
-       const Point &pos = track.get_position();
+GL::Matrix Track3D::get_matrix() const
+{
+       const Vector &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);
+       GL::Matrix matrix;
+       matrix.translate(pos.x, pos.y, pos.z);
+       matrix.rotate(rot, 0, 0, 1);
+       matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0);
 
-       glPushName(reinterpret_cast<unsigned>(this));
+       return matrix;
+}
 
-       type.render(tag);
+void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
+{
+       renderer.matrix_stack() *= get_matrix();
+       glPushName(reinterpret_cast<unsigned>(this));
+}
 
+void Track3D::finish_render(Msp::GL::Renderer &, const GL::Tag &) const
+{
        glPopName();
 }
 
-} // namespace Marklin
+void Track3D::link_changed(unsigned i, Track *trk)
+{
+       if(!trk || trk>&track)
+       {
+               if(!endpoints[i])
+                       endpoints[i] = new Endpoint3D(*this, i);
+       }
+       else if(endpoints[i])
+       {
+               delete endpoints[i];
+               endpoints[i] = 0;
+       }
+}
+
+} // namespace R2C2