File: GLTFAnimation.cpp

package info (click to toggle)
collada2gltf 20140924-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,672 kB
  • sloc: cpp: 33,757; ansic: 5,313; python: 29; makefile: 6
file content (191 lines) | stat: -rw-r--r-- 7,195 bytes parent folder | download | duplicates (3)
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
// Copyright (c) Fabrice Robinet
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "GLTF.h"
#include "GLTFAsset.h"
#include "animationConverter.h"
#include "GLTF-Open3DGC.h" //FIXME: setupAndWriteAnimationParameter has to move out from this header

using namespace rapidjson;
using namespace rapidjson;
#if __cplusplus <= 199711L
using namespace std::tr1;
#endif
using namespace std;

namespace GLTF 
{
    //-- GLTFAnimation
    
    GLTFAnimation::GLTFAnimation() : JSONObject() {
        this->createObjectIfNeeded(kSamplers);
        this->createArrayIfNeeded(kChannels);
        this->createObjectIfNeeded(kParameters);
        
        this->_targets = shared_ptr<JSONObject> (new JSONObject());
    }

    GLTFAnimation::~GLTFAnimation() {}

    shared_ptr <JSONObject> GLTFAnimation::targets() {
        return this->_targets;
    }

    size_t GLTFAnimation::getCount() {
        return this->getUnsignedInt32(kCount);
    }
    
    void GLTFAnimation::setCount(size_t count) {
		this->setUnsignedInt32(kCount, (unsigned int)count);
    }
     
    shared_ptr <JSONObject> GLTFAnimation::parameters() {
        return this->getObject(kParameters);
    }

    shared_ptr <JSONObject>  GLTFAnimation::getParameterNamed(std::string parameter) {
        return this->parameters()->getObject(parameter);
    }

    void GLTFAnimation::removeParameterNamed(std::string parameter) {
        this->parameters()->removeValue(parameter);
    }

    void GLTFAnimation::setID(std::string animationID) {
        this->_id = animationID;
    }
    
    std::string GLTFAnimation::getID() {
        return this->_id;
    }
    
    shared_ptr <JSONObject> GLTFAnimation::samplers() {
        return this->getObject(kSamplers);
    }

    shared_ptr <JSONArray> GLTFAnimation::channels() {
        return this->getArray(kChannels);
    }

    std::string GLTFAnimation::getSamplerIDForName(std::string name) {
        return this->_id + "_" + name + "_sampler";
    }
    
    shared_ptr<GLTFAnimationFlattener> GLTFAnimation::animationFlattenerForTargetUID(std::string targetUID) {
        
        return (*this->_animationFlattenerForTargetUID)[targetUID];
    }

    void GLTFAnimation::setOriginalID(std::string originalID) {
        this->_originalID = originalID;
    }
    
    std::string GLTFAnimation::getOriginalID() {
        return this->_originalID;
    }
    
    void GLTFAnimation::registerAnimationFlatteners(AnimationFlattenerForTargetUIDSharedPtr animationFlattenerMaps) {
        this->_animationFlattenerForTargetUID = animationFlattenerMaps;
    }

    void GLTFAnimation::registerBufferView(std::string parameterName, shared_ptr <GLTFBufferView> bufferView) {
        this->_bufferViews[parameterName] = bufferView;
    }

    void GLTFAnimation::unregisterBufferView(std::string parameterName) {
        this->_bufferViews.erase(parameterName);
    }

    shared_ptr<GLTFBufferView> GLTFAnimation::getBufferViewForParameter(std::string parameterName) {
        return this->_bufferViews[parameterName];
    }

    /*
     Handles Channel creation + additions
     */
    static void __AddChannel(GLTFAnimation* cvtAnimation,
                             const std::string &targetID,
                             const std::string &path) {
        shared_ptr<JSONObject> trChannel(new JSONObject());
        shared_ptr<JSONObject> trTarget(new JSONObject());
        
        trChannel->setString("sampler", cvtAnimation->getSamplerIDForName(path));
        trChannel->setValue(kTarget, trTarget);
        trTarget->setString("id", targetID);
        trTarget->setString("path", path);
        cvtAnimation->channels()->appendValue(trChannel);
    }

    void GLTFAnimation::writeAnimationForTargetID(const std::string &targetID, GLTFAsset* asset) {
        shared_ptr <JSONObject> target =  this->targets()->getObject(targetID);
        shared_ptr<GLTFAnimationFlattener> animationFlattener = this->animationFlattenerForTargetUID(targetID);
        
        size_t count = 0;
        float* rotations = 0;
        float* positions = 0;
        float* scales = 0;
        
        animationFlattener->allocAndFillAffineTransformsBuffers(&positions, &rotations, &scales, count);
        
        if (animationFlattener->hasAnimatedScale()) {
            //Scale
            setupAndWriteAnimationParameter(this,
                                            "scale",
                                            "FLOAT_VEC3",
                                            (unsigned char*)scales,
                                            count * sizeof(float) * 3, false,
                                            asset);
            __AddChannel(this, targetID, "scale");
            free(scales);
        }
        
        if (animationFlattener->hasAnimatedTranslation()) {
            //Translation
            setupAndWriteAnimationParameter(this,
                                            "translation",
                                            "FLOAT_VEC3",
                                            (unsigned char*)positions,
                                            count * sizeof(float) * 3, false,
                                            asset);
            __AddChannel(this, targetID, "translation");
            free(positions);
        }
        
        if (animationFlattener->hasAnimatedRotation()) {
            //Rotation
            setupAndWriteAnimationParameter(this,
                                            "rotation",
                                            "FLOAT_VEC4",
                                            (unsigned char*)rotations,
                                            count * sizeof(float) * 4, false,
                                            asset);
            __AddChannel(this, targetID, "rotation");
            free(rotations);
        }
    }
    
    std::string GLTFAnimation::valueType() {
        return "animation";
    }

}