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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
#include "GLTF.h"
#include "../GLTFOpenCOLLADA.h"
#include "GLTFAsset.h"
#include "animationConverter.h"
#include "meshConverter.h"
#include "../helpers/mathHelpers.h"
#include "GLTF-Open3DGC.h"
using namespace rapidjson;
#if __cplusplus <= 199711L
using namespace std::tr1;
#endif
using namespace std;
namespace GLTF
{
#define ANIMATIONFLATTENER_FOR_PATH_AND_TARGETID(path, targetID) std::string targetUIDWithPath = path+targetID;\
if (asset->_targetUIDWithPathToAnimationFlattener.count(targetUIDWithPath) > 0) {\
animationFlattener = asset->_targetUIDWithPathToAnimationFlattener[targetUIDWithPath];\
} else {\
animationFlattener = cvtAnimation->animationFlattenerForTargetUID(targetID);\
asset->_targetUIDWithPathToAnimationFlattener[targetUIDWithPath] = animationFlattener;\
}
/*
The animation creation/write is in 2 steps.
We first create the animation, but then, we need to set the channels for every targets, and targets ids are not available when animations are created
*/
bool writeAnimation(shared_ptr <GLTFAnimation> cvtAnimation,
const COLLADAFW::AnimationList::AnimationClass animationClass,
AnimatedTargetsSharedPtr animatedTargets,
GLTF::GLTFAsset *asset) {
std::string inputParameterName = "TIME";
shared_ptr<JSONObject> samplers = cvtAnimation->samplers();
shared_ptr<JSONArray> channels = cvtAnimation->channels();
shared_ptr<GLTFBufferView> timeBufferView = cvtAnimation->getBufferViewForParameter(inputParameterName);
shared_ptr<GLTFAnimationFlattener> animationFlattener;
shared_ptr<GLTFBufferView> bufferView = cvtAnimation->getBufferViewForParameter("OUTPUT");
cvtAnimation->unregisterBufferView("OUTPUT");
switch (animationClass) {
case COLLADAFW::AnimationList::TIME:
{
//In Currrent OpenCOLLADA Implementation, this is never called, only cases mapping to OUTPUT are, so we handle INPUT (i.e time) when we enter this function.
}
break;
case COLLADAFW::AnimationList::AXISANGLE: {
//the angles to radians necessary convertion is done within the animationFlattener
//but it might be better to make it before...
for (size_t animatedTargetIndex = 0 ; animatedTargetIndex < animatedTargets->size() ; animatedTargetIndex++) {
shared_ptr<JSONObject> animatedTarget = (*animatedTargets)[animatedTargetIndex];
std::string targetID = animatedTarget->getString(kTarget);
if (asset->_uniqueIDToOpenCOLLADAObject.count(targetID) != 0) {
cvtAnimation->targets()->setValue(targetID, animatedTarget);
std::string path = animatedTarget->getString("path");
if (path == "rotation") {
std::string transformID = animatedTarget->getString("transformId");
animationFlattener = cvtAnimation->animationFlattenerForTargetUID(targetID);
float* timeValues = (float*)timeBufferView->getBufferDataByApplyingOffset();
float* rotations = (float*)bufferView->getBufferDataByApplyingOffset();
for (size_t k = 0 ; k < cvtAnimation->getCount() ; k++) {
size_t offset = k * 4;
shared_ptr <COLLADAFW::Rotate> rotate(new COLLADAFW::Rotate(rotations[offset + 0],
rotations[offset + 1],
rotations[offset + 2],
rotations[offset + 3]));
animationFlattener->insertTransformAtTime(transformID, rotate, timeValues[k]);
}
}
}
}
}
return true;
case COLLADAFW::AnimationList::MATRIX4X4: {
std::vector< shared_ptr <GLTFBufferView> > TRSBufferViews;
//FIXME: we assume float here, might be double
float* matrices = (float*)bufferView->getBufferDataByApplyingOffset();
float* timeValues = (float*)timeBufferView->getBufferDataByApplyingOffset();
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(kTarget);
if (asset->_uniqueIDToOpenCOLLADAObject.count(targetID) != 0) {
cvtAnimation->targets()->setValue(targetID, animatedTarget);
std::string transformID = animatedTarget->getString("transformId");
animationFlattener = cvtAnimation->animationFlattenerForTargetUID(targetID);
for (size_t k = 0 ; k < cvtAnimation->getCount() ; k++) {
size_t offset = k * 16;
float *m = matrices + offset;
COLLADABU::Math::Matrix4 mat;
mat.setAllElements(m[0], m[1], m[2], m[3],
m[4], m[5], m[6], m[7],
m[8], m[9], m[10], m[11],
m[12], m[13], m[14], m[15] );
shared_ptr <COLLADAFW::Matrix> matTr(new COLLADAFW::Matrix(mat));
animationFlattener->insertTransformAtTime(transformID, matTr, timeValues[k]);
}
}
}
}
}
return true;
case COLLADAFW::AnimationList::POSITION_XYZ: {
//the angles to radians necessary convertion is done within the animationFlattener
//but it might be better to make it before...
for (size_t animatedTargetIndex = 0 ; animatedTargetIndex < animatedTargets->size() ; animatedTargetIndex++) {
shared_ptr<JSONObject> animatedTarget = (*animatedTargets)[animatedTargetIndex];
std::string targetID = animatedTarget->getString(kTarget);
if (asset->_uniqueIDToOpenCOLLADAObject.count(targetID) != 0) {
cvtAnimation->targets()->setValue(targetID, animatedTarget);
std::string path = animatedTarget->getString("path");
if (path == "translation") {
std::string transformID = animatedTarget->getString("transformId");
animationFlattener = cvtAnimation->animationFlattenerForTargetUID(targetID);
float* timeValues = (float*)timeBufferView->getBufferDataByApplyingOffset();
float* translations = (float*)bufferView->getBufferDataByApplyingOffset();
for (size_t k = 0 ; k < cvtAnimation->getCount() ; k++) {
size_t offset = k * 3;
shared_ptr <COLLADAFW::Translate> translate(new COLLADAFW::Translate(translations[offset + 0],
translations[offset + 1],
translations[offset + 2]));
animationFlattener->insertTransformAtTime(transformID, translate, timeValues[k]);
}
} else if (path == "scale") {
std::string transformID = animatedTarget->getString("transformId");
animationFlattener = cvtAnimation->animationFlattenerForTargetUID(targetID);
float* timeValues = (float*)timeBufferView->getBufferDataByApplyingOffset();
float* scales = (float*)bufferView->getBufferDataByApplyingOffset();
for (size_t k = 0 ; k < cvtAnimation->getCount() ; k++) {
size_t offset = k * 3;
shared_ptr <COLLADAFW::Scale> scale(new COLLADAFW::Scale(scales[offset + 0],
scales[offset + 1],
scales[offset + 2]));
animationFlattener->insertTransformAtTime(transformID, scale, timeValues[k]);
}
}
}
}
}
return true;
case COLLADAFW::AnimationList::ANGLE: {
//the angles to radians necessary convertion is done within the animationFlattener
//but it might be better to make it before...
for (size_t animatedTargetIndex = 0; animatedTargetIndex < animatedTargets->size(); animatedTargetIndex++) {
shared_ptr<JSONObject> animatedTarget = (*animatedTargets)[animatedTargetIndex];
std::string targetID = animatedTarget->getString(kTarget);
if (asset->_uniqueIDToOpenCOLLADAObject.count(targetID) != 0) {
cvtAnimation->targets()->setValue(targetID, animatedTarget);
std::string path = animatedTarget->getString("path");
if (path == "rotation") {
std::string transformID = animatedTarget->getString("transformId");
ANIMATIONFLATTENER_FOR_PATH_AND_TARGETID(path, targetID);
float* timeValues = (float*)timeBufferView->getBufferDataByApplyingOffset();
float* rotations = (float*)bufferView->getBufferDataByApplyingOffset();
for (size_t k = 0; k < cvtAnimation->getCount(); k++) {
animationFlattener->insertValueAtTime(transformID, rotations[k], 3, timeValues[k]);
}
}
}
}
}
return true;
case COLLADAFW::AnimationList::POSITION_X:
case COLLADAFW::AnimationList::POSITION_Y:
case COLLADAFW::AnimationList::POSITION_Z:
{
int index = animationClass - COLLADAFW::AnimationList::POSITION_X;
for (size_t animatedTargetIndex = 0; animatedTargetIndex < animatedTargets->size(); animatedTargetIndex++) {
shared_ptr<JSONObject> animatedTarget = (*animatedTargets)[animatedTargetIndex];
std::string targetID = animatedTarget->getString(kTarget);
if (asset->_uniqueIDToOpenCOLLADAObject.count(targetID) != 0) {
cvtAnimation->targets()->setValue(targetID, animatedTarget);
std::string path = animatedTarget->getString("path");
std::string transformID = animatedTarget->getString("transformId");
ANIMATIONFLATTENER_FOR_PATH_AND_TARGETID(path, targetID);
float* timeValues = (float*)timeBufferView->getBufferDataByApplyingOffset();
float* values = (float*)bufferView->getBufferDataByApplyingOffset();
for (size_t k = 0; k < cvtAnimation->getCount(); k++) {
animationFlattener->insertValueAtTime(transformID, values[k], index, timeValues[k]);
}
}
}
return true;
}
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:
static bool printedOnce = false;
if (!printedOnce) {
printf("WARNING: unhandled transform type\n");
printedOnce = true;
}
break;
}
return false;
}
shared_ptr <GLTFAnimation> convertOpenCOLLADAAnimationToGLTFAnimation(const COLLADAFW::Animation* animation, GLTF::GLTFAsset *asset)
{
shared_ptr <GLTFAnimation> cvtAnimation(new GLTFAnimation());
if (animation->getAnimationType() == COLLADAFW::Animation::ANIMATION_CURVE) {
shared_ptr <JSONObject> animationParameters = cvtAnimation->parameters();
const COLLADAFW::AnimationCurve *animationCurve = (const COLLADAFW::AnimationCurve*)animation;
//This needs to be fixed when re-working: https://github.com/KhronosGroup/glTF/issues/158
//especially, this point: "by default the converter should replicate COLLADA animations layout (not yet done), but an option should allow to have one animation per target. (this is actually the case)."
std::string animationID = uniqueIdWithType(kAnimation, animation->getUniqueId());
cvtAnimation->setID(animationID);
cvtAnimation->setCount(animationCurve->getKeyCount());
/** Returns the input values of the animation. */
const COLLADAFW::FloatOrDoubleArray &inputArray = animationCurve->getInputValues();
const COLLADAFW::FloatOrDoubleArray &outputArray = animationCurve->getOutputValues();
// Scales any distance values if needed
if (asset->getDistanceScale() != 1.0)
{
bool bNeedsScale = false;
const COLLADAFW::PhysicalDimensionArray& dimensions = animationCurve->getOutPhysicalDimensions();
size_t numDimensions = animationCurve->getOutDimension();
for (size_t dimIndex = 0; dimIndex < numDimensions; ++dimIndex)
{
if (dimensions[dimIndex] == COLLADAFW::PHYSICAL_DIMENSION_LENGTH)
{
bNeedsScale = true;
break;
}
}
if (bNeedsScale)
{
switch (outputArray.getType()) {
case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT: {
COLLADAFW::FloatArray* array = const_cast<COLLADAFW::FloatArray*>(outputArray.getFloatValues());
float* fArray = array->getData();
for (size_t index = 0; index < array->getCount(); ++index)
{
if (dimensions[index % numDimensions] == COLLADAFW::PHYSICAL_DIMENSION_LENGTH)
{
fArray[index] *= (float)asset->getDistanceScale();
}
}
}
break;
case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE: {
COLLADAFW::DoubleArray* array = const_cast<COLLADAFW::DoubleArray*>(outputArray.getDoubleValues());
double* dArray = array->getData();
for (size_t index = 0; index < array->getCount(); ++index)
{
if (dimensions[index % numDimensions] == COLLADAFW::PHYSICAL_DIMENSION_LENGTH)
{
dArray[index] *= asset->getDistanceScale();
}
}
}
break;
default:
case COLLADAFW::MeshVertexData::DATA_TYPE_UNKNOWN:
//FIXME report error
break;
};
}
}
const std::string originalID = animationCurve->getOriginalId();
//shared_ptr <JSONObject> inputParameter(new JSONObject());
shared_ptr <GLTFBufferView> inputBufferView = convertFloatOrDoubleArrayToGLTFBufferView(inputArray);
shared_ptr <GLTFBufferView> outputBufferView = convertFloatOrDoubleArrayToGLTFBufferView(outputArray);
cvtAnimation->registerBufferView("TIME", inputBufferView);
cvtAnimation->registerBufferView("OUTPUT", outputBufferView);
}
return cvtAnimation;
}
}
|