summaryrefslogtreecommitdiff
path: root/indra/newview/gltf/animation.h
blob: ab8839470a3d46eca4cda36d29c8a68f2f0230ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#pragma once

/**
 * @file animation.h
 * @brief LL GLTF Animation Implementation
 *
 * $LicenseInfo:firstyear=2024&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2024, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "accessor.h"
// LL GLTF Implementation
namespace LL
{
    namespace GLTF
    {
        class Asset;

        class Animation
        {
        public:
            class Sampler
            {
            public:
                std::vector<F32> mFrameTimes;

                F32 mMinTime = -FLT_MAX;
                F32 mMaxTime = FLT_MAX;

                S32 mInput = INVALID_INDEX;
                S32 mOutput = INVALID_INDEX;
                std::string mInterpolation;

                F32 mLastFrameTime = 0.f;
                U32 mLastFrameIndex = 0;

                bool prep(Asset& asset);

                void serialize(boost::json::object& dst) const;
                const Sampler& operator=(const Value& value);

                // get the frame index and time for the specified time
                // asset -- the asset to reference for Accessors
                // time -- the animation time to get the frame info for
                // frameIndex -- index of the closest frame that precedes the specified time
                // t - interpolant value between the frameIndex and the next frame
                void getFrameInfo(Asset& asset, F32 time, U32& frameIndex, F32& t);
            };

            class Channel
            {
            public:
                class Target
                {
                public:
                    S32 mNode = INVALID_INDEX;
                    std::string mPath;

                    bool operator==(const Target& other) const;
                    bool operator!=(const Target& other) const;

                    void serialize(boost::json::object& dst) const;
                    const Target& operator=(const Value& value);
                };

                S32 mSampler = INVALID_INDEX;
                Target mTarget;

                void serialize(boost::json::object& dst) const;
                const Channel& operator=(const Value& value);
            };

            class RotationChannel : public Channel
            {
            public:
                RotationChannel() = default;
                RotationChannel(const Channel& channel) : Channel(channel) {}

                std::vector<quat> mRotations;

                // prepare data needed for rendering
                // asset -- asset to reference for Accessors
                // sampler -- Sampler associated with this channel
                bool prep(Asset& asset, Sampler& sampler);

                void apply(Asset& asset, Sampler& sampler, F32 time);
            };

            class TranslationChannel : public Channel
            {
            public:
                TranslationChannel() = default;
                TranslationChannel(const Channel& channel) : Channel(channel) {}

                std::vector<vec3> mTranslations;

                // prepare data needed for rendering
                // asset -- asset to reference for Accessors
                // sampler -- Sampler associated with this channel
                bool prep(Asset& asset, Sampler& sampler);

                void apply(Asset& asset, Sampler& sampler, F32 time);
            };

            class ScaleChannel : public Channel
            {
            public:
                ScaleChannel() = default;
                ScaleChannel(const Channel& channel) : Channel(channel) {}

                std::vector<vec3> mScales;

                // prepare data needed for rendering
                // asset -- asset to reference for Accessors
                // sampler -- Sampler associated with this channel
                bool prep(Asset& asset, Sampler& sampler);

                void apply(Asset& asset, Sampler& sampler, F32 time);
            };

            std::string mName;
            std::vector<Sampler> mSamplers;

            // min/max time values for all samplers combined
            F32 mMinTime = 0.f;
            F32 mMaxTime = 0.f;

            // current time of the animation
            F32 mTime = 0.f;

            std::vector<RotationChannel> mRotationChannels;
            std::vector<TranslationChannel> mTranslationChannels;
            std::vector<ScaleChannel> mScaleChannels;

            void serialize(boost::json::object& dst) const;
            const Animation& operator=(const Value& value);

            bool prep(Asset& asset);

            void update(Asset& asset, float dt);

            // apply this animation at the specified time
            void apply(Asset& asset, F32 time);
        };

    }
}