X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2F3d%2Ftrack.cpp;h=401dd2d9634956a15d645047881663e1d355c44c;hb=1ff06c5bc46a677fa389ef86c6b26664368f1653;hp=1df93a29ca76a60f49904636dd7151dc682c2765;hpb=e179208d1c8370f87ce6ec87f649e5e99eb5724f;p=r2c2.git diff --git a/source/3d/track.cpp b/source/3d/track.cpp index 1df93a2..401dd2d 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -1,259 +1,107 @@ /* $Id$ -This file is part of the MSP Märklin suite -Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa +This file is part of R²C² +Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include -#include +#include #include -#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(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())), + path(new Path3D(*this)) { - build_object(); -} + layout.add_track(*this); + layout.get_scene().add(*this); -void Track3D::set_color(const Msp::GL::Color &c) -{ - color = c; + const vector &type_eps = track.get_type().get_endpoints(); + for(unsigned i=0; i::iterator i=endpoints.begin(); i!=endpoints.end(); ++i) + delete *i; } void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const { - const Point &pos = track.get_position(); - float rot = track.get_rotation(); - float slope = track.get_slope(); + type.get_bounds(angle-track.get_rotation(), minp, maxp); float c = cos(-angle); float s = sin(-angle); - minp.x = maxp.x = c*pos.x-s*pos.y; - minp.y = maxp.y = s*pos.x+c*pos.y; - minp.z = pos.z+min(slope, 0.0f); - maxp.z = pos.z+0.01+max(slope, 0.0f); - - float c2 = cos(rot-angle); - float s2 = sin(rot-angle); - - for(vector::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); - } -} - -void Track3D::render() const -{ - prepare_render(true); - - 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(); -} - -void Track3D::render_endpoints() const -{ - prepare_render(false); - - const vector &endpoints = track.get_type().get_endpoints(); - for(unsigned i=0; i1e-4) - { - Point p; - if(track.get_slope()>0) - { - p = endpoints[1].pos; - p.z += track.get_slope(); - } - else - p = endpoints[0].pos; - glBegin(GL_TRIANGLE_STRIP); - for(unsigned i=0; i<=16; ++i) - { - float c = cos(i*M_PI/8); - float s = sin(i*M_PI/8); - glNormal3f(c, s, 0); - glVertex3f(p.x+c*0.01, p.y+s*0.01, p.z); - glVertex3f(p.x+c*0.01, p.y+s*0.01, -track.get_position().z); - } - glEnd(); - } - - glPopMatrix(); -} - -void Track3D::render_path(int path) const -{ - prepare_render(true); + 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; - varray.apply(); - if(path>=0 && static_cast(path)0) + maxp.z += slope; else - { - for(unsigned i=0; i &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); + 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::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index) +void Track3D::render(const GL::Tag &tag) const { - 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(); + GL::PushMatrix push_mat; - unsigned nsegs = (part.radius ? static_cast(part.length*(1<(this)); - for(unsigned i=0; i