]> git.tdb.fi Git - r2c2.git/commitdiff
Correctly handle slope when snapping tracks
authorMikko Rasa <tdb@tdb.fi>
Thu, 10 Dec 2009 10:26:21 +0000 (10:26 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 10 Dec 2009 10:26:21 +0000 (10:26 +0000)
Render sloped tracks with rotation, not by shearing the mesh
Render a pillar under the higher endpoint of a sloped track
Hide zero slope from tooltip

source/3d/track.cpp
source/3d/track.h
source/designer/designer.cpp
source/libmarklin/track.cpp

index 85050c97f597a50d0b9b56d185d574bf8735ea62..47e6e61161bb0845befa987756251e53c7e47527 100644 (file)
@@ -64,7 +64,7 @@ void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
 
 void Track3D::render() const
 {
-       prepare_render();
+       prepare_render(true);
 
        glPushName(reinterpret_cast<unsigned>(this));
 
@@ -83,7 +83,7 @@ void Track3D::render() const
 
 void Track3D::render_endpoints() const
 {
-       prepare_render();
+       prepare_render(false);
 
        const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
        for(unsigned i=0; i<endpoints.size(); ++i)
@@ -97,12 +97,35 @@ void Track3D::render_endpoints() const
 
                float c = cos(ep.dir);
                float s = sin(ep.dir);
+               float z = (i==1 ? track.get_slope() : 0);
 
                glBegin(GL_QUADS);
-               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, 0);
-               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, 0);
-               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, 0.02);
-               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, 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();
        }
 
@@ -111,7 +134,7 @@ void Track3D::render_endpoints() const
 
 void Track3D::render_route(int route) const
 {
-       prepare_render();
+       prepare_render(true);
 
        varray.apply();
        if(route>=0 && static_cast<unsigned>(route)<route_seq.size())
@@ -125,7 +148,7 @@ void Track3D::render_route(int route) const
        glPopMatrix();
 }
 
-void Track3D::prepare_render() const
+void Track3D::prepare_render(bool slope) const
 {
        const Point &pos = track.get_position();
        float rot = track.get_rotation();
@@ -133,6 +156,8 @@ void Track3D::prepare_render() const
        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()
@@ -196,7 +221,7 @@ void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_build
                        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);
+                       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);
index 660d1cff5fca9105a5d7dc04e03164c9b6cc56b2..3630ac6ae4b8f4a6e4bee1a75e162da67f67e53e 100644 (file)
@@ -40,7 +40,7 @@ public:
        void render_endpoints() const;
        void render_route(int) const;
 private:
-       void prepare_render() const;
+       void prepare_render(bool) const;
        void build_object();
        void build_part(const TrackPart &, Msp::GL::VertexArrayBuilder &, unsigned &);
 };
index ba804f27980c30e6e368cabc7e95979b0e5229be..c5c274edbda8ccd3163b7f81ff30d777aed6cb15 100644 (file)
@@ -287,8 +287,8 @@ void Designer::tick()
                        ostringstream ss;
                        ss.precision(2);
                        ss<<ttype.get_article_number()<<' '<<ttype.get_description();
-                       if(mode!=CATALOGUE)
-                               ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
+                       if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
+                               ss<<" (slope "<<abs(track.get_slope()/ttype.get_total_length()*100)<<"%)";
                        if(track.get_turnout_id())
                                ss<<" (turnout "<<track.get_turnout_id()<<')';
                        else if(track.get_sensor_id())
index 01b789373c6fb655b712a6558533ec588adc08f9..617328a64b7d24e474b39a50deae8e2f12dbda1b 100644 (file)
@@ -151,7 +151,12 @@ bool Track::snap_to(Track &other, bool link)
                        if(dx*dx+dy*dy<limit)
                        {
                                set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
-                               set_position(Point(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)), epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)), epp2.z));
+                               Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
+                                       epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
+                                       epp2.z);
+                               if(eps.size()==2 && i==1)
+                                       p.z -= slope;
+                               set_position(p);
 
                                if(link)
                                {