From 0d9237cab248bff0e05559aa2728384facb8f62b Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 31 May 2009 18:18:59 +0100 Subject: [PATCH] Move smoke trails into Train --- include/IBillboard.hpp | 1 + include/IRollingStock.hpp | 2 +- src/Billboard.cpp | 19 ++++++++++++++----- src/Engine.cpp | 16 +++++----------- src/SmokeTrail.cpp | 7 ++++++- src/Train.cpp | 32 +++++++++++++++++++++++++++++++- src/Waggon.cpp | 4 ++-- 7 files changed, 60 insertions(+), 21 deletions(-) diff --git a/include/IBillboard.hpp b/include/IBillboard.hpp index 34fbf57..c2a0d5f 100644 --- a/include/IBillboard.hpp +++ b/include/IBillboard.hpp @@ -30,6 +30,7 @@ struct IBillboard { virtual void render() const = 0; virtual void setPosition(float x, float y, float z) = 0; + virtual void setScale(float aScale) = 0; }; typedef std::shared_ptr IBillboardPtr; diff --git a/include/IRollingStock.hpp b/include/IRollingStock.hpp index d6a75d3..96dd33b 100644 --- a/include/IRollingStock.hpp +++ b/include/IRollingStock.hpp @@ -28,7 +28,7 @@ struct IRollingStock { virtual ~IRollingStock() {} // Update speed, fuel, etc. - virtual void update(int aDelta, Vector aPosition) = 0; + virtual void update(int aDelta) = 0; // Display the base object // This should also display any animation that is attached to diff --git a/src/Billboard.cpp b/src/Billboard.cpp index 381d9c8..4000be7 100644 --- a/src/Billboard.cpp +++ b/src/Billboard.cpp @@ -30,11 +30,12 @@ class BillboardCommon : public IBillboard { public: BillboardCommon(ITexturePtr aTexture) : myTexture(aTexture), - myX(0.0f), myY(0.0f), myZ(0.0f) {} + myX(0.0f), myY(0.0f), myZ(0.0f), myScale(1.0f) {} virtual ~BillboardCommon() {} // IBillboard interface void setPosition(float x, float y, float z); + void setScale(float aScale); private: const ITexturePtr myTexture; @@ -44,6 +45,7 @@ protected: void translate() const; float myX, myY, myZ; + float myScale; }; void BillboardCommon::setPosition(float x, float y, float z) @@ -53,6 +55,11 @@ void BillboardCommon::setPosition(float x, float y, float z) myZ = z; } +void BillboardCommon::setScale(float aScale) +{ + myScale = aScale; +} + void BillboardCommon::translate() const { glTranslatef(myX, myY, myZ); @@ -71,15 +78,17 @@ void BillboardCommon::drawTextureQuad() const myTexture->bind(); + const float w = myScale / 2.0f; + glBegin(GL_QUADS); glTexCoord2f(1.0f, 1.0f); - glVertex2f(0.5f, 0.5f); + glVertex2f(w, w); glTexCoord2f(0.0f, 1.0f); - glVertex2f(-0.5f, 0.5f); + glVertex2f(-w, w); glTexCoord2f(0.0f, 0.0f); - glVertex2f(-0.5f, -0.5f); + glVertex2f(-w, -w); glTexCoord2f(1.0f, 0.0f); - glVertex2f(0.5f, -0.5f); + glVertex2f(w, -w); glEnd(); glPopAttrib(); diff --git a/src/Engine.cpp b/src/Engine.cpp index 57e25f0..8c0a678 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -19,7 +19,8 @@ #include "IModel.hpp" #include "ILogger.hpp" #include "MovingAverage.hpp" -#include "ISmokeTrail.hpp" + +#include using namespace std; @@ -63,7 +64,7 @@ public: // IRollingStock interface void renderModel() const; void renderEffects() const; - void update(int aDelta, Vector aPosition); + void update(int aDelta); double speed() const { return mySpeed; } IControllerPtr controller() { return shared_from_this(); } @@ -89,8 +90,6 @@ private: // Boiler pressure lags behind temperature MovingAverage myBoilerDelay; - - ISmokeTrailPtr mySmokeTrail; static const double MODEL_SCALE; static const double TRACTIVE_EFFORT_KNEE; @@ -111,7 +110,6 @@ Engine::Engine() isBrakeOn(true), myThrottle(0) { myModel = loadModel("pclass.obj", MODEL_SCALE); - mySmokeTrail = makeSmokeTrail(); } // Draw the engine model @@ -123,7 +121,7 @@ void Engine::renderModel() const // Draw the smoke effects void Engine::renderEffects() const { - mySmokeTrail->render(); + } // Calculate the current tractive effort @@ -153,7 +151,7 @@ double Engine::brakeForce() const } // Compute the next state of the engine -void Engine::update(int aDelta, Vector aPosition) +void Engine::update(int aDelta) { // Update the pressure of the boiler // The fire temperature is delayed and then used to increase it @@ -176,10 +174,6 @@ void Engine::update(int aDelta, Vector aPosition) << ", B=" << B << ", a=" << a << ", v=" << mySpeed << " (delta=" << aDelta << ")";*/ - - // Move the smoke trail - mySmokeTrail->setPosition(aPosition.x, aPosition.y + 1.0f, aPosition.z); - mySmokeTrail->update(aDelta); } // User interface to the engine diff --git a/src/SmokeTrail.cpp b/src/SmokeTrail.cpp index 11a0bcc..83103ab 100644 --- a/src/SmokeTrail.cpp +++ b/src/SmokeTrail.cpp @@ -41,6 +41,7 @@ private: // A single smoke particle struct Particle { float x, y, z; + float scale; }; list myParticles; @@ -75,7 +76,10 @@ void SmokeTrail::newParticle() { Particle p = { // Position - myX, myY, myZ + myX, myY, myZ, + + // Scale + 0.1f, }; debug() << makeVector(p.x, p.y, p.z); @@ -88,6 +92,7 @@ void SmokeTrail::render() const for (list::const_iterator it = myParticles.begin(); it != myParticles.end(); ++it) { myBillboard->setPosition((*it).x, (*it).y, (*it).z); + myBillboard->setScale((*it).scale); myBillboard->render(); } } diff --git a/src/Train.cpp b/src/Train.cpp index 3c60204..f394541 100644 --- a/src/Train.cpp +++ b/src/Train.cpp @@ -19,6 +19,7 @@ #include "IRollingStock.hpp" #include "ILogger.hpp" #include "TrackCommon.hpp" +#include "ISmokeTrail.hpp" #include #include @@ -64,8 +65,10 @@ private: void move(double aDistance); void addPart(IRollingStockPtr aVehicle); Vector partPosition(const Part& aPart) const; + void updateSmokePosition(int aDelta); IMapPtr myMap; + ISmokeTrailPtr mySmokeTrail; // Move part of the train across a connection void enterSegment(Part& aPart, const track::Connection& aConnection); @@ -88,6 +91,8 @@ Train::Train(IMapPtr aMap) for (int i = 1; i <= 5; i++) addPart(makeWaggon()); + + mySmokeTrail = makeSmokeTrail(); } void Train::addPart(IRollingStockPtr aVehicle) @@ -138,11 +143,34 @@ void Train::move(double aDistance) } } +void Train::updateSmokePosition(int aDelta) +{ + const Part& e = engine(); + glPushMatrix(); + glLoadIdentity(); + + e.transformer(e.segmentDelta); + + const float smokeOffX = 1.0f; + const float smokeOffY = 1.0f; + glTranslatef(smokeOffX, smokeOffY, 0.0f); + + float matrix[16]; + glGetFloatv(GL_MODELVIEW_MATRIX, matrix); + + glPopMatrix(); + + mySmokeTrail->setPosition(matrix[12], matrix[13], matrix[14]); + mySmokeTrail->update(aDelta); +} + void Train::update(int aDelta) { for (list::iterator it = myParts.begin(); it != myParts.end(); ++it) - (*it).vehicle->update(aDelta, partPosition(*it)); + (*it).vehicle->update(aDelta); + + updateSmokePosition(aDelta); // How many metres does a tile correspond to? const double M_PER_UNIT = 5.0; @@ -183,6 +211,8 @@ void Train::render() const (*it).vehicle->renderEffects(); } + + mySmokeTrail->render(); } Vector Train::front() const diff --git a/src/Waggon.cpp b/src/Waggon.cpp index 7bc9511..26e4eb0 100644 --- a/src/Waggon.cpp +++ b/src/Waggon.cpp @@ -28,7 +28,7 @@ public: Waggon(); ~Waggon() {} - void update(int aDelta, Vector aPosition); + void update(int aDelta); void renderModel() const; void renderEffects() const {} IControllerPtr controller(); @@ -47,7 +47,7 @@ Waggon::Waggon() myModel = loadModel("coal_truck.obj", MODEL_SCALE); } -void Waggon::update(int aDelta, Vector aPosition) +void Waggon::update(int aDelta) { } -- 2.39.2