From 3bd53b49ae654f5abf4c6b8d81d63853461d50e1 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 27 Mar 2010 17:15:23 +0000 Subject: [PATCH] Correct rendering of S-bends --- include/ITrackSegment.hpp | 2 +- src/Editor.cpp | 14 ++++++++++++- src/SBend.cpp | 41 +++++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/include/ITrackSegment.hpp b/include/ITrackSegment.hpp index 0553f8e..1946f69 100644 --- a/include/ITrackSegment.hpp +++ b/include/ITrackSegment.hpp @@ -160,6 +160,6 @@ ITrackSegmentPtr makeCrossoverTrack(); ITrackSegmentPtr makePoints(track::Direction aDirection, bool reflect); ITrackSegmentPtr makeSlopeTrack(track::Direction axis, Vector slope, Vector slopeBefore, Vector slopeAfter); -ITrackSegmentPtr makeSBend(track::Direction dir, int xoff, int yoff); +ITrackSegmentPtr makeSBend(track::Direction dir, int straight, int off); #endif diff --git a/src/Editor.cpp b/src/Editor.cpp index ce41b19..bba6bd6 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -410,7 +410,19 @@ void Editor::drawDraggedTrack() drawDraggedStraight(axis::X, xlen); else if (startDir == endDir) { // An S-bend - ITrackSegmentPtr track = makeSBend(startDir, xlen, ylen); + + if (startDir == axis::Y && dragBegin.y > dragEnd.y) + swap(dragBegin, dragEnd); + + const int straight = + (startDir == axis::X + ? dragEnd.x - dragBegin.x + : dragEnd.y - dragBegin.y) + 1; + const int offset = + startDir == axis::X + ? dragEnd.y - dragBegin.y + : dragEnd.x - dragBegin.x; + ITrackSegmentPtr track = makeSBend(startDir, straight, offset); const Point where = dragBegin; track->setOrigin(where.x, where.y, map->heightAt(where)); diff --git a/src/SBend.cpp b/src/SBend.cpp index e199adc..182b08e 100644 --- a/src/SBend.cpp +++ b/src/SBend.cpp @@ -28,7 +28,7 @@ // Spline curves which start and finish in the same direction class SBend : public ITrackSegment { public: - SBend(track::Direction dir, int xoff, int yoff); + SBend(track::Direction dir, int straight, int off); // ITrackSegment interface void render() const; @@ -54,7 +54,7 @@ private: void ensureValidDirection(track::Direction dir) const; Point origin; - int xOffset, yOffset; + int straight, offset; float height; track::Direction axis; @@ -62,27 +62,27 @@ private: IMeshPtr railMesh; }; -SBend::SBend(track::Direction dir, int xoff, int yoff) - : xOffset(xoff), yOffset(yoff), +SBend::SBend(track::Direction dir, int straight, int off) + : straight(straight), offset(off), height(0.0f), axis(dir) { - assert(xoff > 0); - assert(yoff > 0); - - debug() << "SBend axis=" << axis << " xoff=" << xoff << " yoff=" << yoff; + debug() << "SBend axis=" << axis + << " straight=" << straight << " off=" << off; + + assert(straight > 0); static const float PINCH = 1.0f; - const float pinchX = dir == axis::X ? PINCH : 0.0f; - const float pinchY = dir == axis::Y ? PINCH : 0.0f; - const float xoffF = static_cast(xoff - (dir == axis::Y ? 1 : 0)); - const float yoffF = static_cast(yoff - (dir == axis::X ? 1 : 0)); + const int reflect = (axis == axis::Y ? -1 : 1); + + const float offsetF = static_cast(offset * reflect); + const float straightF = static_cast(straight); Vector p1 = makeVector(0.0f, 0.0f, 0.0f); - Vector p2 = makeVector(pinchX, 0.0f, pinchY); - Vector p3 = makeVector(xoffF - pinchX, 0.0f, yoffF - pinchY); - Vector p4 = makeVector(xoffF, 0.0f, yoffF); + Vector p2 = makeVector(PINCH, 0.0f, 0.0f); + Vector p3 = makeVector(straightF - PINCH, 0.0f, offsetF); + Vector p4 = makeVector(straightF, 0.0f, offsetF); curve = makeBezierCurve(p1, p2, p3, p4); railMesh = makeBezierRailMesh(curve); @@ -103,6 +103,9 @@ void SBend::render() const glTranslatef(0.0f, height, 0.0f); + if (axis == axis::Y) + glRotatef(-90.0f, 0.0f, 1.0f, 0.0f); + renderRailMesh(railMesh); glPopMatrix(); @@ -140,7 +143,7 @@ track::Connection SBend::nextPosition(const track::TravelToken& token) const void SBend::getEndpoints(vector >& output) const { output.push_back(origin); - output.push_back(origin + makePoint(xOffset, yOffset)); + // output.push_back(origin + makePoint(xOffset, yOffset)); } void SBend::getCovers(vector >& output) const @@ -168,7 +171,7 @@ void SBend::ensureValidDirection(track::Direction dir) const { if (!isValidDirection(dir)) throw runtime_error - ("Invalid direction on straight track: " + ("Invalid direction on s-bend track: " + boost::lexical_cast(dir) + " (should be parallel to " + boost::lexical_cast(axis) + ")"); @@ -179,7 +182,7 @@ xml::element SBend::toXml() const assert(false); } -ITrackSegmentPtr makeSBend(track::Direction dir, int xoff, int yoff) +ITrackSegmentPtr makeSBend(track::Direction dir, int straight, int off) { - return ITrackSegmentPtr(new SBend(dir, xoff, yoff)); + return ITrackSegmentPtr(new SBend(dir, straight, off)); } -- 2.39.2