| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- //
- // Copyright (c) 2008-2013 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Context.h"
- #include "Log.h"
- #include "Node.h"
- #include "Scene.h"
- #include "Spline.h"
- namespace Urho3D
- {
- extern const char* SCENE_CATEGORY;
- Spline::Spline(Context* context) :
- Component(context),
- interpolationMode_(BEZIER_CURVE),
- speed_(1.f),
- elapsedTime_(0.f),
- length_(0.f),
- traveled_(0.f),
- attached_(false)
- {
- }
- void Spline::RegisterObject(Context* context)
- {
- context->RegisterFactory<Spline>(SCENE_CATEGORY);
- ACCESSOR_ATTRIBUTE(Spline, VAR_VARIANTVECTOR, "Control Points", GetControlPointsAttr, SetControlPointsAttr, VariantVector, Variant::emptyVariantVector, AM_FILE);
- ATTRIBUTE(Spline, VAR_FLOAT, "Speed", speed_, 1.f, AM_FILE);
- ATTRIBUTE(Spline, VAR_INT, "Interpolation Mode", interpolationMode_, BEZIER_CURVE, AM_FILE);
- ATTRIBUTE(Spline, VAR_FLOAT, "Traveled", traveled_, 0.f, AM_FILE | AM_NOEDIT);
- ATTRIBUTE(Spline, VAR_FLOAT, "Elapsed Time", elapsedTime_, 0.f, AM_FILE | AM_NOEDIT);
- ATTRIBUTE(Spline, VAR_FLOAT, "Length", length_, 0.f, AM_FILE | AM_NOEDIT);
- ATTRIBUTE(Spline, VAR_BOOL, "Attached", attached_, false, AM_FILE);
- }
- void Spline::SetControlPoints(const Vector<Vector3> controlPoints)
- {
- controlPoints_ = controlPoints;
- }
- void Spline::SetInterpolationMode(InterpolationMode interpolationMode)
- {
- interpolationMode_ = interpolationMode;
- }
- Vector3 Spline::GetPosition(float factor)
- {
- float t = factor;
- if (t < 0.f)
- t = 0.0f;
- else if (t > 1.0f)
- t = 1.0f;
- switch (interpolationMode_)
- {
- case BEZIER_CURVE:
- return BezierMove(controlPoints_, t);
- default:
- return Vector3::ZERO;
- }
- }
- void Spline::Push(const Vector3& controlPoint)
- {
- controlPoints_.Push(controlPoint);
- }
- void Spline::Pop()
- {
- controlPoints_.Pop();
- }
- void Spline::Attach()
- {
- if (controlPoints_.Size() > 0)
- {
- CalculateLength();
-
- switch (interpolationMode_)
- {
- case BEZIER_CURVE:
- if (controlPoints_.Size() < 2)
- {
- LOGERRORF("Spline on Node[%d,%s] in Beizer Curve mode attempted with less than two control points.", GetNode()->GetID(), GetNode()->GetName().CString());
- return;
- }
- GetNode()->SetPosition(BezierMove(controlPoints_, traveled_));
- break;
- }
- attached_ = true;
- }
- }
- void Spline::Move(float timeStep)
- {
- if (!attached_ || traveled_ >= 1.0f || length_ <= 0.0f)
- return;
- elapsedTime_ += timeStep;
- float distanceCovered = elapsedTime_ * speed_;
- traveled_ = distanceCovered / length_;
- switch (interpolationMode_)
- {
- case BEZIER_CURVE:
- if (controlPoints_.Size() < 2)
- {
- LOGERRORF("Spline on Node[%d,%s] in Beizer Curve mode attempted with less than two control points.", GetNode()->GetID(), GetNode()->GetName().CString());
- return;
- }
- GetNode()->SetPosition(BezierMove(controlPoints_,traveled_));
- break;
- }
- }
- void Spline::Detach()
- {
- attached_ = false;
- }
- void Spline::Reset()
- {
- attached_ = false;
- traveled_ = 0.f;
- elapsedTime_ = 0.f;
- }
- Urho3D::VariantVector Spline::GetControlPointsAttr() const
- {
- VariantVector ret;
- for (unsigned i = 0; i < controlPoints_.Size(); i++)
- ret.Push(controlPoints_[i]);
- return ret;
- }
- void Spline::SetControlPointsAttr(VariantVector value)
- {
- for (unsigned i = 0; i < value.Size(); i++)
- controlPoints_.Push(value[i].GetVector3());
- }
- void Spline::CalculateLength()
- {
- length_ = 0.f;
- if (controlPoints_.Size() <= 0)
- {
- return;
- }
- switch (interpolationMode_)
- {
- case BEZIER_CURVE:
- Vector3 a = controlPoints_[0];
- for (float f = 0.000f; f <= 1.000f; f += 0.001f)
- {
- Vector3 b = BezierMove(controlPoints_, f);
- length_ += Abs((a - b).Length());
- a = b;
- }
- break;
- }
- }
- Vector3 Spline::BezierMove(Vector<Vector3>& controlPoints, float t)
- {
- if (controlPoints.Size() == 2)
- {
- return controlPoints[0].Lerp(controlPoints[1], t);
- }
- else
- {
- Vector<Vector3> newControlPoints;
- for (unsigned i = 1; i < controlPoints.Size(); i++)
- {
- newControlPoints.Push(controlPoints[i - 1].Lerp(controlPoints[i], t));
- }
- return BezierMove(newControlPoints, t);
- }
- }
- }
|