コード例 #1
0
ファイル: GLTFAnimation.cpp プロジェクト: Ezio47/glTF
 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);
     }
 }
コード例 #2
0
 bool writeAnimation(shared_ptr <GLTFAnimation> cvtAnimation,
                     const COLLADAFW::AnimationList::AnimationClass animationClass,
                     AnimatedTargetsSharedPtr animatedTargets,
                     std::ofstream &animationsOutputStream,
                     GLTF::GLTFConverterContext &converterContext) {
     
     
     
     std::string samplerID;
     std::string name;
     shared_ptr<JSONObject> samplers = cvtAnimation->samplers();
     shared_ptr<JSONArray> channels = cvtAnimation->channels();
     size_t keyCount = cvtAnimation->getCount();
     GLTFAnimation::Parameter *timeParameter = cvtAnimation->getParameterNamed("TIME");
     if (timeParameter) {
         shared_ptr<GLTFBufferView> timeBufferView = timeParameter->getBufferView();
         std::string name = "TIME";
         std::string samplerID = cvtAnimation->getSamplerIDForName(name);
         
         timeParameter->setByteOffset(static_cast<size_t>(animationsOutputStream.tellp()));
         animationsOutputStream.write((const char*)( timeBufferView->getBufferDataByApplyingOffset()),
                                      timeBufferView->getByteLength());
         
         //printf("time bufferLength: %d\n",(int)timeBufferView->getByteLength());
     }
     
     switch (animationClass) {
         case COLLADAFW::AnimationList::TIME:
         {
             //In Currrent COLLADA Implementation, this is never called, only cases mapping to OUTPUT are, so we handle INPUT when we enter this function.
         }
             break;
         case COLLADAFW::AnimationList::AXISANGLE:
             break;
         case COLLADAFW::AnimationList::MATRIX4X4: {
             GLTFAnimation::Parameter *parameter = cvtAnimation->getParameterNamed("OUTPUT");
             if (parameter) {
                 std::vector< shared_ptr <GLTFBufferView> > TRSBufferViews;
                 //FIXME: we assume float here, might be double
                 shared_ptr<GLTFBufferView> bufferView = parameter->getBufferView();
                 float* matrices = (float*)bufferView->getBufferDataByApplyingOffset();
                 __DecomposeMatrices(matrices, cvtAnimation->getCount(), TRSBufferViews);
                 cvtAnimation->removeParameterNamed("OUTPUT");
                 
                 //Translation
                 __SetupAndWriteAnimationParameter(cvtAnimation,
                                                   "translation",
                                                   "FLOAT_VEC3",
                                                   TRSBufferViews[0],
                                                   animationsOutputStream);
                 
                 //Rotation
                 __SetupAndWriteAnimationParameter(cvtAnimation,
                                                   "rotation",
                                                   "FLOAT_VEC4",
                                                   TRSBufferViews[1],
                                                   animationsOutputStream);
                 
                 //Scale
                 __SetupAndWriteAnimationParameter(cvtAnimation,
                                                   "scale",
                                                   "FLOAT_VEC3",
                                                   TRSBufferViews[2],
                                                   animationsOutputStream);
                 
                 for (size_t animatedTargetIndex = 0 ; animatedTargetIndex < animatedTargets->size() ; animatedTargetIndex++) {
                     shared_ptr<JSONObject> animatedTarget = (*animatedTargets)[animatedTargetIndex];
                     if (animatedTarget->getString("path") == "MATRIX") {
                         std::string targetID = animatedTarget->getString("target");
                         __AddChannel(cvtAnimation, targetID, "translation");
                         __AddChannel(cvtAnimation, targetID, "rotation");
                         __AddChannel(cvtAnimation, targetID, "scale");
                     }
                 }
                 
             } else {
                 //FIXME: report error
                 printf("WARNING: cannot find intermediate parameter named OUTPUT\n");
             }
         }
             return true;
             break;
         case COLLADAFW::AnimationList::POSITION_XYZ: {
             GLTFAnimation::Parameter *parameter = cvtAnimation->getParameterNamed("OUTPUT");
             if (parameter) {
                 shared_ptr<GLTFBufferView> bufferView = parameter->getBufferView();
                 
                 __SetupAndWriteAnimationParameter(cvtAnimation,
                                                   "translation",
                                                   "FLOAT_VEC3",
                                                   bufferView,
                                                   animationsOutputStream);
                 
                 for (size_t animatedTargetIndex = 0 ; animatedTargetIndex < animatedTargets->size() ; animatedTargetIndex++) {
                     shared_ptr<JSONObject> animatedTarget = (*animatedTargets)[animatedTargetIndex];
                     
                     if (animatedTarget->getString("path") == "translation") {
                         std::string targetID = animatedTarget->getString("target");
                         __AddChannel(cvtAnimation, targetID, "translation");
                     }
                 }
                 cvtAnimation->removeParameterNamed("OUTPUT");
             }
         }
             
             return true;
             break;
         case COLLADAFW::AnimationList::ANGLE: {
             GLTFAnimation::Parameter *parameter = cvtAnimation->getParameterNamed("OUTPUT");
             if (parameter) {
                 shared_ptr<GLTFBufferView> bufferView = parameter->getBufferView();
                 //Convert angles to radians
                 float *angles = (float*)bufferView->getBufferDataByApplyingOffset();
                 for (size_t i = 0 ; i < keyCount ; i++) {
                     angles[i] = angles[i] * 0.0174532925; //to radians.
                 }
                 
                 for (size_t animatedTargetIndex = 0 ; animatedTargetIndex < animatedTargets->size() ; animatedTargetIndex++) {
                     shared_ptr<JSONObject> animatedTarget = (*animatedTargets)[animatedTargetIndex];
                     std::string targetID = animatedTarget->getString("target");
                     
                     if (converterContext._uniqueIDToTrackedObject.count(targetID) != 0) {
                         shared_ptr<JSONObject> targetObject = converterContext._uniqueIDToTrackedObject[targetID];
                         std::string path = animatedTarget->getString("path");
                         if (path == "rotation") {
                             shared_ptr<JSONArray> rotationArray = static_pointer_cast <JSONArray>(targetObject->getValue(path));
                             shared_ptr<GLTFBufferView> adjustedBuffer = __CreateBufferViewByReplicatingArrayAndReplacingValueAtIndex(bufferView, rotationArray, 3, "FLOAT", cvtAnimation->getCount());
                             
                             __SetupAndWriteAnimationParameter(cvtAnimation,
                                                               "rotation",
                                                               "FLOAT_VEC4",
                                                               adjustedBuffer,
                                                               animationsOutputStream);
                             
                             __AddChannel(cvtAnimation, targetID, path);
                         }
                     }
                 }
             }
             cvtAnimation->removeParameterNamed("OUTPUT");
         }
             return true;
             break;
         case COLLADAFW::AnimationList::POSITION_X:
         case COLLADAFW::AnimationList::POSITION_Y:
         case COLLADAFW::AnimationList::POSITION_Z:
         case COLLADAFW::AnimationList::COLOR_RGB:
         case COLLADAFW::AnimationList::COLOR_RGBA:
         case COLLADAFW::AnimationList::COLOR_R:
         case COLLADAFW::AnimationList::COLOR_G:
         case COLLADAFW::AnimationList::COLOR_B:
         case COLLADAFW::AnimationList::COLOR_A:
         case COLLADAFW::AnimationList::ARRAY_ELEMENT_1D:
         case COLLADAFW::AnimationList::ARRAY_ELEMENT_2D:
         case COLLADAFW::AnimationList::FLOAT: 
         default:
             break;
     }
     
     return false;
 }