]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/track.cpp
Change terminology to better distinguish routes on the layout from paths across track...
[r2c2.git] / source / 3d / track.cpp
index e64f42ed3dde6394fde1749979f24e163f57ba5a..b2ee4fad8fd34e942f25ceec0549169bad6cdb70 100644 (file)
@@ -1,13 +1,19 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
 #include <cmath>
 #include <GL/gl.h>
 #include <msp/gl/misc.h>
+#include "libmarklin/tracktype.h"
 #include "track.h"
 
 using namespace std;
 using namespace Msp;
 
-#include <iostream>
-
 namespace Marklin {
 
 Track3D::Track3D(Track &t, unsigned q):
@@ -19,43 +25,48 @@ 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<Point>::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);
        }
 }
 
-void Track3D::render()
+void Track3D::render() const
 {
-       prepare_render();
+       prepare_render(true);
 
-       glPushName((unsigned)this);
+       glPushName(reinterpret_cast<unsigned>(this));
 
        varray.apply();
        glColor4f(0.25*color.r, 0.25*color.g, 0.25*color.b, 1);
@@ -70,77 +81,102 @@ void Track3D::render()
        glPopMatrix();
 }
 
-void Track3D::render_endpoints()
+void Track3D::render_endpoints() const
 {
-       prepare_render();
+       prepare_render(false);
 
-       const Point &pos=track.get_position();
-       const Track::EndpointSeq &endpoints=track.get_endpoints();
-       for(Track::EndpointSeq::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
+       const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
+       for(unsigned i=0; i<endpoints.size(); ++i)
        {
-               GL::set(GL_CULL_FACE, i->link);
-               if(i->link)
+               const Endpoint &ep = endpoints[i];
+               GL::set(GL_CULL_FACE, track.get_link(i));
+               if(track.get_link(i))
                        glColor4f(0.5, 0, 1, 0.5);
                else
                        glColor4f(1, 0, 0.5, 0.5);
 
-               float c=cos(i->rot);
-               float s=sin(i->rot);
+               float c = cos(ep.dir);
+               float s = sin(ep.dir);
+               float z = (i==1 ? track.get_slope() : 0);
 
                glBegin(GL_QUADS);
-               glVertex3f(i->pos.x-s*0.025, i->pos.y+c*0.025, 0);
-               glVertex3f(i->pos.x+s*0.025, i->pos.y-c*0.025, 0);
-               glVertex3f(i->pos.x+s*0.025, i->pos.y-c*0.025, pos.z+i->pos.z+0.02);
-               glVertex3f(i->pos.x-s*0.025, i->pos.y+c*0.025, pos.z+i->pos.z+0.02);
+               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z);
+               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z);
+               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z+0.02);
+               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z+0.02);
+               glEnd();
+       }
+
+       if(abs(track.get_slope())>1e-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_route(int route)
+void Track3D::render_path(int path) const
 {
-       prepare_render();
+       prepare_render(true);
 
        varray.apply();
-       if(route>=0 && static_cast<unsigned>(route)<route_seq.size())
-               glDrawElements(GL_QUADS, route_seq[route].size(), GL_UNSIGNED_INT, &route_seq[route][0]);
+       if(path>=0 && static_cast<unsigned>(path)<path_seq.size())
+               glDrawElements(GL_QUADS, path_seq[path].size(), GL_UNSIGNED_INT, &path_seq[path][0]);
        else
        {
-               for(unsigned i=0; i<route_seq.size(); ++i)
-                       glDrawElements(GL_QUADS, route_seq[i].size(), GL_UNSIGNED_INT, &route_seq[i][0]);
+               for(unsigned i=0; i<path_seq.size(); ++i)
+                       glDrawElements(GL_QUADS, path_seq[i].size(), GL_UNSIGNED_INT, &path_seq[i][0]);
        }
 
        glPopMatrix();
 }
 
-void Track3D::prepare_render()
+void Track3D::prepare_render(bool slope) const
 {
-       const Point &pos=track.get_position();
-       float rot=track.get_rotation();
+       const Point &pos = track.get_position();
+       float rot = track.get_rotation();
 
        glPushMatrix();
        glTranslatef(pos.x, pos.y, pos.z);
        glRotatef(rot*180/M_PI, 0, 0, 1);
+       if(slope)
+               glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
 }
 
 void Track3D::build_object()
 {
        varray.clear();
-       RefPtr<GL::VertexArrayBuilder> builder=varray.modify();
+       GL::VertexArrayBuilder builder(varray);
 
        base_seq.clear();
        rail_seq.clear();
-       route_seq.clear();
-       route_seq.resize(track.get_n_routes());
+       path_seq.clear();
+       path_seq.resize(track.get_type().get_n_paths());
 
-       const Track::PartSeq &parts=track.get_parts();
-       unsigned index=0;
-       for(Track::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
-               build_part(*i, *builder, index);
+       const vector<TrackPart> &parts = track.get_type().get_parts();
+       unsigned index = 0;
+       for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+               build_part(*i, builder, index);
 }
 
-void Track3D::build_part(const Track::Part &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index)
+void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index)
 {
        static vector<Point> profile;
        if(profile.empty())
@@ -163,65 +199,32 @@ void Track3D::build_part(const Track::Part &part, GL::VertexArrayBuilder &va_bui
                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<unsigned>(part.length*(1<<quality))+1 : 1);
+       float plen = part.length;
+       if(part.radius)
+               plen *= abs(part.radius);
 
-       unsigned nsegs;
-       if(radius)
+       for(unsigned i=0; i<=nsegs; ++i)
        {
-               nsegs=(unsigned)(part.length*(1<<quality))+1;
-               Point center(x-sin(dir)*radius, y+cos(dir)*radius, 0);
-               float r=fabs(radius);
-               float start=((radius<0)?M_PI:0)+dir;
-               float angle=(radius<0)?-length:length;
-               int inv=(radius<0)?-1:1;
-               for(unsigned i=0; i<=nsegs; ++i)
-               {
-                       float a=start+i*angle/nsegs;
-                       float c=cos(a);
-                       float s=sin(a);
-
-                       for(unsigned j=0; j<profile.size(); ++j)
-                       {
-                               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 p(center.x+s*(r-profile[j].y*inv), center.y-c*(r-profile[j].y*inv), profile[j].z+i*track.get_slope()/nsegs);
-                               va_builder.vertex(p.x, p.y, p.z);
-                               if(profile[j].z==0)
-                                       border.push_back(p);
-                       }
-               }
-       }
-       else
-       {
-               nsegs=1;
-               float c=cos(dir);
-               float s=sin(dir);
-               for(unsigned i=0; i<2; ++i)
+               float a = part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
+               float c = cos(a);
+               float s = sin(a);
+               Point p = part.get_point(i*plen/nsegs);
+
+               for(unsigned j=0; j<psize; ++j)
                {
-                       for(unsigned j=0; j<profile.size(); ++j)
-                       {
-                               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);
-
-                               float len=(part.dead_end && i==1 && j>=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);
+                       va_builder.vertex(v.x, v.y, v.z);
+                       if(profile[j].z==0)
+                               border.push_back(v);
                }
        }
 
@@ -241,13 +244,13 @@ void Track3D::build_part(const Track::Part &part, GL::VertexArrayBuilder &va_bui
                        rail_seq.push_back(base_index+(i+1)*psize+1+j*2);
                        rail_seq.push_back(base_index+i*psize+1+j*2);
                }
-               route_seq[part.route].push_back(base_index+i*psize+18);
-               route_seq[part.route].push_back(base_index+(i+1)*psize+18);
-               route_seq[part.route].push_back(base_index+(i+1)*psize+19);
-               route_seq[part.route].push_back(base_index+i*psize+19);
+               path_seq[part.path].push_back(base_index+i*psize+18);
+               path_seq[part.path].push_back(base_index+(i+1)*psize+18);
+               path_seq[part.path].push_back(base_index+(i+1)*psize+19);
+               path_seq[part.path].push_back(base_index+i*psize+19);
        }
 
-       base_index+=(nsegs+1)*psize;
+       base_index += (nsegs+1)*psize;
 }
 
 } // namespace Marklin