Files
metin2-SDK/gr2/tests/animation.cpp
T
shenleiandClaude Opus 5.5 de8fa40098 gr2: align curve sampling with Granny 2.11 (loop window, cubic, degree 0)
- Curve::eval rebuilds the B-spline window like ConstructBSplineBuffers:
  clamped ends repeat the endpoint; underflow/overflow looping takes knots
  from the other end (+-duration, skipping the duplicated last knot); equal
  trailing knots get +1e-5.
- Degree 3 (CubicCoefficients) supported; degrees 0..3 share one path.
- Degree 0 takes the control of the first knot > t (SampleBSpline0 = pi[0]);
  DaKeyframes32f curves are marked keyframed and keep frame trunc(t/dt).
- Loop flags threaded through BoneTrack::sample, Animation::sample_local and
  sample_pose; control_loop_state() mirrors GetControlLoopState.
- Gr2ModelInstance::SetAnimationLoop(); animated attachments (fmod) loop.
- Quaternion continuity stays on for every span (Granny: buffer path only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 13:39:33 +09:00

148 lines
5.2 KiB
C++

#include <gr2/types.h>
#include <cmath>
#include <cstdio>
#include <vector>
int main() {
// ScaleShearCurve uses 9 values per control point. A quadratic span reads
// three control points, so this is the regression case for the former 3*4
// stack buffer in Curve::eval().
gr2::Curve curve;
curve.degree = 2;
curve.dim = 9;
curve.knots = {0.0f, 1.0f, 2.0f};
curve.controls.resize(27);
for (size_t i = 0; i < curve.controls.size(); ++i) {
curve.controls[i] = 1.0f + static_cast<float>(i) * 0.01f;
}
float out[9] = {};
curve.eval(0.5f, out);
for (float value : out) {
if (!std::isfinite(value)) {
std::fprintf(stderr, "non-finite 9D curve output\n");
return 1;
}
}
// Degree 0 with several knots: granny SampleBSpline0 takes pi[0], the control of
// the first knot > t (CurveFindKnot, clamped to n-1).
gr2::Curve steps;
steps.degree = 0;
steps.dim = 1;
steps.knots = {0.0f, 0.5f, 1.0f};
steps.controls = {10.0f, 20.0f, 30.0f};
const float step_t[] = {-1.0f, 0.0f, 0.49f, 0.5f, 0.99f, 1.0f, 2.0f};
const float step_v[] = {10.0f, 20.0f, 20.0f, 30.0f, 30.0f, 30.0f, 30.0f};
for (size_t i = 0; i < sizeof step_t / sizeof step_t[0]; ++i) {
float v = 0.0f;
steps.eval(step_t[i], &v);
if (v != step_v[i]) {
std::fprintf(stderr, "degree-0 step t=%f got %f want %f\n", step_t[i], v, step_v[i]);
return 1;
}
}
// DaKeyframes32f (keyframed) picks frame trunc(t/dt), i.e. the last knot <= t.
steps.keyframed = true;
const float key_v[] = {10.0f, 10.0f, 10.0f, 20.0f, 20.0f, 30.0f, 30.0f};
for (size_t i = 0; i < sizeof step_t / sizeof step_t[0]; ++i) {
float v = 0.0f;
steps.eval(step_t[i], &v);
if (v != key_v[i]) {
std::fprintf(stderr, "keyframed t=%f got %f want %f\n", step_t[i], v, key_v[i]);
return 1;
}
}
auto near = [](float a, float b) { return std::fabs(a - b) < 1e-5f; };
// Quadratic loop window (ConstructBSplineBuffers with Prev/Next = the curve itself).
// The last knot repeats the first one, as exported looping curves do.
gr2::Curve loop;
loop.degree = 2;
loop.dim = 1;
loop.knots = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f};
loop.controls = {0.0f, 1.0f, 2.0f, 1.0f, 0.0f};
struct LoopCase { float t; bool ul, ol; float want; };
const LoopCase loop_cases[] = {
{0.5f, false, false, 0.125f}, // clamped: the missing knot -1 repeats knot 0
{0.5f, true, false, 0.25f}, // underflow: knot 3 - 4 with control 1
{3.5f, false, true, 1.0f}, // overflow: knots 0+4, 1+4
{0.0f, true, true, 0.5f}, // seam is continuous when looping ...
{3.99999f, true, true, 0.5f},
{0.0f, false, false, 0.0f}, // ... and not when clamped
};
for (const auto& c : loop_cases) {
float v = -1.0f;
loop.eval(c.t, &v, c.ul, c.ol, 4.0f);
if (std::fabs(v - c.want) > 1e-4f) {
std::fprintf(stderr, "loop t=%f ul=%d ol=%d got %f want %f\n", c.t, c.ul, c.ol, v, c.want);
return 1;
}
}
// Cubic (granny CubicCoefficients). Uniform knots at t == knot: 1/6, 4/6, 1/6, 0.
gr2::Curve cubic;
cubic.degree = 3;
cubic.dim = 1;
for (int i = 0; i < 8; ++i) {
cubic.knots.push_back(float(i));
cubic.controls.push_back(float(i * i));
}
float cv = 0.0f;
cubic.eval(3.0f, &cv);
if (!near(cv, 26.0f / 6.0f)) {
std::fprintf(stderr, "cubic t=3 got %f want %f\n", cv, 26.0f / 6.0f);
return 1;
}
for (float& c : cubic.controls) c = 5.0f; // partition of unity, incl. clamped ends
const float cubic_t[] = {0.0f, 0.3f, 3.3f, 6.7f, 7.0f};
for (float t : cubic_t) {
cubic.eval(t, &cv);
if (!near(cv, 5.0f)) {
std::fprintf(stderr, "cubic constant t=%f got %f\n", t, cv);
return 1;
}
}
// granny GetControlLoopState.
bool ul = false, ol = false;
gr2::control_loop_state(0, 5, false, ul, ol);
if (!ul || !ol) { std::fprintf(stderr, "loop state infinite\n"); return 1; }
gr2::control_loop_state(3, 0, false, ul, ol);
if (ul || !ol) { std::fprintf(stderr, "loop state first\n"); return 1; }
gr2::control_loop_state(3, 2, false, ul, ol);
if (!ul || ol) { std::fprintf(stderr, "loop state last\n"); return 1; }
gr2::control_loop_state(0, 0, true, ul, ol);
if (ul || ol) { std::fprintf(stderr, "loop state clamped\n"); return 1; }
// BoneTrack::sample fills SRT components and flags; sample_local = compose(sample).
gr2::BoneTrack track;
track.position.degree = 1;
track.position.dim = 3;
track.position.knots = {0.0f, 1.0f};
track.position.controls = {0.0f, 0.0f, 0.0f, 2.0f, 4.0f, 6.0f};
track.orientation.degree = 0;
track.orientation.dim = 4;
track.orientation.knots = {0.0f};
track.orientation.controls = {0.0f, 0.0f, 0.70710678f, 0.70710678f}; // 90 deg about Z
gr2::Transform x;
track.sample(0.5f, x);
if (x.flags != 3 || std::fabs(x.position[0] - 1.0f) > 1e-5f || std::fabs(x.position[2] - 3.0f) > 1e-5f) {
std::fprintf(stderr, "BoneTrack::sample flags=%u pos=%f,%f,%f\n", x.flags, x.position[0], x.position[1], x.position[2]);
return 1;
}
gr2::Animation anim;
anim.tracks.push_back(track);
std::vector<gr2::Mat4> local;
anim.sample_local(0.5f, local);
const gr2::Mat4 m = gr2::compose(x);
// Row-major composite: row 0 is the rotated X axis (0,1,0); translation in 12..14.
if (local.size() != 1 || local[0] != m || std::fabs(m[1] - 1.0f) > 1e-5f || std::fabs(m[12] - 1.0f) > 1e-5f) {
std::fprintf(stderr, "compose mismatch m[1]=%f m[12]=%f\n", m[1], m[12]);
return 1;
}
return 0;
}