X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2F3d%2Ftrack.cpp;h=85050c97f597a50d0b9b56d185d574bf8735ea62;hb=444c7efb2fadb10e98197a62b791829d821370c0;hp=d4b7079099406d016cee98e5406e0fb68ec17e48;hpb=de09cba1d12af38ff2e7e4312d67624fe63bf0e4;p=r2c2.git diff --git a/source/3d/track.cpp b/source/3d/track.cpp index d4b7079..85050c9 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -14,8 +14,6 @@ Distributed under the GPL using namespace std; using namespace Msp; -#include - namespace Marklin { Track3D::Track3D(Track &t, unsigned q): @@ -27,35 +25,40 @@ Track3D::Track3D(Track &t, unsigned q): build_object(); } +void Track3D::set_color(const Msp::GL::Color &c) +{ + color = c; +} + void Track3D::set_quality(unsigned q) { - quality=q; + quality = q; build_object(); } void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const { - const Point &pos=track.get_position(); - float rot=track.get_rotation(); + const Point &pos = track.get_position(); + float rot = track.get_rotation(); - float c=cos(-angle); - float s=sin(-angle); + 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.x = maxp.x = c*pos.x-s*pos.y; + minp.y = maxp.y = s*pos.x+c*pos.y; - float c2=cos(rot-angle); - float s2=sin(rot-angle); + 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; + 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); + minp.x = min(minp.x, x); + minp.y = min(minp.y, y); + maxp.x = max(maxp.x, x); + maxp.y = max(maxp.y, y); } } @@ -82,24 +85,24 @@ void Track3D::render_endpoints() const { prepare_render(); - const vector &endpoints=track.get_type().get_endpoints(); + const vector &endpoints = track.get_type().get_endpoints(); for(unsigned i=0; i &parts=track.get_type().get_parts(); - unsigned index=0; + const vector &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); } @@ -171,65 +174,32 @@ void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_build profile.push_back(Point(0, -0.002, 0.012)); profile.push_back(Point(0, 0.002, 0.012)); } - static unsigned psize=profile.size(); + 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 = (part.radius ? static_cast(part.length*(1<(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); - } + unsigned k = j&~1; + float dy = profile[k+1].y-profile[k].y; + float dz = profile[k+1].z-profile[k].z; + float d = sqrt(dy*dy+dz*dz); + va_builder.normal(s*dz/d, -c*dz/d, dy/d); + + Point v(p.x+c*profile[j].x-s*profile[j].y, p.y+c*profile[j].y+s*profile[j].x, profile[j].z+i*track.get_slope()/nsegs); + va_builder.vertex(v.x, v.y, v.z); + if(profile[j].z==0) + border.push_back(v); } } @@ -255,7 +225,7 @@ void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_build route_seq[part.route].push_back(base_index+i*psize+19); } - base_index+=(nsegs+1)*psize; + base_index += (nsegs+1)*psize; } } // namespace Marklin