X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2F3d%2Ftrack.cpp;h=a5c5dbf62a166904a5e3203d28b53af8a637284c;hb=bc8ac89bbe774bb133b758416182aa18e5e0a5a5;hp=d4b7079099406d016cee98e5406e0fb68ec17e48;hpb=de09cba1d12af38ff2e7e4312d67624fe63bf0e4;p=r2c2.git diff --git a/source/3d/track.cpp b/source/3d/track.cpp index d4b7079..a5c5dbf 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -1,261 +1,98 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include -#include +#include #include #include "libmarklin/tracktype.h" +#include "endpoint.h" +#include "layout.h" #include "track.h" +#include "tracktype.h" using namespace std; using namespace Msp; -#include - namespace Marklin { -Track3D::Track3D(Track &t, unsigned q): +Track3D::Track3D(Layout3D &l, Track &t): + layout(l), track(t), - color(1, 1, 1), - varray((GL::NORMAL3, GL::VERTEX3)), - quality(q) + type(layout.get_catalogue().get_track(track.get_type())) { - build_object(); -} + layout.add_track(*this); + layout.get_scene().add(*this); -void Track3D::set_quality(unsigned q) -{ - quality=q; - build_object(); + const vector &type_eps = track.get_type().get_endpoints(); + for(unsigned i=0; i::const_iterator i=border.begin(); i!=border.end(); ++i) - { - float x=c*pos.x-s*pos.y + c2*i->x-s2*i->y; - float y=s*pos.x+c*pos.y + s2*i->x+c2*i->y; - - minp.x=min(minp.x, x); - minp.y=min(minp.y, y); - maxp.x=max(maxp.x, x); - maxp.y=max(maxp.y, y); - } + for(vector::iterator i=endpoints.begin(); i!=endpoints.end(); ++i) + delete *i; } -void Track3D::render() const +void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const { - prepare_render(); - - glPushName(reinterpret_cast(this)); - - varray.apply(); - glColor4f(0.25*color.r, 0.25*color.g, 0.25*color.b, 1); - glDrawElements(GL_QUADS, base_seq.size(), GL_UNSIGNED_INT, &base_seq[0]); - if(quality>1) - { - glColor4f(0.85*color.r, 0.85*color.g, 0.85*color.b, 1); - glDrawElements(GL_QUADS, rail_seq.size(), GL_UNSIGNED_INT, &rail_seq[0]); - } - - glPopName(); - glPopMatrix(); + 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; } -void Track3D::render_endpoints() const +Point Track3D::get_node() const { - prepare_render(); - - const vector &endpoints=track.get_type().get_endpoints(); - for(unsigned i=0; i=0 && static_cast(route) &parts=track.get_type().get_parts(); - unsigned index=0; - for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) - build_part(*i, builder, index); -} + glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0); -void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index) -{ - static vector profile; - if(profile.empty()) - { - profile.push_back(Point(0, -0.02, 0)); - profile.push_back(Point(0, -0.014, 0.008)); - profile.push_back(Point(0, -0.014, 0.008)); - profile.push_back(Point(0, 0.014, 0.008)); - profile.push_back(Point(0, 0.014, 0.008)); - profile.push_back(Point(0, 0.02, 0)); - for(unsigned i=0; i<2; ++i) - { - profile.push_back(Point(0, -0.009+i*0.017, 0.008)); - profile.push_back(Point(0, -0.009+i*0.017, 0.0103)); - profile.push_back(Point(0, -0.009+i*0.017, 0.0103)); - profile.push_back(Point(0, -0.008+i*0.017, 0.0103)); - profile.push_back(Point(0, -0.008+i*0.017, 0.0103)); - profile.push_back(Point(0, -0.008+i*0.017, 0.008)); - } - profile.push_back(Point(0, -0.002, 0.012)); - profile.push_back(Point(0, 0.002, 0.012)); - } - static unsigned psize=profile.size(); - - const float &radius=part.radius; - const float &x=part.x; - const float &y=part.y; - const float &length=part.length; - const float &dir=part.dir; - - unsigned nsegs; - if(radius) - { - nsegs=static_cast(part.length*(1<=6) ? length/2 : length; - Point p(x+c*len*i-s*profile[j].y, y+s*len*i+c*profile[j].y, profile[j].z+i*track.get_slope()); - va_builder.vertex(p.x, p.y, p.z); - if(profile[j].z==0) - border.push_back(p); - } - } - } + glPushName(reinterpret_cast(this)); - for(unsigned i=0; i