mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
10 KiB
357 lines
10 KiB
|
8 years ago
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
6 years ago
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
|
||
|
3 years ago
|
2020, 2021, 2022, 2023 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
8 years ago
|
|
||
|
|
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.
|
||
|
|
*/
|
||
|
|
|
||
|
6 years ago
|
#include <vector>
|
||
|
|
|
||
|
8 years ago
|
#include "Magnum/Timeline.h"
|
||
|
8 years ago
|
#include "Magnum/Math/Bezier.h"
|
||
|
|
#include "Magnum/Math/Matrix3.h"
|
||
|
8 years ago
|
#include "Magnum/Math/Quaternion.h"
|
||
|
8 years ago
|
#include "Magnum/Math/Packing.h"
|
||
|
|
#include "Magnum/Animation/Easing.h"
|
||
|
8 years ago
|
#include "Magnum/Animation/Player.h"
|
||
|
8 years ago
|
|
||
|
4 years ago
|
#define DOXYGEN_ELLIPSIS(...) __VA_ARGS__
|
||
|
|
|
||
|
8 years ago
|
using namespace Magnum;
|
||
|
|
using namespace Magnum::Math::Literals;
|
||
|
|
|
||
|
2 years ago
|
/* Make sure the name doesn't conflict with any other snippets to avoid linker
|
||
|
|
warnings, unlike with `int main()` there now has to be a declaration to
|
||
|
|
avoid -Wmisssing-prototypes */
|
||
|
|
void mainAnimation();
|
||
|
|
void mainAnimation() {
|
||
|
8 years ago
|
{
|
||
|
|
Float t{};
|
||
|
|
{
|
||
|
7 years ago
|
Vector3 a, b;
|
||
|
8 years ago
|
/* [ease] */
|
||
|
|
auto lerpBounceIn =
|
||
|
|
Animation::ease<Vector3, Math::lerp, Animation::Easing::bounceIn>();
|
||
|
|
|
||
|
|
Vector3 result1 = Math::lerp(a, b, Animation::Easing::bounceIn(t));
|
||
|
|
Vector3 result2 = lerpBounceIn (a, b, t);
|
||
|
|
/* [ease] */
|
||
|
|
static_cast<void>(result1);
|
||
|
|
static_cast<void>(result2);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [unpack] */
|
||
|
4 years ago
|
UnsignedShort a = DOXYGEN_ELLIPSIS(0), b = DOXYGEN_ELLIPSIS(0);
|
||
|
8 years ago
|
auto lerpPacked =
|
||
|
|
Animation::unpack<UnsignedShort, Float, Math::lerp, Math::unpack<Float>>();
|
||
|
|
|
||
|
|
Float result1 = Math::lerp(Math::unpack<Float>(a), Math::unpack<Float>(b), t);
|
||
|
|
Float result2 = lerpPacked(a, b, t);
|
||
|
|
/* [unpack] */
|
||
|
|
static_cast<void>(result1);
|
||
|
|
static_cast<void>(result2);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [unpackEase] */
|
||
|
4 years ago
|
UnsignedShort a = DOXYGEN_ELLIPSIS(0), b = DOXYGEN_ELLIPSIS(0);
|
||
|
8 years ago
|
auto lerpPackedBounceIn = Animation::unpackEase<UnsignedShort, Float,
|
||
|
|
Math::lerp, Math::unpack<Float>, Animation::Easing::bounceIn>();
|
||
|
|
|
||
|
|
Float result1 = Math::lerp(Math::unpack<Float>(a), Math::unpack<Float>(b),
|
||
|
|
Animation::Easing::bounceIn(t));
|
||
|
|
Float result2 = lerpPackedBounceIn(a, b, t);
|
||
|
|
/* [unpackEase] */
|
||
|
|
static_cast<void>(result1);
|
||
|
|
static_cast<void>(result2);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
Vector3 a, b;
|
||
|
|
Float t{};
|
||
|
|
{
|
||
|
|
/* [Easing-factor] */
|
||
|
|
Vector3 result = Math::lerp(a, b, Animation::Easing::quadraticInOut(t));
|
||
|
|
/* [Easing-factor] */
|
||
|
|
static_cast<void>(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Easing-ease] */
|
||
|
|
auto lerpQuadraticInOut =
|
||
|
|
Animation::ease<Vector3, Math::lerp, Animation::Easing::quadraticInOut>();
|
||
|
|
|
||
|
|
Vector3 result = lerpQuadraticInOut(a, b, t);
|
||
|
|
/* [Easing-ease] */
|
||
|
|
static_cast<void>(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Easing-clamp] */
|
||
|
|
auto lerpCircularOutClamped = Animation::easeClamped<
|
||
|
|
Vector3, Math::lerp, Animation::Easing::quadraticInOut>();
|
||
|
|
|
||
|
|
Vector3 result1 = Math::lerp(a, b,
|
||
|
|
Math::clamp(0.0f, 1.0f, Animation::Easing::circularOut(t)));
|
||
|
|
Vector3 result2 = lerpCircularOutClamped(a, b, t);
|
||
|
|
/* [Easing-clamp] */
|
||
|
|
static_cast<void>(result1);
|
||
|
|
static_cast<void>(result2);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Easing-bezier-transform] */
|
||
|
|
Matrix3 transformation;
|
||
|
|
CubicBezier2D easing;
|
||
|
|
CubicBezier2D transformed{
|
||
|
|
transformation.transformPoint(easing[0]),
|
||
|
|
transformation.transformPoint(easing[1]),
|
||
|
|
transformation.transformPoint(easing[2]),
|
||
|
|
transformation.transformPoint(easing[3])};
|
||
|
|
/* [Easing-bezier-transform] */
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Easing-smoothstep] */
|
||
|
|
Math::lerp(a, b, Animation::Easing::smoothstep(t));
|
||
|
|
/* [Easing-smoothstep] */
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
8 years ago
|
{
|
||
|
|
/* [Player-usage] */
|
||
|
|
const Animation::TrackView<Float, Vector3> translation;
|
||
|
|
const Animation::TrackView<Float, Quaternion> rotation;
|
||
|
|
const Animation::TrackView<Float, Vector3> scaling;
|
||
|
|
|
||
|
|
Vector3 objectScaling;
|
||
|
|
Quaternion objectRotation;
|
||
|
|
Vector3 objectTranslation;
|
||
|
|
|
||
|
|
Animation::Player<Float> player;
|
||
|
|
player.add(scaling, objectScaling)
|
||
|
|
.add(rotation, objectRotation)
|
||
|
|
.add(translation, objectTranslation);
|
||
|
|
/* [Player-usage] */
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
const Animation::TrackView<Float, Vector3> translation;
|
||
|
|
const Animation::TrackView<Float, Quaternion> rotation;
|
||
|
|
const Animation::TrackView<Float, Vector3> scaling;
|
||
|
|
struct Object3D {
|
||
|
|
Object3D& setTranslation(const Vector3&) { return *this; }
|
||
|
|
Object3D& setRotation(const Quaternion&) { return *this; }
|
||
|
|
Object3D& setScaling(const Vector3&) { return *this; }
|
||
|
|
};
|
||
|
|
/* [Player-usage-callback] */
|
||
|
4 years ago
|
Object3D* object = DOXYGEN_ELLIPSIS({});
|
||
|
8 years ago
|
|
||
|
|
Animation::Player<Float> player;
|
||
|
|
player.addWithCallback(scaling,
|
||
|
8 years ago
|
[](Float, const Vector3& scaling, Object3D& object) {
|
||
|
8 years ago
|
object.setScaling(scaling);
|
||
|
|
}, *object);
|
||
|
|
player.addWithCallback(rotation,
|
||
|
8 years ago
|
[](Float, const Quaternion& rotation, Object3D& object) {
|
||
|
8 years ago
|
object.setRotation(rotation);
|
||
|
|
}, *object);
|
||
|
|
player.addWithCallback(translation,
|
||
|
8 years ago
|
[](Float, const Vector3& translation, Object3D& object) {
|
||
|
8 years ago
|
object.setTranslation(translation);
|
||
|
|
}, *object);
|
||
|
|
/* [Player-usage-callback] */
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Player-usage-playback] */
|
||
|
|
Animation::Player<Float> player;
|
||
|
|
Timeline timeline;
|
||
|
|
|
||
|
|
// during initialization
|
||
|
|
timeline.start();
|
||
|
|
player.play(timeline.previousFrameTime());
|
||
|
|
|
||
|
|
// every frame
|
||
|
|
player.advance(timeline.previousFrameTime());
|
||
|
|
/* [Player-usage-playback] */
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Player-usage-chrono] */
|
||
|
|
Animation::Player<std::chrono::nanoseconds, Float> player;
|
||
|
|
// add tracks…
|
||
|
|
|
||
|
|
// start the animation
|
||
|
|
player.play(std::chrono::system_clock::now().time_since_epoch());
|
||
|
|
|
||
|
|
// call every frame
|
||
|
|
player.advance(std::chrono::system_clock::now().time_since_epoch());
|
||
|
|
/* [Player-usage-chrono] */
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Player-higher-order] */
|
||
|
|
struct Data {
|
||
|
|
Animation::Player<Float> player; // player we want to control
|
||
|
|
Timeline timeline;
|
||
|
|
} data;
|
||
|
|
|
||
|
|
Animation::Track<Float, Animation::State> stateTrack{{
|
||
|
|
{3.0f, Animation::State::Playing},
|
||
|
|
{3.0f, Animation::State::Paused},
|
||
|
|
{3.5f, Animation::State::Playing},
|
||
|
|
{5.0f, Animation::State::Stopped}
|
||
|
|
}, Math::select};
|
||
|
|
Animation::State state;
|
||
|
|
|
||
|
|
Animation::Player<Float> controller;
|
||
|
|
controller.addWithCallbackOnChange(stateTrack,
|
||
|
8 years ago
|
[](Float, const Animation::State& state, Data& data) {
|
||
|
8 years ago
|
data.player.setState(state, data.timeline.previousFrameTime());
|
||
|
|
}, state, data);
|
||
|
|
/* [Player-higher-order] */
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
Timeline timeline;
|
||
|
|
/* [Player-higher-order-animated-time] */
|
||
|
|
Animation::Player<Float> player; // player we want to control
|
||
|
|
|
||
|
|
Animation::Track<Float, Float> timeTrack{{
|
||
|
|
{0.0f, 0.0f}, /* Start normal */
|
||
|
|
{1.0f, 1.0f}, /* Then speed up */
|
||
|
|
{2.0f, 3.0f}, /* Pause for a bit */
|
||
|
|
{5.0f, 3.0f}, /* And normal again */
|
||
|
|
{6.0f, 4.0f}
|
||
|
|
}, Animation::Interpolation::Linear};
|
||
|
|
|
||
|
|
Animation::Player<Float> timer;
|
||
|
|
timer.addWithCallback(timeTrack,
|
||
|
8 years ago
|
[](Float, const Float& time, Animation::Player<Float>& player) {
|
||
|
8 years ago
|
player.advance(time);
|
||
|
|
}, player);
|
||
|
|
|
||
|
|
/* Calls player.advance() with the animated time */
|
||
|
|
timer.advance(timeline.previousFrameTime());
|
||
|
|
/* [Player-higher-order-animated-time] */
|
||
|
|
}
|
||
|
|
|
||
|
8 years ago
|
{
|
||
|
8 years ago
|
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Can't call + on lambdas */
|
||
|
8 years ago
|
/* [Player-addRawCallback] */
|
||
|
|
Animation::Track<Float, Int> track;
|
||
|
|
|
||
|
|
Int result;
|
||
|
|
std::vector<Int> data;
|
||
|
|
auto callback = [](std::vector<Int>& data, Int value) {
|
||
|
|
data.push_back(value);
|
||
|
|
};
|
||
|
|
|
||
|
|
Animation::Player<Float> player;
|
||
|
|
player.addRawCallback(track,
|
||
|
7 years ago
|
[](const Animation::TrackViewStorage<const Float>& track, Float key,
|
||
|
8 years ago
|
std::size_t& hint, void* destination, void(*callback)(), void* userData) {
|
||
|
7 years ago
|
Int value = static_cast<const Animation::TrackView<const Float, const Int>&>(track)
|
||
|
8 years ago
|
.atStrict(key, hint);
|
||
|
|
if(value == *static_cast<Int*>(destination)) return;
|
||
|
|
*static_cast<Int*>(destination) = value;
|
||
|
|
reinterpret_cast<void(*)(std::vector<Int>&, Int)>(callback)
|
||
|
|
(*static_cast<std::vector<Int>*>(userData), value);
|
||
|
|
}, &result, reinterpret_cast<void(*)()>(+callback), &data);
|
||
|
|
/* [Player-addRawCallback] */
|
||
|
8 years ago
|
#endif
|
||
|
8 years ago
|
}
|
||
|
|
|
||
|
8 years ago
|
{
|
||
|
|
/* [Track-usage] */
|
||
|
|
const Animation::Track<Float, Vector2> jump{{
|
||
|
|
{0.0f, Vector2::yAxis(0.0f)},
|
||
|
|
{1.0f, Vector2::yAxis(0.5f)},
|
||
|
|
{2.0f, Vector2::yAxis(0.75f)},
|
||
|
|
{3.0f, Vector2::yAxis(0.875f)},
|
||
|
|
{4.0f, Vector2::yAxis(0.75f)},
|
||
|
|
{5.0f, Vector2::yAxis(0.5f)},
|
||
|
|
{6.0f, Vector2::yAxis(0.0f)}
|
||
|
|
}, Math::lerp, Animation::Extrapolation::Constant};
|
||
|
|
|
||
|
|
Vector2 position = jump.at(2.2f); // y = 0.775
|
||
|
|
/* [Track-usage] */
|
||
|
7 years ago
|
static_cast<void>(position);
|
||
|
|
}
|
||
|
8 years ago
|
|
||
|
|
{
|
||
|
7 years ago
|
const Animation::Track<Float, Vector2> jump;
|
||
|
8 years ago
|
/* [Track-performance-hint] */
|
||
|
|
std::size_t hint = 0;
|
||
|
|
Vector2 position = jump.at(2.2f, hint); // y = 0.775, hint = 2
|
||
|
|
/* [Track-performance-hint] */
|
||
|
|
static_cast<void>(position);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
7 years ago
|
const Animation::Track<Float, Vector2> jump;
|
||
|
8 years ago
|
/* [Track-performance-strict] */
|
||
|
|
std::size_t hint = 0;
|
||
|
|
Vector2 position = jump.atStrict(2.2f, hint); // y = 0.775, hint = 2
|
||
|
|
/* [Track-performance-strict] */
|
||
|
|
static_cast<void>(position);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
/* [Track-performance-cache] */
|
||
|
|
struct Keyframe {
|
||
|
|
Float time;
|
||
|
|
Vector2 position;
|
||
|
|
Deg rotation;
|
||
|
|
};
|
||
|
|
const Keyframe data[]{
|
||
|
|
{0.0f, Vector2::yAxis(0.0f), 0.0_degf},
|
||
|
|
{1.0f, Vector2::yAxis(0.5f), 60.0_degf},
|
||
|
|
{2.0f, Vector2::yAxis(0.75f), 80.0_degf},
|
||
|
|
{3.0f, Vector2::yAxis(0.875f), 90.0_degf},
|
||
|
|
{4.0f, Vector2::yAxis(0.75f), 100.0_degf},
|
||
|
|
{5.0f, Vector2::yAxis(0.5f), 120.0_degf},
|
||
|
|
{6.0f, Vector2::yAxis(0.0f), 180.0_degf}
|
||
|
|
};
|
||
|
|
|
||
|
7 years ago
|
Animation::TrackView<const Float, const Vector2> positions{
|
||
|
7 years ago
|
{data, &data[0].time, Containers::arraySize(data), sizeof(Keyframe)},
|
||
|
|
{data, &data[0].position, Containers::arraySize(data), sizeof(Keyframe)},
|
||
|
8 years ago
|
Math::lerp};
|
||
|
7 years ago
|
Animation::TrackView<const Float, const Deg> rotations{
|
||
|
7 years ago
|
{data, &data[0].time, Containers::arraySize(data), sizeof(Keyframe)},
|
||
|
|
{data, &data[0].rotation, Containers::arraySize(data), sizeof(Keyframe)},
|
||
|
8 years ago
|
Math::lerp};
|
||
|
|
|
||
|
|
Float time = 2.2f;
|
||
|
|
std::size_t hint = 0;
|
||
|
|
Vector2 position = positions.atStrict(time, hint); // y = 0.775f
|
||
|
|
Deg rotation = rotations.atStrict(time, hint); // φ = 82°
|
||
|
|
/* [Track-performance-cache] */
|
||
|
|
static_cast<void>(position);
|
||
|
|
static_cast<void>(rotation);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|