From 5042d01ab476def34be5358e21840cf09c0104a7 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 5 Jun 2010 12:39:47 +0100 Subject: [PATCH] Render S-bends as static meshes --- src/SBend.cpp | 49 ++++++++++++++++++++++++++++++++++++++-------- src/SlopeTrack.cpp | 4 ++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/SBend.cpp b/src/SBend.cpp index 44a235e..963716c 100644 --- a/src/SBend.cpp +++ b/src/SBend.cpp @@ -21,6 +21,7 @@ #include "XMLBuilder.hpp" #include "BezierCurve.hpp" #include "OpenGLHelper.hpp" +#include "Matrix.hpp" #include #include @@ -28,13 +29,15 @@ #include // Spline curves which start and finish in the same direction -class SBend : public ITrackSegment { +class SBend : public ITrackSegment, + private SleeperHelper, + private BezierHelper { public: SBend(track::Direction dir, int straight, int off); // ITrackSegment interface void render() const; - void merge(IMeshBufferPtr buf) const {} + void merge(IMeshBufferPtr buf) const; void setOrigin(int x, int y, float h); float segmentLength(const track::TravelToken& token) const; bool isValidDirection(const track::Direction& dir) const; @@ -62,10 +65,10 @@ private: track::Direction axis; BezierCurve curve; - IMeshPtr railMesh; + IMeshBufferPtr railBuf; typedef tuple Parameters; - typedef map MeshCache; + typedef map MeshCache; static MeshCache meshCache; }; @@ -95,11 +98,11 @@ SBend::SBend(track::Direction dir, int straight, int off) Parameters parms = make_tuple(straight, offset * reflect); MeshCache::iterator it = meshCache.find(parms); if (it == meshCache.end()) { - railMesh = makeBezierRailMesh(curve); - meshCache[parms] = railMesh; + railBuf = makeBezierRailMesh(curve); + meshCache[parms] = railBuf; } else - railMesh = (*it).second; + railBuf = (*it).second; } void SBend::setOrigin(int x, int y, float h) @@ -107,9 +110,38 @@ void SBend::setOrigin(int x, int y, float h) origin = makePoint(x, y); height = h; } - + +void SBend::merge(IMeshBufferPtr buf) const +{ + Vector off = makeVector( + static_cast(origin.x), + height, + static_cast(origin.y)); + + float yAngle = axis == axis::Y ? -90.0f : 0.0f; + + { + Vector t = makeVector(-0.5f, 0.0f, 0.0f); + buf->merge(railBuf, off + rotateY(t, yAngle), yAngle); + } + + // Draw the sleepers + for (float i = 0.2f; i < curve.length; i += 0.25f) { + Vector v = curve(i / curve.length); + + Vector t = makeVector(v.x - 0.5f, 0.0f, v.z); + + const Vector deriv = curve.deriv(i / curve.length); + const float angle = + radToDeg(atanf(deriv.z / deriv.x)); + + mergeSleeper(buf, off + rotateY(t, yAngle), yAngle - angle); + } +} + void SBend::render() const { +#if 0 glPushMatrix(); glTranslatef( @@ -146,6 +178,7 @@ void SBend::render() const } glPopMatrix(); +#endif } float SBend::segmentLength(const track::TravelToken& token) const diff --git a/src/SlopeTrack.cpp b/src/SlopeTrack.cpp index 3ff6a78..2a71d02 100644 --- a/src/SlopeTrack.cpp +++ b/src/SlopeTrack.cpp @@ -32,8 +32,8 @@ // Like StraightTrack but with a change of height class SlopeTrack : public ITrackSegment, - public SleeperHelper, - public BezierHelper { + private SleeperHelper, + private BezierHelper { public: SlopeTrack(track::Direction axis, Vector slope, Vector slopeBefore, Vector slopeAfter); -- 2.39.2