Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "config.h"

#include "Headers/AnimationEvaluator.h"
#include "Headers/AnimationUtils.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/Buffers/ShaderBuffer/Headers/ShaderBuffer.h"

#include "Core/Headers/Console.h"
#include "Utility/Headers/Localization.h"

namespace Divide {

AnimEvaluator::AnimEvaluator() : _lastTime(0.0),
                                 _ticksPerSecond(0.0),
                                 _duration(0.0),
                                 _playAnimationForward(true)
{
}

// ------------------------------------------------------------------------------------------------
// Constructor on a given animation.
AnimEvaluator::AnimEvaluator(const aiAnimation* pAnim, U32 idx) : AnimEvaluator() {
    _lastTime = 0.0;
    _ticksPerSecond = !IS_ZERO(pAnim->mTicksPerSecond)
                          ? pAnim->mTicksPerSecond
                          : ANIMATION_TICKS_PER_SECOND;
    _duration = pAnim->mDuration;
    _name = pAnim->mName.data;
    if (_name.empty()) {
        _name = Util::StringFormat("unnamed_anim_%d", idx);
    }
    Console::d_printfn(Locale::get(_ID("CREATE_ANIMATION_BEGIN")), _name.c_str());

    _channels.resize(pAnim->mNumChannels);
    for (U32 a = 0; a < pAnim->mNumChannels; a++) {
        aiNodeAnim* srcChannel = pAnim->mChannels[a];
        AnimationChannel& dstChannel = _channels[a];

        dstChannel._name = srcChannel->mNodeName.data;
        dstChannel._nameKey = _ID_RT(dstChannel._name);

        for (U32 i(0); i < srcChannel->mNumPositionKeys; i++) {
            dstChannel._positionKeys.push_back(srcChannel->mPositionKeys[i]);
        }
        for (U32 i(0); i < srcChannel->mNumRotationKeys; i++) {
            dstChannel._rotationKeys.push_back(srcChannel->mRotationKeys[i]);
        }
        for (U32 i(0); i < srcChannel->mNumScalingKeys; i++) {
            dstChannel._scalingKeys.push_back(srcChannel->mScalingKeys[i]);
        }
        dstChannel._numPositionKeys = srcChannel->mNumPositionKeys;
        dstChannel._numRotationKeys = srcChannel->mNumRotationKeys;
        dstChannel._numScalingKeys = srcChannel->mNumScalingKeys;
    }

    _lastPositions.resize(pAnim->mNumChannels, vec3<U32>());

    Console::d_printfn(Locale::get(_ID("CREATE_ANIMATION_END")), _name.c_str());
}

AnimEvaluator::~AnimEvaluator()
{
}

bool AnimEvaluator::initBuffers() {
    DIVIDE_ASSERT(_boneTransformBuffer.get() == nullptr && !_transforms.empty(),
                  "AnimEvaluator error: can't create bone buffer at current stage!");

    _boneTransformBuffer.reset(GFX_DEVICE.newSB(1, true, false, BufferUpdateFrequency::ONCE));

    DIVIDE_ASSERT(_transforms.size() <= Config::MAX_BONE_COUNT_PER_NODE,
        "AnimEvaluator error: Too many bones for current node! "
        "Increase MAX_BONE_COUNT_PER_NODE in Config!");
    size_t paddingFactor = _boneTransformBuffer->getAlignmentRequirement();

    U32 bonePadding = 0;
    size_t boneCount = _transforms.front().size();
    size_t bufferSize = sizeof(mat4<F32>) * boneCount;
    while (bufferSize % paddingFactor != 0) {
        bufferSize += sizeof(mat4<F32>);
        bonePadding++;
    }

    _boneTransformBuffer->create(frameCount(), bufferSize);

    vectorImplAligned<mat4<F32>> animationData;
    animationData.reserve((boneCount + bonePadding) * frameCount());

    for (const vectorImplAligned<mat4<F32>>& frameTransforms : _transforms) {
        animationData.insert(std::cend(animationData), std::cbegin(frameTransforms), std::cend(frameTransforms));
        for (U32 i = 0; i < bonePadding; ++i) {
            animationData.push_back(mat4<F32>());
        }
    }

    vectorAlg::shrinkToFit(animationData);

    _boneTransformBuffer->setData((const bufferPtr)animationData.data());

    return !animationData.empty();
}

I32 AnimEvaluator::frameIndexAt(const D64 elapsedTime) const {
    D64 time = 0.0f;
    if (_duration > 0.0) {
        // get a [0.f ... 1.f) value by allowing the percent to wrap around 1
        time = fmod(elapsedTime * _ticksPerSecond, _duration);
    }

    D64 percent = time / _duration;

    // this will invert the percent so the animation plays backwards
    if (!_playAnimationForward) {
        percent = (percent - 1.0) * -1.0;
    }

    return std::min(to_int(_transforms.size() * percent),
                    to_int(_transforms.size() - 1));
}

// ------------------------------------------------------------------------------------------------
// Evaluates the animation tracks for a given time stamp.
void AnimEvaluator::evaluate(const D64 dt, Bone* skeleton) {
    D64 pTime = dt * _ticksPerSecond;

    D64 time = 0.0f;
    if (_duration > 0.0) {
        time = fmod(pTime, _duration);
    }

    frameIndexAt(pTime);
    aiVector3D presentPosition(0, 0, 0);
    aiQuaternion presentRotation(1, 0, 0, 0);
    aiQuaternion presentRotationDefault(1, 0, 0, 0);
    aiVector3D presentScaling(1, 1, 1);
    bool transposeMat = GFX_DEVICE.getAPI() == RenderAPI::Direct3D;
    // calculate the transformations for each animation channel
    for (U32 a = 0; a < _channels.size(); a++) {
        
        const AnimationChannel* channel = &_channels[a];
        Bone* bonenode = skeleton->find(channel->_nameKey);

        if (bonenode == nullptr) {
            Console::d_errorfn(Locale::get(_ID("ERROR_BONE_FIND")),
                               channel->_name.c_str());
            continue;
        }

        // ******** Position *****
        if (channel->_positionKeys.size() > 0) {
            // Look for present frame number. Search from last position if time
            // is after the last time, else from beginning
            // Should be much quicker than always looking from start for the
            // average use case.
            U32 frame = (time >= _lastTime) ? _lastPositions[a].x : 0;
            while (frame < channel->_positionKeys.size() - 1) {
                if (time < channel->_positionKeys[frame + 1].mTime) {
                    break;
                }
                frame++;
            }

            // interpolate between this frame's value and next frame's value
            U32 nextFrame = (frame + 1) % channel->_positionKeys.size();

            const aiVectorKey& key = channel->_positionKeys[frame];
            const aiVectorKey& nextKey = channel->_positionKeys[nextFrame];
            D64 diffTime = nextKey.mTime - key.mTime;
            if (diffTime < 0.0) diffTime += _duration;
            if (diffTime > 0) {
                F32 factor = F32((time - key.mTime) / diffTime);
                presentPosition =
                    key.mValue + (nextKey.mValue - key.mValue) * factor;
            } else {
                presentPosition = key.mValue;
            }
            _lastPositions[a].x = frame;
        } else {
            presentPosition.Set(0.0f, 0.0f, 0.0f);
        }

        // ******** Rotation *********
        if (channel->_rotationKeys.size() > 0) {
            U32 frame = (time >= _lastTime) ? _lastPositions[a].y : 0;
            while (frame < channel->_rotationKeys.size() - 1) {
                if (time < channel->_rotationKeys[frame + 1].mTime) break;
                frame++;
            }

            // interpolate between this frame's value and next frame's value
            U32 nextFrame = (frame + 1) % channel->_rotationKeys.size();

            const aiQuatKey& key = channel->_rotationKeys[frame];
            const aiQuatKey& nextKey = channel->_rotationKeys[nextFrame];
            D64 diffTime = nextKey.mTime - key.mTime;
            if (diffTime < 0.0) diffTime += _duration;
            if (diffTime > 0) {
                F32 factor = F32((time - key.mTime) / diffTime);
                presentRotation = presentRotationDefault;
                aiQuaternion::Interpolate(presentRotation, key.mValue,
                                          nextKey.mValue, factor);
            } else {
                presentRotation = key.mValue;
            }
            _lastPositions[a].y = frame;
        } else {
            presentRotation = presentRotationDefault;
        }

        // ******** Scaling **********
        if (channel->_scalingKeys.size() > 0) {
            U32 frame = (time >= _lastTime) ? _lastPositions[a].z : 0;
            while (frame < channel->_scalingKeys.size() - 1) {
                if (time < channel->_scalingKeys[frame + 1].mTime) break;
                frame++;
            }

            presentScaling = channel->_scalingKeys[frame].mValue;
            _lastPositions[a].z = frame;
        } else {
            presentScaling.Set(1.0f, 1.0f, 1.0f);
        }

        aiMatrix4x4& mat = bonenode->_localTransform;
        mat = aiMatrix4x4(presentRotation.GetMatrix());
        mat.a1 *= presentScaling.x;
        mat.b1 *= presentScaling.x;
        mat.c1 *= presentScaling.x;
        mat.a2 *= presentScaling.y;
        mat.b2 *= presentScaling.y;
        mat.c2 *= presentScaling.y;
        mat.a3 *= presentScaling.z;
        mat.b3 *= presentScaling.z;
        mat.c3 *= presentScaling.z;
        mat.a4  = presentPosition.x;
        mat.b4  = presentPosition.y;
        mat.c4  = presentPosition.z;
        if (transposeMat) {
            mat.Transpose();
        }
    }
    _lastTime = time;
}
};

Commits for Divide-Framework/trunk/Source Code/Geometry/Animations/AnimationEvaluator.cpp

Diff revisions: vs.
Revision Author Commited Message
727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

712 Diff Diff IonutCava picture IonutCava Tue 24 May, 2016 16:18:37 +0000

[IonutCava]
- Added the concept of “buffer” to be used by GenericVertexData and ShaderBuffer without caring if it’s persistent or not.
— Persistent buffers handle their own locking instead of relying on the parent class
- HiZ occlusion culling fixes and optimizations

680 Diff Diff IonutCava picture IonutCava Thu 14 Apr, 2016 16:15:38 +0000

[IonutCava]
- Added a mat2 class (untested)
- Added static asserts to matrices, vectors and quaternions to allow only arithmetic (sans bool) data types
- Added more vector unit tests
- Added unit tests for matrices
- D32 finally renamed to D64 as the VC++ compiler is considered as the reference compiler

674 Diff Diff IonutCava picture IonutCava Mon 11 Apr, 2016 16:17:10 +0000

[IonutCava]
- Some profile guided optimizations:
— Remove the task state hashmap and replace with flat array
— Allow PhysX to load collision meshes from a RAM cache first -> then file -> then recompute
- Improve memory logger output

671 Diff Diff IonutCava picture IonutCava Fri 08 Apr, 2016 16:26:49 +0000

[Ionut]
- Custom memory allocation system part 1:
— Add http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Pe
— Add custom allocators to strings and vectors
— Add custom allocators to a few test classes
— No support for aligned memory yet, so vec4<F32> and mat4<F32> vectors use the old allocation system
- Replace more std::async call with our custom task system

654 Diff Diff IonutCava picture IonutCava Tue 01 Mar, 2016 17:22:09 +0000

[Ionut]
- Moved window related code from rendering API code into proper classes:
— DisplayWindow handles SDL based window management
— WindowManager handles multiple DisplayWindow instances
— Code still needs proper extensive testing
- Small performance optimizations

628 Diff Diff IonutCava picture IonutCava Wed 20 Jan, 2016 17:17:53 +0000

[IonutCava]
- Reworked GenericVertexData ring-buffering system and changed vertex attributes to the vertex format system
— Might be buggy
- Disabled bump-mapping for low-LoD level entities
- Removed a forgotten test line in BRDF shader that prevented lighting calculations to occur properly (an early return)
- Updated auto-reflection system for high shininess materials
- Converted a lot of ‘to_xyz’ calls to ‘to_const_xyz’ calls where appropriate to reduce runtime cost (‘to_const_xyz’ is evaluated at compile time)

591 Diff Diff IonutCava picture IonutCava Wed 02 Dec, 2015 17:17:23 +0000

[IonutCava]
- More work on the ForwardPlus renderer
- Singleton cleanup
- First steps for reducing scope of GFXDevice class

577 Diff Diff IonutCava picture IonutCava Fri 20 Nov, 2015 15:59:10 +0000

[IonutCava]
- Replace all string indexed maps with unsigned long long indexed maps and add both compile time and runtime hashing functions to convert strings to the proper indices
— Balancing the calls between _ID and _ID_RT can reduce map lookup overhead, especially with ParamHandler and Locale::get

528 IonutCava picture IonutCava Mon 19 Oct, 2015 15:53:19 +0000

[IonutCava]
- Fix occlusion culling
- Fix blur shader
- Fix particle rendering
- Fix ShaderBuffer class function naming