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
/*
   Copyright (c) 2016 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

/*Code references:
    http://nolimitsdesigns.com/game-design/open-asset-import-library-animation-loader/
*/

#ifndef SCENE_ANIMATOR_H_
#define SCENE_ANIMATOR_H_

#include "AnimationEvaluator.h"
#include "Core/Math/Headers/Line.h"

struct aiMesh;
struct aiNode;
struct aiScene;

namespace Divide {

/// Calculates the global transformation matrix for the given internal node
void calculateBoneToWorldTransform(Bone* pInternalNode);
class ByteBuffer;
class SceneAnimator {
   public:
    // index = frameIndex; entry = vectorIndex;
    typedef vectorImpl<I32> LineMap;
    // index = animationID;
    typedef vectorImpl<LineMap> LineCollection;

    SceneAnimator();
    ~SceneAnimator();

    /// This must be called to fill the SceneAnimator with valid data
    bool init(Bone* skeleton, const vectorImpl<Bone*>& bones);
    /// Frees all memory and initializes everything to a default state
    void release(bool releaseAnimations = true);
    void save(ByteBuffer& dataOut) const;
    void load(ByteBuffer& dataIn);
    /// Lets the caller know if there is a skeleton present
    inline bool hasSkeleton() const { return _skeleton != nullptr; }
    /// The next two functions are good if you want to change the direction of
    /// the current animation.
    /// You could use a forward walking animation and reverse it to get a
    /// walking backwards
    inline void playAnimationForward(I32 animationIndex) {
        _animations[animationIndex]->playAnimationForward(true);
    }
    inline void playAnimationBackward(I32 animationIndex) {
        _animations[animationIndex]->playAnimationForward(false);
    }
    /// This function will adjust the current animations speed by a percentage.
    /// So, passing 100, would do nothing, passing 50, would decrease the speed
    /// by half, and 150 increase it by 50%
    inline void adjustAnimationSpeedBy(I32 animationIndex, const D64 percent) {
        std::shared_ptr<AnimEvaluator> animation = _animations.at(animationIndex);
        animation->ticksPerSecond(animation->ticksPerSecond() * (percent / 100.0));
    }
    /// This will set the animation speed
    inline void adjustAnimationSpeedTo(I32 animationIndex,
                                       const D64 tickspersecond) {
        _animations[animationIndex]->ticksPerSecond(tickspersecond);
    }
    /// Get the animationspeed... in ticks per second
    inline D64 animationSpeed(I32 animationIndex) const {
        return _animations[animationIndex]->ticksPerSecond();
    }

    /// Get the transforms needed to pass to the vertex shader.
    /// This will wrap the dt value passed, so it is safe to pass 50000000 as a
    /// valid number
    inline I32 frameIndexForTimeStamp(I32 animationIndex, const D64 dt) const {
        return _animations[animationIndex]->frameIndexAt(dt);
    }

    inline const vectorImplAligned<mat4<F32>>& transforms(I32 animationIndex, U32 index) const {
        return _animations[animationIndex]->transforms(index);
    }

    inline const AnimEvaluator& animationByIndex(I32 animationIndex) const {
        assert(IS_IN_RANGE_INCLUSIVE(animationIndex, 0, to_int(_animations.size()) - 1));
        const std::shared_ptr<AnimEvaluator>& animation = _animations.at(animationIndex);
        assert(animation != nullptr);
        return *animation;
    }

    inline AnimEvaluator& animationByIndex(I32 animationIndex) {
        assert(IS_IN_RANGE_INCLUSIVE(animationIndex, 0, to_int(_animations.size()) - 1));
        const std::shared_ptr<AnimEvaluator>& animation = _animations.at(animationIndex);
        assert(animation != nullptr);
        return *animation;
    }

    inline U32 frameCount(I32 animationIndex) const {
        return _animations[animationIndex]->frameCount();
    }

    inline const vectorImpl<std::shared_ptr<AnimEvaluator>>& animations() const {
        return _animations;
    }

    inline const stringImpl& animationName(I32 animationIndex) const {
        return _animations[animationIndex]->name();
    }

    inline bool animationID(const stringImpl& animationName, U32& ID) {
        hashMapImpl<ULL, U32>::iterator itr =
            _animationNameToID.find(_ID_RT(animationName));
        if (itr != std::end(_animationNameToID)) {
            ID = itr->second;
            return true;
        }
        return false;
    }
    /// GetBoneTransform will return the matrix of the bone given its name and
    /// the time.
    /// Be careful with this to make sure and send the correct dt. If the dt is
    /// different from what the model is currently at,
    /// the transform will be off
    inline const mat4<F32>& boneTransform(I32 animationIndex, const D64 dt,
                                          const stringImpl& bname) {
        I32 boneID = boneIndex(bname);
        if (boneID != -1) {
            return boneTransform(animationIndex, dt, boneID);
        }

        _boneTransformCache.identity();
        return _boneTransformCache;
    }

    /// Same as above, except takes the index
    inline const mat4<F32>& boneTransform(I32 animationIndex, const D64 dt,
                                          I32 bindex) {
        if (bindex != -1) {
            return _animations[animationIndex]->transforms(dt).at(bindex);
        }

        _boneTransformCache.identity();
        return _boneTransformCache;
    }

    /// Get the bone's global transform
    inline const mat4<F32>& boneOffsetTransform(const stringImpl& bname) {
        Bone* bone = boneByName(bname);
        if (bone != nullptr) {
            AnimUtils::TransformMatrix(bone->_offsetMatrix, _boneTransformCache);
        }
        return _boneTransformCache;
    }

    Bone* boneByName(const stringImpl& name) const;
    /// GetBoneIndex will return the index of the bone given its name.
    /// The index can be used to index directly into the vector returned from
    /// GetTransform
    I32 boneIndex(const stringImpl& bname) const;
    const vectorImpl<Line>& skeletonLines(I32 animationIndex, const D64 dt);

    /// Returns the frame count of the longest registered animation
    inline U32 getMaxAnimationFrames() const {
        return _maximumAnimationFrames;
    }

    inline size_t boneCount() const {
        return _skeletonDepthCache;
    }

    void registerAnimation(std::shared_ptr<AnimEvaluator> animation);

   private:
    bool init();
    /// I/O operations
    void saveSkeleton(ByteBuffer& dataOut, Bone* pNode) const;
    Bone* loadSkeleton(ByteBuffer& dataIn, Bone* pNode);

    void updateTransforms(Bone* pNode);
    void calculate(I32 animationIndex, const D64 pTime);
    I32 createSkeleton(Bone* piNode, const aiMatrix4x4& parent,
                       vectorImpl<Line>& lines);

   private:
    /// Frame count of the longest registered animation
    U32 _maximumAnimationFrames;
    /// Root node of the internal scene structure
    Bone* _skeleton;
    I32   _skeletonDepthCache;
    vectorImpl<Bone*> _bones;
    /// A vector that holds each animation
    vectorImpl<std::shared_ptr<AnimEvaluator>> _animations;
    /// find animations quickly
    hashMapImpl<ULL, U32> _animationNameToID;
    /// temp array of transforms
    vectorImpl<aiMatrix4x4> _transforms;
    mat4<F32> _boneTransformCache;
    LineCollection _skeletonLines;
    vectorImpl<vectorImpl<Line>> _skeletonLinesContainer;
};

};  // namespace Divide

#endif // SCENE_ANIMATOR_H_

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

Diff revisions: vs.
Revision Author Commited Message
750 Diff Diff IonutCava picture IonutCava Thu 07 Jul, 2016 16:02:03 +0000

[IonutCava]
- Static analysis based fixes and improvements

684 Diff Diff IonutCava picture IonutCava Wed 20 Apr, 2016 16:27:05 +0000

[IonutCava]
- More math template code cleanup

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

648 Diff Diff IonutCava picture IonutCava Sun 21 Feb, 2016 16:32:52 +0000

[IonutCava]
- Update copyright notice
- Move BoundingBox and BoundingSphere to a new BoundsComponent
— Add a temp hack in SceneGraphNode to update these

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)

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

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