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
#include "Headers/AnimationController.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Headers/AnimationUtils.h"

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

#include <assimp/scene.h>

namespace Divide {

void SceneAnimator::Release(){
    // this should clean everything up
    _skeletonLines.clear();
    _skeletonLinesContainer.clear();
    // clear all animations
    _animations.clear();
    _bonesByName.clear();
    _bonesToIndex.clear();
    _animationNameToId.clear();
    _transforms.clear();
    // This node will delete all children recursivly
    MemoryManager::DELETE( _skeleton );
}

/// This will build the skeleton based on the scene passed to it and CLEAR EVERYTHING
bool SceneAnimator::Init(const aiScene* pScene, U8 meshPointer){
    if(!pScene->HasAnimations()) return false;
    Release();

    _skeleton = CreateBoneTree( pScene->mRootNode, nullptr);
    ExtractAnimations(pScene);

    const aiMesh* mesh = pScene->mMeshes[meshPointer];

    for (U32 n = 0; n < mesh->mNumBones;++n){
        const aiBone* bone = mesh->mBones[n];
        hashMapImpl<stringImpl, Bone*>::iterator found = _bonesByName.find(bone->mName.data);
        if(found != _bonesByName.end()){// FOUND IT!!! woohoo, make sure its not already in the bone list
            bool skip = false;
            for(vectorAlg::vecSize j(0); j< _bones.size(); j++){
                stringImpl bname = bone->mName.data;
                if(_bones[j]->_name == bname) {
                    skip = true;// already inserted, skip this so as not to insert the same bone multiple times
                    break;
                }
            }
            if(!skip){// only insert the bone if it has not already been inserted
                stringImpl tes = found->second->_name;
                found->second->_offsetMatrix =  bone->mOffsetMatrix;
                _bones.push_back(found->second);
                _bonesToIndex[found->first] = (U32)_bones.size()-1;
            }
        }
    }

    _transforms.resize( _bones.size());
    D32 timestep = 1.0/ANIMATION_TICKS_PER_SECOND;// ANIMATION_TICKS_PER_SECOND
    mat4<F32> rotationmat;
    vectorImpl<mat4<F32> > vec;
    for(vectorAlg::vecSize i(0); i< _animations.size(); i++){// pre calculate the animations
        D32 dt = 0;
        for(D32 ticks = 0; ticks < _animations[i]._duration; ticks += _animations[i]._ticksPerSecond/ANIMATION_TICKS_PER_SECOND){
            dt +=timestep;
            Calculate((I32)i, dt);
            _animations[i]._transforms.push_back(vec);
            vectorImpl<mat4<F32> >& trans = _animations[i]._transforms.back();
            if(GFX_DEVICE.getApi() == Direct3D){
                for( vectorAlg::vecSize a = 0; a < _transforms.size(); ++a){
                    AnimUtils::TransformMatrix(_bones[a]->_offsetMatrix * _bones[a]->_globalTransform, rotationmat);
                    trans.push_back(rotationmat);
                }
            }else{
                for( vectorAlg::vecSize a = 0; a < _transforms.size(); ++a){
                    AnimUtils::TransformMatrix(_bones[a]->_globalTransform * _bones[a]->_offsetMatrix, rotationmat);
                    trans.push_back(rotationmat);
                }
            }
        }
    }

    D_PRINT_FN(Locale::get("LOAD_ANIMATIONS_END"), _bones.size());
    return !_transforms.empty();
}

void SceneAnimator::ExtractAnimations(const aiScene* pScene){
    D_PRINT_FN(Locale::get("LOAD_ANIMATIONS_BEGIN"));
    for(size_t i(0); i< pScene->mNumAnimations; i++){
        if(pScene->mAnimations[i]->mDuration > 0.0f){
            _animations.push_back(AnimEvaluator(pScene->mAnimations[i]) );// add the animations
        }
    }
    // get all the animation names so I can reference them by name and get the correct id
    U16 i = 0;
    for(AnimEvaluator& animation : _animations){
        _animationNameToId.insert(hashMapImpl<stringImpl, U32>::value_type(animation._name, i++));
    }
}

// ------------------------------------------------------------------------------------------------
// Calculates the node transformations for the scene.
void SceneAnimator::Calculate(I32 animationIndex, const D32 pTime){
    if ((animationIndex < 0) || (animationIndex >= (I32)_animations.size())) return;// invalid animation
    _animations[animationIndex].Evaluate(pTime, _bonesByName);
    UpdateTransforms(_skeleton);
}

// ------------------------------------------------------------------------------------------------
// Recursively creates an internal node structure matching the current scene and animation.
Bone* SceneAnimator::CreateBoneTree( aiNode* pNode, Bone* parent){
    Bone* internalNode = MemoryManager_NEW Bone(pNode->mName.data);// create a node
    internalNode->_parent = parent; //set the parent, in the case this is theroot node, it will be null
    _bonesByName[internalNode->_name] = internalNode;// use the name as a key
    internalNode->_localTransform =  pNode->mTransformation;
    if(GFX_DEVICE.getApi() == Direct3D){
        internalNode->_localTransform.Transpose();
    }
    internalNode->_originalLocalTransform = internalNode->_localTransform;// a copy saved
    CalculateBoneToWorldTransform(internalNode);

    // continue for all child nodes and assign the created internal nodes as our children
    // recursively call this function on all children
    for(uint32_t i = 0; i < pNode->mNumChildren; i++){
        Bone* childNode = CreateBoneTree(pNode->mChildren[i], internalNode);
        internalNode->_children.push_back(childNode);
    }
    return internalNode;
}

/// ------------------------------------------------------------------------------------------------
/// 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::GetBoneByName(const stringImpl& bname) const {
    hashMapImpl<stringImpl, Bone*>::const_iterator found = _bonesByName.find(bname);
    if ( found != _bonesByName.end() ) {
        return found->second;
    } else {
        return nullptr;
    }
}

I32 SceneAnimator::GetBoneIndex(const stringImpl& bname) const {
    hashMapImpl<stringImpl, U32>::const_iterator found = _bonesToIndex.find(bname);
    if ( found != _bonesToIndex.end() ) {
        return found->second;
    } else {
        return -1;
    }
}

/// ------------------------------------------------------------------------------------------------
/// Calculates the global transformation matrix for the given internal node
void SceneAnimator::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(GFX_DEVICE.getApi() == Direct3D){
        while( parent ){
            child->_globalTransform *= parent->_localTransform;
            parent  = parent->_parent;// get the parent of the bone we are working on
        }
    }else{
        while( parent) {
            child->_globalTransform = parent->_localTransform * child->_globalTransform;
            parent  = parent->_parent;// get the parent of the bone we are working on
        }
    }
}

///Renders the current skeleton pose at time index dt
const vectorImpl<Line >& SceneAnimator::getSkeletonLines(I32 animationIndex, const D32 dt) {

    // typedef hashMapImpl<I32/*frameIndex*/, vectorAlg::vecSize/*vectorIntex*/> LineMap;
    // typedef hashMapImpl<I32/*animationId*/, LineMap> LineCollection;
    // LineCollection _skeletonLines;
    // vectorImpl<vectorImpl<Line >> _skeletonLinesContainer;

    I32 frameIndex = _animations[animationIndex].GetFrameIndexAt(dt);
    LineMap& lineMap = _skeletonLines[animationIndex];
    LineMap::iterator it = lineMap.find(frameIndex);

    if (it == lineMap.end()) {
        vectorAlg::vecSize lineEntry = static_cast<vectorAlg::vecSize>(_skeletonLinesContainer.size());
        it = hashAlg::insert(lineMap, hashAlg::makePairCpy(frameIndex, lineEntry)).first;

        _skeletonLinesContainer.push_back(vectorImpl<Line>());
    }

    // create all the needed points
    vectorImpl<Line >& lines = _skeletonLinesContainer[it->second];
    if (lines.empty()){
        lines.reserve(_bones.size());
        // 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){

    aiMatrix4x4 me = piNode->_globalTransform;
    if(GFX_DEVICE.getApi() == Direct3D){
        me.Transpose();
    }

    if (piNode->_parent) {
        lines.push_back(Line(vec3<F32>(parent.a4, parent.b4, parent.c4),
                             vec3<F32>(me.a4, me.b4, me.c4),
                             vec4<U8>(255,0,0,255)));
    }

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

    return 1;
}

};

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

Diff revisions: vs.
Revision Author Commited Message
337 Diff Diff IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)

334 Diff Diff IonutCava picture IonutCava Wed 10 Dec, 2014 17:21:22 +0000

[Ionut]
- Reorganized the Hardware project and renamed to "Platform"
- Reorganized the Server project

333 Diff Diff IonutCava picture IonutCava Tue 09 Dec, 2014 16:15:44 +0000

[Ionut]
- Removed “core.h” and moved everything to their appropriate header files
- Renamed MathClasses to MathMatrices (finally)
- Renamed “-Inl.h” files to ".inl"
- Some String wrapper refactoring
- Update “InstallLibraries.bat” so that it still offers valid options

331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

326 Diff Diff IonutCava picture IonutCava Tue 30 Sep, 2014 21:11:32 +0000

[Ionut]
- Fixed more memory leaks
- Simplified Task interface and timing System
- Improved compatibility between Boost, STL and EASTL with random combinations of vectors, strings and maps
- Simplified Light class by removing the “slot” member
- Random optimizations

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

324 Diff Diff IonutCava picture IonutCava Sun 14 Sep, 2014 14:01:39 +0000

[Ionut]
- New memory management system (no custom allocators in this commit)
— Memory tracer to detect memory leaks on shutdown (from: https://www.relisoft.com/book/tech/9new.html)
— SAFE_DELETE_XYZ are now inline template functions not macros
— ASSERTION system now supports logging and message boxes and continue on error configurable for each build type
— Fixed a lot of memory leaks detected by the new system (some still remain)

- Added SRGB support (part 1)
— Albedo textures are loaded as SRGB (make sure resources are SRGB)
— Shaders output gamma corrected colors
— Moved terrain textures back to 2 array system (albedo and normal maps, albedo maps using a SRGB sampler)

- Sky is now loaded via the ResourceCache system
- Added include safeguards in shader atoms

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

319 IonutCava picture IonutCava Sat 30 Aug, 2014 21:37:57 +0000

[Ionut]
- Performance tweaks based on profiling data