X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2F3d%2Ftrack.cpp;h=bd148a7c3f307cb65ede9c21d341bec7b979137a;hb=7a36d396eded897c421424905b2c938d770df341;hp=baf35195ff3dd454ebd693cfb53a8b1eb6ae4ed6;hpb=a277834b140eb5987419e2c335032c534a7184cd;p=r2c2.git diff --git a/source/3d/track.cpp b/source/3d/track.cpp index baf3519..bd148a7 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -1,14 +1,7 @@ -/* $Id$ - -This file is part of the MSP Märklin suite -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #include #include -#include -#include "libmarklin/tracktype.h" +#include +#include "libr2c2/tracktype.h" #include "endpoint.h" #include "layout.h" #include "path.h" @@ -18,9 +11,10 @@ Distributed under the GPL 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())), @@ -29,9 +23,17 @@ Track3D::Track3D(Layout3D &l, Track &t): layout.add_track(*this); layout.get_scene().add(*this); - const vector &type_eps = track.get_type().get_endpoints(); + const vector &type_eps = track.get_type().get_endpoints(); + const vector &links = track.get_links(); for(unsigned i=0; 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() @@ -45,20 +47,13 @@ Track3D::~Track3D() delete *i; } -void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const +void Track3D::get_bounds(const Angle &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(); - 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; + Vector pos = rotated_vector(track.get_position(), -angle); + minp += pos; + maxp += pos; float slope = track.get_slope(); if(slope>0) @@ -67,41 +62,42 @@ void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const minp.z += slope; } -Point Track3D::get_node() const +Vector Track3D::get_node() const { - 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); + Vector minp; + Vector maxp; + type.get_bounds(Angle::zero(), minp, maxp); + + return track.get_position()+rotated_vector((minp+maxp)/2.0f, track.get_rotation())+Vector(0, 0, 0.02); } -void Track3D::apply_matrix() const +GL::Matrix Track3D::create_matrix() const { - const Point &pos = track.get_position(); - float rot = track.get_rotation(); + GL::Matrix matrix; + matrix.translate(track.get_position()); + matrix.rotate(track.get_rotation(), 0, 0, 1); + matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0); - 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); + return matrix; } -void Track3D::render(const GL::Tag &tag) const +void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const { - GL::PushMatrix push_mat; - - apply_matrix(); - - glPushName(reinterpret_cast(this)); - - type.render(tag); + renderer.matrix_stack() *= create_matrix(); +} - glPopName(); +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 Marklin +} // namespace R2C2