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
#include "config.h"

#include "Headers/SceneAnimator.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Headers/AnimationUtils.h"

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

namespace Divide {

/// ------------------------------------------------------------------------------------------------
/// Calculates the global transformation matrix for the given internal node
void calculateBoneToWorldTransform(Bone* child) {
    child->_globalTransform = child->_localTransform;
    Bone* parent = child->_parent;
    // This will climb the nodes up along through the parents concatenating all
    // the matrices to get the Object to World transform,
    // or in this case, the Bone To World transform
    if (Config::USE_OPENGL_RENDERING) {
        while (parent) {
            child->_globalTransform =  parent->_localTransform * child->_globalTransform;
            // get the parent of the bone we are working on
            parent = parent->_parent;
        }
    } else {
        while (parent) {
            child->_globalTransform *= parent->_localTransform;
            // get the parent of the bone we are working on
            parent = parent->_parent;
        }
    }
}

SceneAnimator::SceneAnimator()
    : _skeleton(nullptr),
    _skeletonDepthCache(-1),
    _maximumAnimationFrames(0)
{
}

SceneAnimator::~SceneAnimator()
{
    release();
}

void SceneAnimator::release(bool releaseAnimations)
{
    // this should clean everything up
    _skeletonLines.clear();
    _skeletonLinesContainer.clear();
    _skeletonDepthCache = -2;
    if (releaseAnimations) {
        // clear all animations
        _animations.clear();
        _animationNameToID.clear();
        _transforms.clear();
    }
    // This node will delete all children recursively
    MemoryManager::DELETE(_skeleton);
}

bool SceneAnimator::init() {
    Console::d_printfn(Locale::get(_ID("LOAD_ANIMATIONS_BEGIN")));

    _transforms.resize(_skeletonDepthCache);

    D64 timestep = 1.0 / ANIMATION_TICKS_PER_SECOND;
    mat4<F32> rotationmat;
    vectorImplAligned<mat4<F32> > vec;
    vectorAlg::vecSize animationCount = _animations.size();
    _skeletonLines.resize(animationCount);

    // pre-calculate the animations
    for (vectorAlg::vecSize i(0); i < animationCount; ++i) {
        std::shared_ptr<AnimEvaluator> crtAnimation = _animations[i];
        D64 duration = crtAnimation->duration();
        D64 tickStep = crtAnimation->ticksPerSecond() / ANIMATION_TICKS_PER_SECOND;
        D64 dt = 0;
        for (D64 ticks = 0; ticks < duration; ticks += tickStep) {
            dt += timestep;
            calculate((I32)i, dt);
            crtAnimation->transforms().push_back(vec);
            vectorImplAligned<mat4<F32> >& trans = crtAnimation->transforms().back();
            if (Config::USE_OPENGL_RENDERING) {
                for (I32 a = 0; a < _skeletonDepthCache; ++a) {
                    Bone* bone = _bones[a];
                    AnimUtils::TransformMatrix(bone->_globalTransform * bone->_offsetMatrix, rotationmat);
                    trans.push_back(rotationmat);
                    bone->_boneID = a;
                }
            } else {
                for (I32 a = 0; a < _skeletonDepthCache; ++a) {
                    Bone* bone = _bones[a];
                    AnimUtils::TransformMatrix(bone->_offsetMatrix * bone->_globalTransform, rotationmat);
                    trans.push_back(rotationmat);
                    bone->_boneID = a;
                }
            }
        }

        _skeletonLines[i].resize(crtAnimation->frameCount(), -1);
        _maximumAnimationFrames = std::max(crtAnimation->frameCount(), _maximumAnimationFrames);
    }

    // pay the cost upfront
    for(std::shared_ptr<AnimEvaluator> crtAnimation : _animations) {
        crtAnimation->initBuffers();
    }

     Console::d_printfn(Locale::get(_ID("LOAD_ANIMATIONS_END")), _skeletonDepthCache);

    return !_transforms.empty();
}

/// This will build the skeleton based on the scene passed to it and CLEAR EVERYTHING
bool SceneAnimator::init(Bone* skeleton, const vectorImpl<Bone*>& bones) {
    release(false);
    _skeleton = skeleton;
    _bones = bones;
    _skeletonDepthCache = to_int(_bones.size());
    return init();
   
}

void SceneAnimator::registerAnimation(std::shared_ptr<AnimEvaluator> animation) {
    hashAlg::emplace(_animationNameToID, _ID_RT(animation->name()), to_uint(_animations.size()));
    _animations.push_back(animation);
}

// ------------------------------------------------------------------------------------------------
// Calculates the node transformations for the scene.
void SceneAnimator::calculate(I32 animationIndex, const D64 pTime) {
    assert(_skeleton != nullptr);

    if ((animationIndex < 0) || (animationIndex >= (I32)_animations.size())) {
        return;  // invalid animation
    }
    _animations[animationIndex]->evaluate(pTime, _skeleton);
    updateTransforms(_skeleton);
}

/// ------------------------------------------------------------------------------------------------
/// Recursively updates the internal node transformations from the given matrix
/// array
void SceneAnimator::updateTransforms(Bone* pNode) {
    calculateBoneToWorldTransform(pNode);  // update global transform as well
    /// continue for all children
    for (Bone* bone : pNode->_children) {
        updateTransforms(bone);
    }
}

Bone* SceneAnimator::boneByName(const stringImpl& bname) const {
    assert(_skeleton != nullptr);

    return _skeleton->find(bname);
}

I32 SceneAnimator::boneIndex(const stringImpl& bname) const {
    Bone* bone = boneByName(bname);

    if (bone != nullptr) {
        return bone->_boneID;
    }

    return -1;
}

/// Renders the current skeleton pose at time index dt
const vectorImpl<Line>& SceneAnimator::skeletonLines(I32 animationIndex,
                                                     const D64 dt) {
    I32 frameIndex = std::max(_animations[animationIndex]->frameIndexAt(dt) - 1, 0);
    I32& vecIndex = _skeletonLines.at(animationIndex).at(frameIndex);

    if (vecIndex == -1) {
        vecIndex = to_int(_skeletonLinesContainer.size());
        _skeletonLinesContainer.push_back(vectorImpl<Line>());
    }

    // create all the needed points
    vectorImpl<Line>& lines = _skeletonLinesContainer.at(vecIndex);
    if (lines.empty()) {
        lines.reserve(vectorAlg::vecSize(boneCount()));
        // Construct skeleton
        calculate(animationIndex, dt);
        // Start with identity transform
        createSkeleton(_skeleton, aiMatrix4x4(), lines);
    }

    return lines;
}

/// Create animation skeleton
I32 SceneAnimator::createSkeleton(Bone* piNode, const aiMatrix4x4& parent,
                                  vectorImpl<Line>& lines) {

    const aiMatrix4x4& me = piNode->_globalTransform;

    if (piNode->_parent) {
        Line line;
        line.colour(255, 0, 0, 255);
        line.width(2.0f);

        if (GFX_DEVICE.getAPI() == RenderAPI::Direct3D) {
            line.segment(parent.d1, parent.d2, parent.d3, me.d1, me.d2, me.d3);
        } else {
            line.segment(parent.a4, parent.b4, parent.c4, me.a4, me.b4, me.c4);
        }
        vectorAlg::emplace_back(lines, line);
    }

    // render all child nodes
    for (Bone* bone : piNode->_children) {
        createSkeleton(bone, me, lines);
    }

    return 1;
}
};

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

Diff revisions: vs.
Revision Author Commited Message
721 Diff Diff IonutCava picture IonutCava Wed 08 Jun, 2016 15:47:49 +0000

[IonutCava]
- Spelling: change all references from our code of “color” to the PROPER British version of “colour” because it sounds 100x better

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

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

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

541 Diff Diff IonutCava picture IonutCava Thu 29 Oct, 2015 17:03:27 +0000

[IonutCava]
- Initial work on multi-threading the main loop (experimental. unstable):
— Multi-threaded frustum culling
— Multi-threaded particle update system

524 Diff Diff IonutCava picture IonutCava Thu 08 Oct, 2015 16:14:58 +0000

[IonutCava]
- Profiler guided optimizations:
— Replace most hashMaps with vectors where the common access pattern is linear (e.g. SceneGraphNode’s child list)
— Specify ShaderBuffer access patters at creation time to properly inform the rendering API
— Fix and optimise the LockManager & BufferLockManager classes
— Add a AtomicWrapper class that allows assignment and copy-constructions for atomic variables
-— This allows us to use vector of atomic bools for BB creation for SkinnedSubMesh objects

523 Diff Diff IonutCava picture IonutCava Wed 07 Oct, 2015 16:11:44 +0000

[IonutCava]
- Performance optimizations:
— Change GFXDevice’s vector of RenderPackages to a standalone class that handles its own memory, thus avoiding the hit from clear()/push_back()
— Also minimizes memory allocation in each render pass
- Remove the extra WaitForLockedRange call in LockRange command. It was added to work around a bug that doesn’t happen any more due to better buffer usage.
- Add some vegetation generation notes for future updates

522 IonutCava picture IonutCava Tue 06 Oct, 2015 20:23:41 +0000

[Ionut]
- Move bone SSBO from the AnimationComponent to the AnimEvaluator class thus holding a single buffer per Mesh per animation instead of one per SGN per animation.
— This is made possible by the recent change that uploads the entire animation to VRAM at once
— Will also allow the upload of multiple skyboxes at once and the possibility to switch between them with a shader uniform
- Consider all Cube Maps in OpenGL as Cube Map Arrays
— Allows for cleaner OpenGL texturing code and bypasses a cube map driver bug on AMD