]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/track.cpp
Rename the project to R²C²
[r2c2.git] / source / 3d / track.cpp
index 32857961d9ecc54134bb4f352c7136783f3098a6..401dd2d9634956a15d645047881663e1d355c44c 100644 (file)
@@ -1,6 +1,6 @@
 /* $Id$
 
-This file is part of the MSP Märklin suite
+This file is part of R²C²
 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
@@ -8,33 +8,36 @@ Distributed under the GPL
 #include <cmath>
 #include <msp/gl/matrix.h>
 #include <msp/gl/misc.h>
-#include "libmarklin/tracktype.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),
        track(t),
        type(layout.get_catalogue().get_track(track.get_type())),
-       color(1, 1, 1)
+       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();
        for(unsigned i=0; i<type_eps.size(); ++i)
                endpoints.push_back(new Endpoint3D(*this, i));
 }
 
 Track3D::~Track3D()
 {
+       delete path;
+
        layout.remove_track(*this);
        layout.get_scene().remove(*this);
 
@@ -42,11 +45,6 @@ Track3D::~Track3D()
                delete *i;
 }
 
-void Track3D::set_color(const Msp::GL::Color &c)
-{
-       color = c;
-}
-
 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
 {
        type.get_bounds(angle-track.get_rotation(), minp, maxp);
@@ -69,16 +67,35 @@ void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
                minp.z += slope;
 }
 
-void Track3D::render(const GL::Tag &tag) const
+Point Track3D::get_node() const
 {
-       GL::PushMatrix push_mat;
+       const Point &pos = track.get_position();
+       Point minp;
+       Point maxp;
+       type.get_bounds(0, minp, maxp);
+       float rot = track.get_rotation();
+       float c = cos(rot);
+       float s = sin(rot);
 
+       Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
+       return Point(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02);
+}
+
+void Track3D::apply_matrix() const
+{
        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);
+       GL::translate(pos.x, pos.y, pos.z);
+       GL::rotate(rot*180/M_PI, 0, 0, 1);
+       GL::rotate(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
+}
+
+void Track3D::render(const GL::Tag &tag) const
+{
+       GL::PushMatrix push_mat;
+
+       apply_matrix();
 
        glPushName(reinterpret_cast<unsigned>(this));
 
@@ -87,4 +104,4 @@ void Track3D::render(const GL::Tag &tag) const
        glPopName();
 }
 
-} // namespace Marklin
+} // namespace R2C2