2012-06-28 18:02:09 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2012, assimp team
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software 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.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2014-03-31 08:30:04 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
2012-06-28 18:02:09 +00:00
|
|
|
"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 THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS 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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2012-07-01 12:38:14 +00:00
|
|
|
/** @file FBXConverter.cpp
|
|
|
|
* @brief Implementation of the FBX DOM -> aiScene converter
|
2012-06-28 18:02:09 +00:00
|
|
|
*/
|
|
|
|
#include "AssimpPCH.h"
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
|
|
|
|
|
2013-01-25 03:44:08 +00:00
|
|
|
#include <iterator>
|
2014-03-09 15:18:05 +00:00
|
|
|
#include <sstream>
|
2012-07-21 02:15:10 +00:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
#include "FBXParser.h"
|
|
|
|
#include "FBXConverter.h"
|
|
|
|
#include "FBXDocument.h"
|
|
|
|
#include "FBXUtil.h"
|
2012-07-02 17:59:31 +00:00
|
|
|
#include "FBXProperties.h"
|
2012-07-03 18:41:21 +00:00
|
|
|
#include "FBXImporter.h"
|
2012-06-28 18:02:09 +00:00
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
namespace FBX {
|
2012-07-02 17:59:31 +00:00
|
|
|
|
|
|
|
using namespace Util;
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
|
|
|
|
#define MAGIC_NODE_TAG "_$AssimpFbx$"
|
2012-08-25 20:24:08 +00:00
|
|
|
|
2012-08-25 14:13:19 +00:00
|
|
|
#define CONVERT_FBX_TIME(time) static_cast<double>(time) / 46186158000L
|
2012-07-27 01:00:16 +00:00
|
|
|
|
2012-07-03 23:45:15 +00:00
|
|
|
// XXX vc9's debugger won't step into anonymous namespaces
|
|
|
|
//namespace {
|
2012-06-28 18:02:09 +00:00
|
|
|
|
|
|
|
/** Dummy class to encapsulate the conversion process */
|
|
|
|
class Converter
|
|
|
|
{
|
2012-07-27 19:09:45 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
/** the different parts that make up the final local transformation of a fbx node */
|
|
|
|
enum TransformationComp
|
|
|
|
{
|
|
|
|
TransformationComp_Translation = 0,
|
|
|
|
TransformationComp_RotationOffset,
|
|
|
|
TransformationComp_RotationPivot,
|
|
|
|
TransformationComp_PreRotation,
|
|
|
|
TransformationComp_Rotation,
|
|
|
|
TransformationComp_PostRotation,
|
|
|
|
TransformationComp_RotationPivotInverse,
|
|
|
|
TransformationComp_ScalingOffset,
|
|
|
|
TransformationComp_ScalingPivot,
|
|
|
|
TransformationComp_Scaling,
|
|
|
|
TransformationComp_ScalingPivotInverse,
|
2013-12-29 19:02:16 +00:00
|
|
|
TransformationComp_GeometricTranslation,
|
|
|
|
TransformationComp_GeometricRotation,
|
|
|
|
TransformationComp_GeometricScaling,
|
2012-07-27 19:09:45 +00:00
|
|
|
|
|
|
|
TransformationComp_MAXIMUM
|
|
|
|
};
|
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
Converter(aiScene* out, const Document& doc)
|
2013-04-08 18:13:29 +00:00
|
|
|
: defaultMaterialIndex()
|
|
|
|
, out(out)
|
2012-06-28 18:02:09 +00:00
|
|
|
, doc(doc)
|
|
|
|
{
|
2012-07-27 14:18:47 +00:00
|
|
|
// animations need to be converted first since this will
|
|
|
|
// populate the node_anim_chain_bits map, which is needed
|
|
|
|
// to determine which nodes need to be generated.
|
2012-07-21 14:19:11 +00:00
|
|
|
ConvertAnimations();
|
2012-07-27 14:18:47 +00:00
|
|
|
ConvertRootNode();
|
2012-06-28 21:44:23 +00:00
|
|
|
|
2012-07-04 13:07:57 +00:00
|
|
|
if(doc.Settings().readAllMaterials) {
|
|
|
|
// unfortunately this means we have to evaluate all objects
|
|
|
|
BOOST_FOREACH(const ObjectMap::value_type& v,doc.Objects()) {
|
2012-07-03 23:45:15 +00:00
|
|
|
|
2012-07-04 13:07:57 +00:00
|
|
|
const Object* ob = v.second->Get();
|
|
|
|
if(!ob) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Material* mat = dynamic_cast<const Material*>(ob);
|
|
|
|
if(mat) {
|
2012-07-03 23:45:15 +00:00
|
|
|
|
2012-07-04 13:07:57 +00:00
|
|
|
if (materials_converted.find(mat) == materials_converted.end()) {
|
2014-05-27 07:11:27 +00:00
|
|
|
ConvertMaterial(*mat, 0);
|
2012-07-04 13:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-03 23:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
TransferDataToScene();
|
2012-08-25 16:17:11 +00:00
|
|
|
|
|
|
|
// if we didn't read any meshes set the AI_SCENE_FLAGS_INCOMPLETE
|
|
|
|
// to make sure the scene passes assimp's validation. FBX files
|
|
|
|
// need not contain geometry (i.e. camera animations, raw armatures).
|
|
|
|
if (out->mNumMeshes == 0) {
|
|
|
|
out->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
|
|
|
|
}
|
2012-06-28 18:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
~Converter()
|
|
|
|
{
|
|
|
|
std::for_each(meshes.begin(),meshes.end(),Util::delete_fun<aiMesh>());
|
2012-07-02 17:59:31 +00:00
|
|
|
std::for_each(materials.begin(),materials.end(),Util::delete_fun<aiMaterial>());
|
2012-07-21 18:02:00 +00:00
|
|
|
std::for_each(animations.begin(),animations.end(),Util::delete_fun<aiAnimation>());
|
2012-08-10 23:51:04 +00:00
|
|
|
std::for_each(lights.begin(),lights.end(),Util::delete_fun<aiLight>());
|
|
|
|
std::for_each(cameras.begin(),cameras.end(),Util::delete_fun<aiCamera>());
|
2012-06-28 18:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// find scene root and trigger recursive scene conversion
|
|
|
|
void ConvertRootNode()
|
|
|
|
{
|
2012-07-04 14:33:21 +00:00
|
|
|
out->mRootNode = new aiNode();
|
2012-07-06 17:32:41 +00:00
|
|
|
out->mRootNode->mName.Set("RootNode");
|
2012-07-04 14:33:21 +00:00
|
|
|
|
|
|
|
// root has ID 0
|
|
|
|
ConvertNodes(0L, *out->mRootNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// collect and assign child nodes
|
2012-07-27 20:36:40 +00:00
|
|
|
void ConvertNodes(uint64_t id, aiNode& parent, const aiMatrix4x4& parent_transform = aiMatrix4x4())
|
2012-07-04 14:33:21 +00:00
|
|
|
{
|
2012-07-21 18:02:00 +00:00
|
|
|
const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(id, "Model");
|
2012-07-04 14:33:21 +00:00
|
|
|
|
|
|
|
std::vector<aiNode*> nodes;
|
|
|
|
nodes.reserve(conns.size());
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
std::vector<aiNode*> nodes_chain;
|
|
|
|
|
2012-07-26 23:49:02 +00:00
|
|
|
try {
|
|
|
|
BOOST_FOREACH(const Connection* con, conns) {
|
2012-06-28 18:02:09 +00:00
|
|
|
|
2012-07-26 23:49:02 +00:00
|
|
|
// ignore object-property links
|
|
|
|
if(con->PropertyName().length()) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-04 14:33:21 +00:00
|
|
|
|
2012-07-26 23:49:02 +00:00
|
|
|
const Object* const object = con->SourceObject();
|
|
|
|
if(!object) {
|
|
|
|
FBXImporter::LogWarn("failed to convert source object for Model link");
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-04 14:33:21 +00:00
|
|
|
|
2012-07-26 23:49:02 +00:00
|
|
|
const Model* const model = dynamic_cast<const Model*>(object);
|
2012-07-04 14:33:21 +00:00
|
|
|
|
2012-07-26 23:49:02 +00:00
|
|
|
if(model) {
|
2012-07-27 01:00:16 +00:00
|
|
|
nodes_chain.clear();
|
2012-07-27 19:09:45 +00:00
|
|
|
|
2012-07-27 20:36:40 +00:00
|
|
|
aiMatrix4x4 new_abs_transform = parent_transform;
|
|
|
|
|
2012-07-27 19:09:45 +00:00
|
|
|
// even though there is only a single input node, the design of
|
|
|
|
// assimp (or rather: the complicated transformation chain that
|
|
|
|
// is employed by fbx) means that we may need multiple aiNode's
|
|
|
|
// to represent a fbx node's transformation.
|
2012-07-27 01:00:16 +00:00
|
|
|
GenerateTransformationNodeChain(*model,nodes_chain);
|
|
|
|
|
|
|
|
ai_assert(nodes_chain.size());
|
2012-07-06 14:55:25 +00:00
|
|
|
|
2012-07-27 19:09:45 +00:00
|
|
|
const std::string& original_name = FixNodeName(model->Name());
|
|
|
|
|
|
|
|
// check if any of the nodes in the chain has the name the fbx node
|
|
|
|
// is supposed to have. If there is none, add another node to
|
|
|
|
// preserve the name - people might have scripts etc. that rely
|
|
|
|
// on specific node names.
|
2012-08-25 20:24:08 +00:00
|
|
|
aiNode* name_carrier = NULL;
|
2012-07-27 19:09:45 +00:00
|
|
|
BOOST_FOREACH(aiNode* prenode, nodes_chain) {
|
|
|
|
if ( !strcmp(prenode->mName.C_Str(), original_name.c_str()) ) {
|
2012-08-25 20:24:08 +00:00
|
|
|
name_carrier = prenode;
|
2012-07-27 19:09:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 20:24:08 +00:00
|
|
|
if(!name_carrier) {
|
2012-07-27 19:09:45 +00:00
|
|
|
nodes_chain.push_back(new aiNode(original_name));
|
2012-08-25 20:24:08 +00:00
|
|
|
name_carrier = nodes_chain.back();
|
2012-07-27 19:09:45 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 11:06:34 +00:00
|
|
|
//setup metadata on newest node
|
|
|
|
SetupNodeMetadata(*model, *nodes_chain.back());
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
// link all nodes in a row
|
|
|
|
aiNode* last_parent = &parent;
|
|
|
|
BOOST_FOREACH(aiNode* prenode, nodes_chain) {
|
|
|
|
ai_assert(prenode);
|
2012-07-04 14:33:21 +00:00
|
|
|
|
2012-07-27 17:29:57 +00:00
|
|
|
if(last_parent != &parent) {
|
|
|
|
last_parent->mNumChildren = 1;
|
|
|
|
last_parent->mChildren = new aiNode*[1];
|
|
|
|
last_parent->mChildren[0] = prenode;
|
|
|
|
}
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
prenode->mParent = last_parent;
|
|
|
|
last_parent = prenode;
|
2012-07-27 20:36:40 +00:00
|
|
|
|
|
|
|
new_abs_transform *= prenode->mTransformation;
|
2012-07-27 01:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// attach geometry
|
2012-07-27 20:36:40 +00:00
|
|
|
ConvertModel(*model, *nodes_chain.back(), new_abs_transform);
|
2012-07-04 14:33:21 +00:00
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
// attach sub-nodes
|
2012-07-27 20:36:40 +00:00
|
|
|
ConvertNodes(model->ID(), *nodes_chain.back(), new_abs_transform);
|
2012-07-27 01:00:16 +00:00
|
|
|
|
2012-08-11 14:41:44 +00:00
|
|
|
if(doc.Settings().readLights) {
|
|
|
|
ConvertLights(*model);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(doc.Settings().readCameras) {
|
|
|
|
ConvertCameras(*model);
|
|
|
|
}
|
2012-08-10 23:51:04 +00:00
|
|
|
|
2012-07-27 17:29:57 +00:00
|
|
|
nodes.push_back(nodes_chain.front());
|
|
|
|
nodes_chain.clear();
|
2012-07-26 23:49:02 +00:00
|
|
|
}
|
2012-07-04 14:33:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 23:49:02 +00:00
|
|
|
if(nodes.size()) {
|
|
|
|
parent.mChildren = new aiNode*[nodes.size()]();
|
|
|
|
parent.mNumChildren = static_cast<unsigned int>(nodes.size());
|
2012-07-04 14:33:21 +00:00
|
|
|
|
2012-07-26 23:49:02 +00:00
|
|
|
std::swap_ranges(nodes.begin(),nodes.end(),parent.mChildren);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(std::exception&) {
|
2012-07-27 01:00:16 +00:00
|
|
|
Util::delete_fun<aiNode> deleter;
|
|
|
|
std::for_each(nodes.begin(),nodes.end(),deleter);
|
|
|
|
std::for_each(nodes_chain.begin(),nodes_chain.end(),deleter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-10 23:51:04 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ConvertLights(const Model& model)
|
|
|
|
{
|
|
|
|
const std::vector<const NodeAttribute*>& node_attrs = model.GetAttributes();
|
|
|
|
BOOST_FOREACH(const NodeAttribute* attr, node_attrs) {
|
|
|
|
const Light* const light = dynamic_cast<const Light*>(attr);
|
|
|
|
if(light) {
|
|
|
|
ConvertLight(model, *light);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ConvertCameras(const Model& model)
|
|
|
|
{
|
|
|
|
const std::vector<const NodeAttribute*>& node_attrs = model.GetAttributes();
|
|
|
|
BOOST_FOREACH(const NodeAttribute* attr, node_attrs) {
|
|
|
|
const Camera* const cam = dynamic_cast<const Camera*>(attr);
|
|
|
|
if(cam) {
|
|
|
|
ConvertCamera(model, *cam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ConvertLight(const Model& model, const Light& light)
|
|
|
|
{
|
|
|
|
lights.push_back(new aiLight());
|
|
|
|
aiLight* const out_light = lights.back();
|
|
|
|
|
|
|
|
out_light->mName.Set(FixNodeName(model.Name()));
|
|
|
|
|
|
|
|
const float intensity = light.Intensity();
|
|
|
|
const aiVector3D& col = light.Color();
|
|
|
|
|
|
|
|
out_light->mColorDiffuse = aiColor3D(col.x,col.y,col.z);
|
|
|
|
out_light->mColorDiffuse.r *= intensity;
|
|
|
|
out_light->mColorDiffuse.g *= intensity;
|
|
|
|
out_light->mColorDiffuse.b *= intensity;
|
|
|
|
|
|
|
|
out_light->mColorSpecular = out_light->mColorDiffuse;
|
|
|
|
|
|
|
|
switch(light.LightType())
|
|
|
|
{
|
|
|
|
case Light::Type_Point:
|
|
|
|
out_light->mType = aiLightSource_POINT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Light::Type_Directional:
|
|
|
|
out_light->mType = aiLightSource_DIRECTIONAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Light::Type_Spot:
|
|
|
|
out_light->mType = aiLightSource_SPOT;
|
|
|
|
out_light->mAngleOuterCone = AI_DEG_TO_RAD(light.OuterAngle());
|
|
|
|
out_light->mAngleInnerCone = AI_DEG_TO_RAD(light.InnerAngle());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Light::Type_Area:
|
|
|
|
FBXImporter::LogWarn("cannot represent area light, set to UNDEFINED");
|
|
|
|
out_light->mType = aiLightSource_UNDEFINED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Light::Type_Volume:
|
|
|
|
FBXImporter::LogWarn("cannot represent volume light, set to UNDEFINED");
|
|
|
|
out_light->mType = aiLightSource_UNDEFINED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: how to best convert the near and far decay ranges?
|
|
|
|
switch(light.DecayType())
|
|
|
|
{
|
|
|
|
case Light::Decay_None:
|
|
|
|
out_light->mAttenuationConstant = 1.0f;
|
|
|
|
break;
|
|
|
|
case Light::Decay_Linear:
|
|
|
|
out_light->mAttenuationLinear = 1.0f;
|
|
|
|
break;
|
|
|
|
case Light::Decay_Quadratic:
|
|
|
|
out_light->mAttenuationQuadratic = 1.0f;
|
|
|
|
break;
|
|
|
|
case Light::Decay_Cubic:
|
|
|
|
FBXImporter::LogWarn("cannot represent cubic attenuation, set to Quadratic");
|
|
|
|
out_light->mAttenuationQuadratic = 1.0f;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ConvertCamera(const Model& model, const Camera& cam)
|
|
|
|
{
|
|
|
|
cameras.push_back(new aiCamera());
|
|
|
|
aiCamera* const out_camera = cameras.back();
|
|
|
|
|
|
|
|
out_camera->mName.Set(FixNodeName(model.Name()));
|
|
|
|
|
2014-03-26 21:44:49 +00:00
|
|
|
out_camera->mAspect = cam.AspectWidth() / cam.AspectHeight();
|
2012-08-10 23:51:04 +00:00
|
|
|
out_camera->mPosition = cam.Position();
|
|
|
|
out_camera->mLookAt = cam.InterestPosition() - out_camera->mPosition;
|
|
|
|
out_camera->mHorizontalFOV = AI_DEG_TO_RAD(cam.FieldOfView());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-07-27 02:55:56 +00:00
|
|
|
// this returns unified names usable within assimp identifiers (i.e. no space characters -
|
|
|
|
// while these would be allowed, they are a potential trouble spot so better not use them).
|
2012-07-27 01:00:16 +00:00
|
|
|
const char* NameTransformationComp(TransformationComp comp)
|
|
|
|
{
|
|
|
|
switch(comp)
|
|
|
|
{
|
|
|
|
case TransformationComp_Translation:
|
|
|
|
return "Translation";
|
|
|
|
case TransformationComp_RotationOffset:
|
|
|
|
return "RotationOffset";
|
|
|
|
case TransformationComp_RotationPivot:
|
|
|
|
return "RotationPivot";
|
|
|
|
case TransformationComp_PreRotation:
|
|
|
|
return "PreRotation";
|
|
|
|
case TransformationComp_Rotation:
|
|
|
|
return "Rotation";
|
|
|
|
case TransformationComp_PostRotation:
|
|
|
|
return "PostRotation";
|
|
|
|
case TransformationComp_RotationPivotInverse:
|
|
|
|
return "RotationPivotInverse";
|
|
|
|
case TransformationComp_ScalingOffset:
|
|
|
|
return "ScalingOffset";
|
|
|
|
case TransformationComp_ScalingPivot:
|
|
|
|
return "ScalingPivot";
|
|
|
|
case TransformationComp_Scaling:
|
|
|
|
return "Scaling";
|
|
|
|
case TransformationComp_ScalingPivotInverse:
|
|
|
|
return "ScalingPivotInverse";
|
2013-12-29 19:02:16 +00:00
|
|
|
case TransformationComp_GeometricScaling:
|
|
|
|
return "GeometricScaling";
|
|
|
|
case TransformationComp_GeometricRotation:
|
|
|
|
return "GeometricRotation";
|
|
|
|
case TransformationComp_GeometricTranslation:
|
|
|
|
return "GeometricTranslation";
|
2013-04-08 17:45:09 +00:00
|
|
|
case TransformationComp_MAXIMUM: // this is to silence compiler warnings
|
|
|
|
break;
|
2012-07-04 14:33:21 +00:00
|
|
|
}
|
2012-07-27 01:00:16 +00:00
|
|
|
|
|
|
|
ai_assert(false);
|
2012-07-27 02:55:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// note: this returns the REAL fbx property names
|
|
|
|
const char* NameTransformationCompProperty(TransformationComp comp)
|
|
|
|
{
|
|
|
|
switch(comp)
|
|
|
|
{
|
|
|
|
case TransformationComp_Translation:
|
|
|
|
return "Lcl Translation";
|
|
|
|
case TransformationComp_RotationOffset:
|
|
|
|
return "RotationOffset";
|
|
|
|
case TransformationComp_RotationPivot:
|
|
|
|
return "RotationPivot";
|
|
|
|
case TransformationComp_PreRotation:
|
|
|
|
return "PreRotation";
|
|
|
|
case TransformationComp_Rotation:
|
|
|
|
return "Lcl Rotation";
|
|
|
|
case TransformationComp_PostRotation:
|
|
|
|
return "PostRotation";
|
|
|
|
case TransformationComp_RotationPivotInverse:
|
|
|
|
return "RotationPivotInverse";
|
|
|
|
case TransformationComp_ScalingOffset:
|
|
|
|
return "ScalingOffset";
|
|
|
|
case TransformationComp_ScalingPivot:
|
|
|
|
return "ScalingPivot";
|
|
|
|
case TransformationComp_Scaling:
|
|
|
|
return "Lcl Scaling";
|
|
|
|
case TransformationComp_ScalingPivotInverse:
|
|
|
|
return "ScalingPivotInverse";
|
2013-12-29 19:02:16 +00:00
|
|
|
case TransformationComp_GeometricScaling:
|
|
|
|
return "GeometricScaling";
|
|
|
|
case TransformationComp_GeometricRotation:
|
|
|
|
return "GeometricRotation";
|
|
|
|
case TransformationComp_GeometricTranslation:
|
|
|
|
return "GeometricTranslation";
|
2013-04-08 17:45:09 +00:00
|
|
|
case TransformationComp_MAXIMUM: // this is to silence compiler warnings
|
|
|
|
break;
|
2012-07-27 02:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ai_assert(false);
|
|
|
|
return NULL;
|
2012-06-28 18:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 16:45:02 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiVector3D TransformationCompDefaultValue(TransformationComp comp)
|
|
|
|
{
|
|
|
|
// XXX a neat way to solve the never-ending special cases for scaling
|
|
|
|
// would be to do everything in log space!
|
|
|
|
return comp == TransformationComp_Scaling ? aiVector3D(1.f,1.f,1.f) : aiVector3D();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-05 15:06:15 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-11 00:17:22 +00:00
|
|
|
void GetRotationMatrix(Model::RotOrder mode, const aiVector3D& rotation, aiMatrix4x4& out)
|
2012-07-27 01:00:16 +00:00
|
|
|
{
|
2012-08-11 00:17:22 +00:00
|
|
|
if(mode == Model::RotOrder_SphericXYZ) {
|
|
|
|
FBXImporter::LogError("Unsupported RotationMode: SphericXYZ");
|
|
|
|
out = aiMatrix4x4();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
const float angle_epsilon = 1e-6f;
|
|
|
|
|
|
|
|
out = aiMatrix4x4();
|
|
|
|
|
2012-08-11 00:17:22 +00:00
|
|
|
bool is_id[3] = { true, true, true };
|
|
|
|
|
|
|
|
aiMatrix4x4 temp[3];
|
2014-09-22 22:42:32 +00:00
|
|
|
if(std::fabs(rotation.z) > angle_epsilon) {
|
2012-08-11 00:17:22 +00:00
|
|
|
aiMatrix4x4::RotationZ(AI_DEG_TO_RAD(rotation.z),temp[2]);
|
|
|
|
is_id[2] = false;
|
|
|
|
}
|
2014-09-22 22:42:32 +00:00
|
|
|
if(std::fabs(rotation.y) > angle_epsilon) {
|
2012-08-11 00:17:22 +00:00
|
|
|
aiMatrix4x4::RotationY(AI_DEG_TO_RAD(rotation.y),temp[1]);
|
|
|
|
is_id[1] = false;
|
|
|
|
}
|
2014-09-22 22:42:32 +00:00
|
|
|
if(std::fabs(rotation.x) > angle_epsilon) {
|
2012-08-11 00:17:22 +00:00
|
|
|
aiMatrix4x4::RotationX(AI_DEG_TO_RAD(rotation.x),temp[0]);
|
|
|
|
is_id[0] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int order[3] = {-1, -1, -1};
|
|
|
|
|
|
|
|
// note: rotation order is inverted since we're left multiplying as is usual in assimp
|
2012-07-27 01:00:16 +00:00
|
|
|
switch(mode)
|
|
|
|
{
|
2012-08-11 00:17:22 +00:00
|
|
|
case Model::RotOrder_EulerXYZ:
|
|
|
|
order[0] = 2;
|
|
|
|
order[1] = 1;
|
|
|
|
order[2] = 0;
|
|
|
|
break;
|
2012-07-27 01:00:16 +00:00
|
|
|
|
2012-08-11 00:17:22 +00:00
|
|
|
case Model::RotOrder_EulerXZY:
|
|
|
|
order[0] = 1;
|
|
|
|
order[1] = 2;
|
|
|
|
order[2] = 0;
|
|
|
|
break;
|
2012-07-27 01:00:16 +00:00
|
|
|
|
2012-08-11 00:17:22 +00:00
|
|
|
case Model::RotOrder_EulerYZX:
|
|
|
|
order[0] = 0;
|
|
|
|
order[1] = 2;
|
|
|
|
order[2] = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Model::RotOrder_EulerYXZ:
|
|
|
|
order[0] = 2;
|
|
|
|
order[1] = 0;
|
|
|
|
order[2] = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Model::RotOrder_EulerZXY:
|
|
|
|
order[0] = 1;
|
|
|
|
order[1] = 0;
|
|
|
|
order[2] = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Model::RotOrder_EulerZYX:
|
|
|
|
order[0] = 0;
|
|
|
|
order[1] = 1;
|
|
|
|
order[2] = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
2012-07-27 01:00:16 +00:00
|
|
|
}
|
2013-08-07 22:43:29 +00:00
|
|
|
|
|
|
|
ai_assert((order[0] >= 0) && (order[0] <= 2));
|
|
|
|
ai_assert((order[1] >= 0) && (order[1] <= 2));
|
|
|
|
ai_assert((order[2] >= 0) && (order[2] <= 2));
|
2012-07-27 01:00:16 +00:00
|
|
|
|
2012-08-11 00:17:22 +00:00
|
|
|
if(!is_id[order[0]]) {
|
|
|
|
out = temp[order[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_id[order[1]]) {
|
|
|
|
out = out * temp[order[1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_id[order[2]]) {
|
|
|
|
out = out * temp[order[2]];
|
|
|
|
}
|
2012-07-27 01:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 02:55:56 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
/** checks if a node has more than just scaling, rotation and translation components */
|
|
|
|
bool NeedsComplexTransformationChain(const Model& model)
|
|
|
|
{
|
|
|
|
const PropertyTable& props = model.Props();
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
const float zero_epsilon = 1e-6f;
|
|
|
|
for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i) {
|
|
|
|
const TransformationComp comp = static_cast<TransformationComp>(i);
|
|
|
|
|
2013-12-29 19:02:16 +00:00
|
|
|
if( comp == TransformationComp_Rotation || comp == TransformationComp_Scaling || comp == TransformationComp_Translation ||
|
|
|
|
comp == TransformationComp_GeometricScaling || comp == TransformationComp_GeometricRotation || comp == TransformationComp_GeometricTranslation ) {
|
2012-07-27 02:55:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const aiVector3D& v = PropertyGet<aiVector3D>(props,NameTransformationCompProperty(comp),ok);
|
|
|
|
if(ok && v.SquareLength() > zero_epsilon) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 13:31:02 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// note: name must be a FixNodeName() result
|
|
|
|
std::string NameTransformationChainNode(const std::string& name, TransformationComp comp)
|
|
|
|
{
|
|
|
|
return name + std::string(MAGIC_NODE_TAG) + "_" + NameTransformationComp(comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
/** note: memory for output_nodes will be managed by the caller */
|
|
|
|
void GenerateTransformationNodeChain(const Model& model,
|
|
|
|
std::vector<aiNode*>& output_nodes)
|
2012-07-05 15:06:15 +00:00
|
|
|
{
|
|
|
|
const PropertyTable& props = model.Props();
|
2012-08-11 00:17:22 +00:00
|
|
|
const Model::RotOrder rot = model.RotationOrder();
|
2012-07-26 23:49:02 +00:00
|
|
|
|
2012-07-05 15:06:15 +00:00
|
|
|
bool ok;
|
2012-07-27 01:00:16 +00:00
|
|
|
|
|
|
|
aiMatrix4x4 chain[TransformationComp_MAXIMUM];
|
|
|
|
std::fill_n(chain, static_cast<unsigned int>(TransformationComp_MAXIMUM), aiMatrix4x4());
|
2012-07-05 15:06:15 +00:00
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
// generate transformation matrices for all the different transformation components
|
|
|
|
const float zero_epsilon = 1e-6f;
|
|
|
|
bool is_complex = false;
|
|
|
|
|
|
|
|
const aiVector3D& PreRotation = PropertyGet<aiVector3D>(props,"PreRotation",ok);
|
|
|
|
if(ok && PreRotation.SquareLength() > zero_epsilon) {
|
|
|
|
is_complex = true;
|
|
|
|
|
|
|
|
GetRotationMatrix(rot, PreRotation, chain[TransformationComp_PreRotation]);
|
2012-07-05 15:06:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
const aiVector3D& PostRotation = PropertyGet<aiVector3D>(props,"PostRotation",ok);
|
|
|
|
if(ok && PostRotation.SquareLength() > zero_epsilon) {
|
|
|
|
is_complex = true;
|
|
|
|
|
|
|
|
GetRotationMatrix(rot, PostRotation, chain[TransformationComp_PostRotation]);
|
2012-07-05 15:06:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
const aiVector3D& RotationPivot = PropertyGet<aiVector3D>(props,"RotationPivot",ok);
|
|
|
|
if(ok && RotationPivot.SquareLength() > zero_epsilon) {
|
|
|
|
is_complex = true;
|
|
|
|
|
|
|
|
aiMatrix4x4::Translation(RotationPivot,chain[TransformationComp_RotationPivot]);
|
|
|
|
aiMatrix4x4::Translation(-RotationPivot,chain[TransformationComp_RotationPivotInverse]);
|
2012-07-05 15:06:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
const aiVector3D& RotationOffset = PropertyGet<aiVector3D>(props,"RotationOffset",ok);
|
|
|
|
if(ok && RotationOffset.SquareLength() > zero_epsilon) {
|
|
|
|
is_complex = true;
|
|
|
|
|
|
|
|
aiMatrix4x4::Translation(RotationOffset,chain[TransformationComp_RotationOffset]);
|
2012-07-05 15:06:15 +00:00
|
|
|
}
|
2012-07-27 01:00:16 +00:00
|
|
|
|
|
|
|
const aiVector3D& ScalingOffset = PropertyGet<aiVector3D>(props,"ScalingOffset",ok);
|
|
|
|
if(ok && ScalingOffset.SquareLength() > zero_epsilon) {
|
|
|
|
is_complex = true;
|
|
|
|
|
|
|
|
aiMatrix4x4::Translation(ScalingOffset,chain[TransformationComp_ScalingOffset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const aiVector3D& ScalingPivot = PropertyGet<aiVector3D>(props,"ScalingPivot",ok);
|
|
|
|
if(ok && ScalingPivot.SquareLength() > zero_epsilon) {
|
|
|
|
is_complex = true;
|
|
|
|
|
|
|
|
aiMatrix4x4::Translation(ScalingPivot,chain[TransformationComp_ScalingPivot]);
|
|
|
|
aiMatrix4x4::Translation(-ScalingPivot,chain[TransformationComp_ScalingPivotInverse]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const aiVector3D& Translation = PropertyGet<aiVector3D>(props,"Lcl Translation",ok);
|
|
|
|
if(ok && Translation.SquareLength() > zero_epsilon) {
|
|
|
|
aiMatrix4x4::Translation(Translation,chain[TransformationComp_Translation]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const aiVector3D& Scaling = PropertyGet<aiVector3D>(props,"Lcl Scaling",ok);
|
2014-09-22 22:42:32 +00:00
|
|
|
if(ok && std::fabs(Scaling.SquareLength()-1.0f) > zero_epsilon) {
|
2012-07-27 01:00:16 +00:00
|
|
|
aiMatrix4x4::Scaling(Scaling,chain[TransformationComp_Scaling]);
|
2012-07-05 15:06:15 +00:00
|
|
|
}
|
2012-07-27 01:00:16 +00:00
|
|
|
|
|
|
|
const aiVector3D& Rotation = PropertyGet<aiVector3D>(props,"Lcl Rotation",ok);
|
2012-07-27 17:29:57 +00:00
|
|
|
if(ok && Rotation.SquareLength() > zero_epsilon) {
|
2012-07-27 01:00:16 +00:00
|
|
|
GetRotationMatrix(rot, Rotation, chain[TransformationComp_Rotation]);
|
|
|
|
}
|
2013-12-29 19:02:16 +00:00
|
|
|
|
|
|
|
const aiVector3D& GeometricScaling = PropertyGet<aiVector3D>(props, "GeometricScaling", ok);
|
2014-09-22 22:42:32 +00:00
|
|
|
if (ok && std::fabs(GeometricScaling.SquareLength() - 1.0f) > zero_epsilon) {
|
2013-12-29 19:02:16 +00:00
|
|
|
aiMatrix4x4::Scaling(GeometricScaling, chain[TransformationComp_GeometricScaling]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const aiVector3D& GeometricRotation = PropertyGet<aiVector3D>(props, "GeometricRotation", ok);
|
|
|
|
if (ok && GeometricRotation.SquareLength() > zero_epsilon) {
|
|
|
|
GetRotationMatrix(rot, GeometricRotation, chain[TransformationComp_GeometricRotation]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const aiVector3D& GeometricTranslation = PropertyGet<aiVector3D>(props, "GeometricTranslation", ok);
|
|
|
|
if (ok && GeometricTranslation.SquareLength() > zero_epsilon){
|
|
|
|
aiMatrix4x4::Translation(GeometricTranslation, chain[TransformationComp_GeometricTranslation]);
|
|
|
|
}
|
2012-07-27 01:00:16 +00:00
|
|
|
|
2012-07-27 02:55:56 +00:00
|
|
|
// is_complex needs to be consistent with NeedsComplexTransformationChain()
|
|
|
|
// or the interplay between this code and the animation converter would
|
|
|
|
// not be guaranteed.
|
|
|
|
ai_assert(NeedsComplexTransformationChain(model) == is_complex);
|
|
|
|
|
2012-07-27 01:00:16 +00:00
|
|
|
const std::string& name = FixNodeName(model.Name());
|
|
|
|
|
|
|
|
// now, if we have more than just Translation, Scaling and Rotation,
|
|
|
|
// we need to generate a full node chain to accommodate for assimp's
|
|
|
|
// lack to express pivots and offsets.
|
|
|
|
if(is_complex && doc.Settings().preservePivots) {
|
|
|
|
FBXImporter::LogInfo("generating full transformation chain for node: " + name);
|
|
|
|
|
2012-07-27 14:18:47 +00:00
|
|
|
// query the anim_chain_bits dictionary to find out which chain elements
|
|
|
|
// have associated node animation channels. These can not be dropped
|
|
|
|
// even if they have identity transform in bind pose.
|
|
|
|
NodeAnimBitMap::const_iterator it = node_anim_chain_bits.find(name);
|
|
|
|
const unsigned int anim_chain_bitmask = (it == node_anim_chain_bits.end() ? 0 : (*it).second);
|
|
|
|
|
|
|
|
unsigned int bit = 0x1;
|
|
|
|
for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i, bit <<= 1) {
|
|
|
|
const TransformationComp comp = static_cast<TransformationComp>(i);
|
|
|
|
|
|
|
|
if (chain[i].IsIdentity() && (anim_chain_bitmask & bit) == 0) {
|
2012-08-20 16:33:54 +00:00
|
|
|
continue;
|
2012-07-27 01:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aiNode* nd = new aiNode();
|
|
|
|
output_nodes.push_back(nd);
|
2012-07-27 13:31:02 +00:00
|
|
|
|
2012-07-27 14:18:47 +00:00
|
|
|
nd->mName.Set(NameTransformationChainNode(name, comp));
|
2012-07-27 01:00:16 +00:00
|
|
|
nd->mTransformation = chain[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ai_assert(output_nodes.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else, we can just multiply the matrices together
|
|
|
|
aiNode* nd = new aiNode();
|
|
|
|
output_nodes.push_back(nd);
|
|
|
|
|
|
|
|
nd->mName.Set(name);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i) {
|
2012-07-27 17:41:14 +00:00
|
|
|
nd->mTransformation = nd->mTransformation * chain[i];
|
2012-07-05 15:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 11:06:34 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SetupNodeMetadata(const Model& model, aiNode& nd)
|
|
|
|
{
|
|
|
|
const PropertyTable& props = model.Props();
|
2014-03-09 15:18:05 +00:00
|
|
|
DirectPropertyMap unparsedProperties = props.GetUnparsedProperties();
|
2012-07-05 15:06:15 +00:00
|
|
|
|
2014-03-09 15:18:05 +00:00
|
|
|
// create metadata on node
|
|
|
|
std::size_t numStaticMetaData = 2;
|
2014-02-22 11:06:34 +00:00
|
|
|
aiMetadata* data = new aiMetadata();
|
2014-03-09 15:18:05 +00:00
|
|
|
data->mNumProperties = unparsedProperties.size() + numStaticMetaData;
|
2014-02-22 11:06:34 +00:00
|
|
|
data->mKeys = new aiString[data->mNumProperties]();
|
2014-03-15 07:37:45 +00:00
|
|
|
data->mValues = new aiMetadataEntry[data->mNumProperties]();
|
2014-02-23 10:27:34 +00:00
|
|
|
nd.mMetaData = data;
|
2014-03-09 15:18:05 +00:00
|
|
|
int index = 0;
|
2014-02-22 11:06:34 +00:00
|
|
|
|
2014-03-09 15:18:05 +00:00
|
|
|
// find user defined properties (3ds Max)
|
2014-03-14 13:17:34 +00:00
|
|
|
data->Set(index++, "UserProperties", aiString(PropertyGet<std::string>(props, "UDP3DSMAX", "")));
|
2014-02-23 10:27:34 +00:00
|
|
|
// preserve the info that a node was marked as Null node in the original file.
|
2014-03-14 13:17:34 +00:00
|
|
|
data->Set(index++, "IsNull", model.IsNull() ? true : false);
|
2014-03-09 15:18:05 +00:00
|
|
|
|
|
|
|
// add unparsed properties to the node's metadata
|
|
|
|
BOOST_FOREACH(const DirectPropertyMap::value_type& prop, unparsedProperties) {
|
|
|
|
|
|
|
|
// Interpret the property as a concrete type
|
2014-03-14 13:17:34 +00:00
|
|
|
if (const TypedProperty<bool>* interpreted = prop.second->As<TypedProperty<bool> >())
|
|
|
|
data->Set(index++, prop.first, interpreted->Value());
|
2014-03-09 16:04:27 +00:00
|
|
|
else if (const TypedProperty<int>* interpreted = prop.second->As<TypedProperty<int> >())
|
2014-03-14 13:17:34 +00:00
|
|
|
data->Set(index++, prop.first, interpreted->Value());
|
2014-03-09 16:04:27 +00:00
|
|
|
else if (const TypedProperty<uint64_t>* interpreted = prop.second->As<TypedProperty<uint64_t> >())
|
2014-03-14 13:17:34 +00:00
|
|
|
data->Set(index++, prop.first, interpreted->Value());
|
2014-03-09 16:04:27 +00:00
|
|
|
else if (const TypedProperty<float>* interpreted = prop.second->As<TypedProperty<float> >())
|
2014-03-14 13:17:34 +00:00
|
|
|
data->Set(index++, prop.first, interpreted->Value());
|
2014-05-22 14:22:38 +00:00
|
|
|
else if (const TypedProperty<std::string>* interpreted = prop.second->As<TypedProperty<std::string> >())
|
|
|
|
data->Set(index++, prop.first, aiString(interpreted->Value()));
|
2014-03-09 16:04:27 +00:00
|
|
|
else if (const TypedProperty<aiVector3D>* interpreted = prop.second->As<TypedProperty<aiVector3D> >())
|
2014-03-14 13:17:34 +00:00
|
|
|
data->Set(index++, prop.first, interpreted->Value());
|
|
|
|
else
|
|
|
|
assert(false);
|
2014-03-09 15:18:05 +00:00
|
|
|
}
|
2014-02-22 11:06:34 +00:00
|
|
|
}
|
2012-07-05 15:06:15 +00:00
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-07-27 20:36:40 +00:00
|
|
|
void ConvertModel(const Model& model, aiNode& nd, const aiMatrix4x4& node_global_transform)
|
2012-06-28 18:02:09 +00:00
|
|
|
{
|
2012-07-04 14:33:21 +00:00
|
|
|
const std::vector<const Geometry*>& geos = model.GetGeometry();
|
|
|
|
|
|
|
|
std::vector<unsigned int> meshes;
|
|
|
|
meshes.reserve(geos.size());
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Geometry* geo, geos) {
|
|
|
|
|
|
|
|
const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*>(geo);
|
|
|
|
if(mesh) {
|
2012-07-27 20:36:40 +00:00
|
|
|
const std::vector<unsigned int>& indices = ConvertMesh(*mesh, model, node_global_transform);
|
2012-07-06 17:32:41 +00:00
|
|
|
std::copy(indices.begin(),indices.end(),std::back_inserter(meshes) );
|
2012-07-04 14:33:21 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
FBXImporter::LogWarn("ignoring unrecognized geometry: " + geo->Name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(meshes.size()) {
|
|
|
|
nd.mMeshes = new unsigned int[meshes.size()]();
|
|
|
|
nd.mNumMeshes = static_cast<unsigned int>(meshes.size());
|
|
|
|
|
|
|
|
std::swap_ranges(meshes.begin(),meshes.end(),nd.mMeshes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// MeshGeometry -> aiMesh, return mesh index + 1 or 0 if the conversion failed
|
2012-08-10 20:33:57 +00:00
|
|
|
std::vector<unsigned int> ConvertMesh(const MeshGeometry& mesh,const Model& model,
|
|
|
|
const aiMatrix4x4& node_global_transform)
|
2012-07-04 14:33:21 +00:00
|
|
|
{
|
2012-07-05 14:35:08 +00:00
|
|
|
std::vector<unsigned int> temp;
|
|
|
|
|
2012-07-04 14:33:21 +00:00
|
|
|
MeshMap::const_iterator it = meshes_converted.find(&mesh);
|
|
|
|
if (it != meshes_converted.end()) {
|
2012-07-06 17:32:41 +00:00
|
|
|
std::copy((*it).second.begin(),(*it).second.end(),std::back_inserter(temp));
|
2012-07-05 14:35:08 +00:00
|
|
|
return temp;
|
2012-07-04 14:33:21 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 21:24:46 +00:00
|
|
|
const std::vector<aiVector3D>& vertices = mesh.GetVertices();
|
|
|
|
const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
|
|
|
|
if(vertices.empty() || faces.empty()) {
|
2012-07-04 14:33:21 +00:00
|
|
|
FBXImporter::LogWarn("ignoring empty geometry: " + mesh.Name());
|
2012-07-05 14:35:08 +00:00
|
|
|
return temp;
|
2012-06-28 21:24:46 +00:00
|
|
|
}
|
|
|
|
|
2012-07-05 14:35:08 +00:00
|
|
|
// one material per mesh maps easily to aiMesh. Multiple material
|
|
|
|
// meshes need to be split.
|
2012-08-10 20:33:57 +00:00
|
|
|
const MatIndexArray& mindices = mesh.GetMaterialIndices();
|
2012-07-20 23:00:26 +00:00
|
|
|
if (doc.Settings().readMaterials && !mindices.empty()) {
|
2012-08-10 20:33:57 +00:00
|
|
|
const MatIndexArray::value_type base = mindices[0];
|
|
|
|
BOOST_FOREACH(MatIndexArray::value_type index, mindices) {
|
2012-07-05 14:35:08 +00:00
|
|
|
if(index != base) {
|
2012-07-27 20:36:40 +00:00
|
|
|
return ConvertMeshMultiMaterial(mesh, model, node_global_transform);
|
2012-07-05 14:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// faster codepath, just copy the data
|
2012-07-27 20:36:40 +00:00
|
|
|
temp.push_back(ConvertMeshSingleMaterial(mesh, model, node_global_transform));
|
2012-07-05 14:35:08 +00:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-10 20:33:57 +00:00
|
|
|
aiMesh* SetupEmptyMesh(const MeshGeometry& mesh)
|
2012-07-05 14:35:08 +00:00
|
|
|
{
|
2012-07-06 14:48:09 +00:00
|
|
|
aiMesh* const out_mesh = new aiMesh();
|
|
|
|
meshes.push_back(out_mesh);
|
2012-07-06 17:32:41 +00:00
|
|
|
meshes_converted[&mesh].push_back(static_cast<unsigned int>(meshes.size()-1));
|
|
|
|
|
|
|
|
// set name
|
|
|
|
std::string name = mesh.Name();
|
|
|
|
if (name.substr(0,10) == "Geometry::") {
|
|
|
|
name = name.substr(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name.length()) {
|
|
|
|
out_mesh->mName.Set(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-10 20:33:57 +00:00
|
|
|
unsigned int ConvertMeshSingleMaterial(const MeshGeometry& mesh, const Model& model,
|
|
|
|
const aiMatrix4x4& node_global_transform)
|
2012-07-06 17:32:41 +00:00
|
|
|
{
|
2012-08-10 20:33:57 +00:00
|
|
|
const MatIndexArray& mindices = mesh.GetMaterialIndices();
|
|
|
|
aiMesh* const out_mesh = SetupEmptyMesh(mesh);
|
2012-07-06 14:48:09 +00:00
|
|
|
|
2012-07-05 14:35:08 +00:00
|
|
|
const std::vector<aiVector3D>& vertices = mesh.GetVertices();
|
|
|
|
const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
|
|
|
|
|
2012-06-28 21:24:46 +00:00
|
|
|
// copy vertices
|
2012-07-05 14:35:08 +00:00
|
|
|
out_mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
|
2012-06-28 21:24:46 +00:00
|
|
|
out_mesh->mVertices = new aiVector3D[vertices.size()];
|
|
|
|
std::copy(vertices.begin(),vertices.end(),out_mesh->mVertices);
|
|
|
|
|
|
|
|
// generate dummy faces
|
2012-07-05 14:35:08 +00:00
|
|
|
out_mesh->mNumFaces = static_cast<unsigned int>(faces.size());
|
2012-06-28 21:24:46 +00:00
|
|
|
aiFace* fac = out_mesh->mFaces = new aiFace[faces.size()]();
|
|
|
|
|
|
|
|
unsigned int cursor = 0;
|
|
|
|
BOOST_FOREACH(unsigned int pcount, faces) {
|
|
|
|
aiFace& f = *fac++;
|
|
|
|
f.mNumIndices = pcount;
|
|
|
|
f.mIndices = new unsigned int[pcount];
|
|
|
|
switch(pcount)
|
|
|
|
{
|
2012-07-05 14:35:08 +00:00
|
|
|
case 1:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
break;
|
2012-06-28 21:24:46 +00:00
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < pcount; ++i) {
|
|
|
|
f.mIndices[i] = cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy normals
|
2012-07-04 14:47:10 +00:00
|
|
|
const std::vector<aiVector3D>& normals = mesh.GetNormals();
|
2012-06-28 21:24:46 +00:00
|
|
|
if(normals.size()) {
|
|
|
|
ai_assert(normals.size() == vertices.size());
|
|
|
|
|
|
|
|
out_mesh->mNormals = new aiVector3D[vertices.size()];
|
|
|
|
std::copy(normals.begin(),normals.end(),out_mesh->mNormals);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy tangents - assimp requires both tangents and bitangents (binormals)
|
|
|
|
// to be present, or neither of them. Compute binormals from normals
|
|
|
|
// and tangents if needed.
|
|
|
|
const std::vector<aiVector3D>& tangents = mesh.GetTangents();
|
|
|
|
const std::vector<aiVector3D>* binormals = &mesh.GetBinormals();
|
|
|
|
|
|
|
|
if(tangents.size()) {
|
|
|
|
std::vector<aiVector3D> tempBinormals;
|
|
|
|
if (!binormals->size()) {
|
|
|
|
if (normals.size()) {
|
|
|
|
tempBinormals.resize(normals.size());
|
|
|
|
for (unsigned int i = 0; i < tangents.size(); ++i) {
|
|
|
|
tempBinormals[i] = normals[i] ^ tangents[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
binormals = &tempBinormals;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
binormals = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(binormals) {
|
|
|
|
ai_assert(tangents.size() == vertices.size() && binormals->size() == vertices.size());
|
|
|
|
|
|
|
|
out_mesh->mTangents = new aiVector3D[vertices.size()];
|
|
|
|
std::copy(tangents.begin(),tangents.end(),out_mesh->mTangents);
|
|
|
|
|
|
|
|
out_mesh->mBitangents = new aiVector3D[vertices.size()];
|
|
|
|
std::copy(binormals->begin(),binormals->end(),out_mesh->mBitangents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy texture coords
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
|
|
|
const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
|
|
|
|
if(uvs.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
aiVector3D* out_uv = out_mesh->mTextureCoords[i] = new aiVector3D[vertices.size()];
|
|
|
|
BOOST_FOREACH(const aiVector2D& v, uvs) {
|
|
|
|
*out_uv++ = aiVector3D(v.x,v.y,0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
out_mesh->mNumUVComponents[i] = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy vertex colors
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
|
|
|
|
const std::vector<aiColor4D>& colors = mesh.GetVertexColors(i);
|
|
|
|
if(colors.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_mesh->mColors[i] = new aiColor4D[vertices.size()];
|
|
|
|
std::copy(colors.begin(),colors.end(),out_mesh->mColors[i]);
|
|
|
|
}
|
2012-07-04 13:07:57 +00:00
|
|
|
|
2012-07-20 23:00:26 +00:00
|
|
|
if(!doc.Settings().readMaterials || mindices.empty()) {
|
2012-07-05 14:37:13 +00:00
|
|
|
FBXImporter::LogError("no material assigned to mesh, setting default material");
|
|
|
|
out_mesh->mMaterialIndex = GetDefaultMaterial();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ConvertMaterialForMesh(out_mesh,model,mesh,mindices[0]);
|
|
|
|
}
|
2012-07-05 14:35:08 +00:00
|
|
|
|
2012-07-26 20:53:31 +00:00
|
|
|
if(doc.Settings().readWeights && mesh.DeformerSkin() != NULL) {
|
2012-07-27 20:36:40 +00:00
|
|
|
ConvertWeights(out_mesh, model, mesh, node_global_transform, NO_MATERIAL_SEPARATION);
|
2012-07-26 20:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:32:41 +00:00
|
|
|
return static_cast<unsigned int>(meshes.size() - 1);
|
2012-07-05 14:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-10 20:33:57 +00:00
|
|
|
std::vector<unsigned int> ConvertMeshMultiMaterial(const MeshGeometry& mesh, const Model& model,
|
|
|
|
const aiMatrix4x4& node_global_transform)
|
2012-07-05 14:35:08 +00:00
|
|
|
{
|
2012-08-10 20:33:57 +00:00
|
|
|
const MatIndexArray& mindices = mesh.GetMaterialIndices();
|
2012-07-05 14:35:08 +00:00
|
|
|
ai_assert(mindices.size());
|
|
|
|
|
2012-08-10 20:33:57 +00:00
|
|
|
std::set<MatIndexArray::value_type> had;
|
2012-07-05 14:35:08 +00:00
|
|
|
std::vector<unsigned int> indices;
|
|
|
|
|
2012-08-10 20:33:57 +00:00
|
|
|
BOOST_FOREACH(MatIndexArray::value_type index, mindices) {
|
2012-07-06 15:00:59 +00:00
|
|
|
if(had.find(index) == had.end()) {
|
2012-07-05 14:35:08 +00:00
|
|
|
|
2012-07-27 20:36:40 +00:00
|
|
|
indices.push_back(ConvertMeshMultiMaterial(mesh, model, index, node_global_transform));
|
2012-07-05 14:35:08 +00:00
|
|
|
had.insert(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-10 20:33:57 +00:00
|
|
|
unsigned int ConvertMeshMultiMaterial(const MeshGeometry& mesh, const Model& model,
|
|
|
|
MatIndexArray::value_type index,
|
|
|
|
const aiMatrix4x4& node_global_transform)
|
2012-07-05 14:35:08 +00:00
|
|
|
{
|
2012-08-10 20:33:57 +00:00
|
|
|
aiMesh* const out_mesh = SetupEmptyMesh(mesh);
|
2012-07-06 14:48:09 +00:00
|
|
|
|
2012-08-10 20:33:57 +00:00
|
|
|
const MatIndexArray& mindices = mesh.GetMaterialIndices();
|
2012-07-05 14:35:08 +00:00
|
|
|
const std::vector<aiVector3D>& vertices = mesh.GetVertices();
|
|
|
|
const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
|
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
const bool process_weights = doc.Settings().readWeights && mesh.DeformerSkin() != NULL;
|
|
|
|
|
2012-07-05 14:35:08 +00:00
|
|
|
unsigned int count_faces = 0;
|
|
|
|
unsigned int count_vertices = 0;
|
|
|
|
|
|
|
|
// count faces
|
2012-08-10 20:33:57 +00:00
|
|
|
std::vector<unsigned int>::const_iterator itf = faces.begin();
|
|
|
|
for(MatIndexArray::const_iterator it = mindices.begin(),
|
|
|
|
end = mindices.end(); it != end; ++it, ++itf)
|
2012-07-05 14:35:08 +00:00
|
|
|
{
|
|
|
|
if ((*it) != index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++count_faces;
|
|
|
|
count_vertices += *itf;
|
|
|
|
}
|
|
|
|
|
|
|
|
ai_assert(count_faces);
|
2012-07-26 22:50:58 +00:00
|
|
|
ai_assert(count_vertices);
|
|
|
|
|
|
|
|
// mapping from output indices to DOM indexing, needed to resolve weights
|
|
|
|
std::vector<unsigned int> reverseMapping;
|
2012-07-05 14:35:08 +00:00
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
if (process_weights) {
|
|
|
|
reverseMapping.resize(count_vertices);
|
|
|
|
}
|
2012-07-05 14:35:08 +00:00
|
|
|
|
|
|
|
// allocate output data arrays, but don't fill them yet
|
|
|
|
out_mesh->mNumVertices = count_vertices;
|
|
|
|
out_mesh->mVertices = new aiVector3D[count_vertices];
|
|
|
|
|
|
|
|
out_mesh->mNumFaces = count_faces;
|
|
|
|
aiFace* fac = out_mesh->mFaces = new aiFace[count_faces]();
|
|
|
|
|
|
|
|
|
|
|
|
// allocate normals
|
|
|
|
const std::vector<aiVector3D>& normals = mesh.GetNormals();
|
|
|
|
if(normals.size()) {
|
|
|
|
ai_assert(normals.size() == vertices.size());
|
|
|
|
out_mesh->mNormals = new aiVector3D[vertices.size()];
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate tangents, binormals.
|
|
|
|
const std::vector<aiVector3D>& tangents = mesh.GetTangents();
|
|
|
|
const std::vector<aiVector3D>* binormals = &mesh.GetBinormals();
|
|
|
|
|
|
|
|
if(tangents.size()) {
|
|
|
|
std::vector<aiVector3D> tempBinormals;
|
|
|
|
if (!binormals->size()) {
|
|
|
|
if (normals.size()) {
|
|
|
|
// XXX this computes the binormals for the entire mesh, not only
|
|
|
|
// the part for which we need them.
|
|
|
|
tempBinormals.resize(normals.size());
|
|
|
|
for (unsigned int i = 0; i < tangents.size(); ++i) {
|
|
|
|
tempBinormals[i] = normals[i] ^ tangents[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
binormals = &tempBinormals;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
binormals = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(binormals) {
|
|
|
|
ai_assert(tangents.size() == vertices.size() && binormals->size() == vertices.size());
|
|
|
|
|
|
|
|
out_mesh->mTangents = new aiVector3D[vertices.size()];
|
|
|
|
out_mesh->mBitangents = new aiVector3D[vertices.size()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate texture coords
|
|
|
|
unsigned int num_uvs = 0;
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i, ++num_uvs) {
|
|
|
|
const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
|
|
|
|
if(uvs.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_mesh->mTextureCoords[i] = new aiVector3D[vertices.size()];
|
|
|
|
out_mesh->mNumUVComponents[i] = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate vertex colors
|
|
|
|
unsigned int num_vcs = 0;
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i, ++num_vcs) {
|
|
|
|
const std::vector<aiColor4D>& colors = mesh.GetVertexColors(i);
|
|
|
|
if(colors.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_mesh->mColors[i] = new aiColor4D[vertices.size()];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int cursor = 0, in_cursor = 0;
|
|
|
|
|
2012-08-10 20:33:57 +00:00
|
|
|
itf = faces.begin();
|
|
|
|
for(MatIndexArray::const_iterator it = mindices.begin(),
|
|
|
|
end = mindices.end(); it != end; ++it, ++itf)
|
2012-07-05 14:35:08 +00:00
|
|
|
{
|
|
|
|
const unsigned int pcount = *itf;
|
|
|
|
if ((*it) != index) {
|
|
|
|
in_cursor += pcount;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
aiFace& f = *fac++;
|
|
|
|
|
|
|
|
f.mNumIndices = pcount;
|
|
|
|
f.mIndices = new unsigned int[pcount];
|
|
|
|
switch(pcount)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < pcount; ++i, ++cursor, ++in_cursor) {
|
|
|
|
f.mIndices[i] = cursor;
|
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
if(reverseMapping.size()) {
|
|
|
|
reverseMapping[cursor] = in_cursor;
|
|
|
|
}
|
|
|
|
|
2012-07-05 14:35:08 +00:00
|
|
|
out_mesh->mVertices[cursor] = vertices[in_cursor];
|
|
|
|
|
|
|
|
if(out_mesh->mNormals) {
|
|
|
|
out_mesh->mNormals[cursor] = normals[in_cursor];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(out_mesh->mTangents) {
|
|
|
|
out_mesh->mTangents[cursor] = tangents[in_cursor];
|
|
|
|
out_mesh->mBitangents[cursor] = (*binormals)[in_cursor];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < num_uvs; ++i) {
|
|
|
|
const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
|
|
|
|
out_mesh->mTextureCoords[i][cursor] = aiVector3D(uvs[in_cursor].x,uvs[in_cursor].y, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < num_vcs; ++i) {
|
|
|
|
const std::vector<aiColor4D>& cols = mesh.GetVertexColors(i);
|
|
|
|
out_mesh->mColors[i][cursor] = cols[in_cursor];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConvertMaterialForMesh(out_mesh,model,mesh,index);
|
2012-07-26 21:45:07 +00:00
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
if(process_weights) {
|
2012-07-27 20:36:40 +00:00
|
|
|
ConvertWeights(out_mesh, model, mesh, node_global_transform, index, &reverseMapping);
|
2012-07-26 21:45:07 +00:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:32:41 +00:00
|
|
|
return static_cast<unsigned int>(meshes.size() - 1);
|
2012-07-04 13:07:57 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
static const unsigned int NO_MATERIAL_SEPARATION = /* std::numeric_limits<unsigned int>::max() */
|
|
|
|
static_cast<unsigned int>(-1);
|
|
|
|
|
2012-07-04 13:07:57 +00:00
|
|
|
|
2012-07-26 20:53:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-07-26 22:50:58 +00:00
|
|
|
/** - if materialIndex == NO_MATERIAL_SEPARATION, materials are not taken into
|
|
|
|
* account when determining which weights to include.
|
|
|
|
* - outputVertStartIndices is only used when a material index is specified, it gives for
|
|
|
|
* each output vertex the DOM index it maps to. */
|
|
|
|
void ConvertWeights(aiMesh* out, const Model& model, const MeshGeometry& geo,
|
2012-07-27 20:36:40 +00:00
|
|
|
const aiMatrix4x4& node_global_transform = aiMatrix4x4(),
|
2012-07-26 22:50:58 +00:00
|
|
|
unsigned int materialIndex = NO_MATERIAL_SEPARATION,
|
|
|
|
std::vector<unsigned int>* outputVertStartIndices = NULL)
|
2012-07-26 20:53:31 +00:00
|
|
|
{
|
|
|
|
ai_assert(geo.DeformerSkin());
|
|
|
|
|
|
|
|
std::vector<size_t> out_indices;
|
|
|
|
std::vector<size_t> index_out_indices;
|
|
|
|
std::vector<size_t> count_out_indices;
|
|
|
|
|
|
|
|
const Skin& sk = *geo.DeformerSkin();
|
|
|
|
|
|
|
|
std::vector<aiBone*> bones;
|
|
|
|
bones.reserve(sk.Clusters().size());
|
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
const bool no_mat_check = materialIndex == NO_MATERIAL_SEPARATION;
|
2012-07-26 23:49:02 +00:00
|
|
|
ai_assert(no_mat_check || outputVertStartIndices);
|
2012-07-26 20:53:31 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Cluster* cluster, sk.Clusters()) {
|
|
|
|
ai_assert(cluster);
|
|
|
|
|
|
|
|
const WeightIndexArray& indices = cluster->GetIndices();
|
|
|
|
|
2012-07-26 20:57:52 +00:00
|
|
|
if(indices.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-26 20:53:31 +00:00
|
|
|
const MatIndexArray& mats = geo.GetMaterialIndices();
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
const size_t no_index_sentinel = std::numeric_limits<size_t>::max();
|
|
|
|
|
|
|
|
count_out_indices.clear();
|
|
|
|
index_out_indices.clear();
|
|
|
|
out_indices.clear();
|
|
|
|
|
|
|
|
// now check if *any* of these weights is contained in the output mesh,
|
|
|
|
// taking notes so we don't need to do it twice.
|
|
|
|
BOOST_FOREACH(WeightIndexArray::value_type index, indices) {
|
|
|
|
|
2015-02-13 11:17:47 +00:00
|
|
|
unsigned int count = 0;
|
2012-07-26 20:53:31 +00:00
|
|
|
const unsigned int* const out_idx = geo.ToOutputVertexIndex(index, count);
|
2015-02-13 11:17:47 +00:00
|
|
|
// ToOutputVertexIndex only returns NULL if index is out of bounds
|
|
|
|
// which should never happen
|
|
|
|
ai_assert(out_idx != NULL);
|
2012-07-26 20:53:31 +00:00
|
|
|
|
|
|
|
index_out_indices.push_back(no_index_sentinel);
|
2012-07-26 22:50:58 +00:00
|
|
|
count_out_indices.push_back(0);
|
2012-07-26 20:53:31 +00:00
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
for(unsigned int i = 0; i < count; ++i) {
|
2013-04-08 18:13:29 +00:00
|
|
|
if (no_mat_check || static_cast<size_t>(mats[geo.FaceForVertexIndex(out_idx[i])]) == materialIndex) {
|
2012-07-26 20:53:31 +00:00
|
|
|
|
|
|
|
if (index_out_indices.back() == no_index_sentinel) {
|
|
|
|
index_out_indices.back() = out_indices.size();
|
2012-07-26 22:50:58 +00:00
|
|
|
|
2012-07-26 20:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:50:58 +00:00
|
|
|
if (no_mat_check) {
|
|
|
|
out_indices.push_back(out_idx[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// this extra lookup is in O(logn), so the entire algorithm becomes O(nlogn)
|
|
|
|
const std::vector<unsigned int>::iterator it = std::lower_bound(
|
|
|
|
outputVertStartIndices->begin(),
|
|
|
|
outputVertStartIndices->end(),
|
|
|
|
out_idx[i]
|
|
|
|
);
|
|
|
|
|
|
|
|
out_indices.push_back(std::distance(outputVertStartIndices->begin(), it));
|
|
|
|
}
|
2012-07-26 20:53:31 +00:00
|
|
|
|
|
|
|
++count_out_indices.back();
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we found at least one, generate the output bones
|
|
|
|
// XXX this could be heavily simplified by collecting the bone
|
|
|
|
// data in a single step.
|
|
|
|
if (ok) {
|
2012-07-27 20:36:40 +00:00
|
|
|
ConvertCluster(bones, model, *cluster, out_indices, index_out_indices,
|
|
|
|
count_out_indices, node_global_transform);
|
2012-07-26 20:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception&) {
|
|
|
|
std::for_each(bones.begin(),bones.end(),Util::delete_fun<aiBone>());
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bones.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->mBones = new aiBone*[bones.size()]();
|
|
|
|
out->mNumBones = static_cast<unsigned int>(bones.size());
|
|
|
|
|
|
|
|
std::swap_ranges(bones.begin(),bones.end(),out->mBones);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 20:36:40 +00:00
|
|
|
|
2012-07-26 20:53:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-08-18 00:09:06 +00:00
|
|
|
void ConvertCluster(std::vector<aiBone*>& bones, const Model& /*model*/, const Cluster& cl,
|
2012-07-26 20:53:31 +00:00
|
|
|
std::vector<size_t>& out_indices,
|
|
|
|
std::vector<size_t>& index_out_indices,
|
2012-07-27 20:36:40 +00:00
|
|
|
std::vector<size_t>& count_out_indices,
|
|
|
|
const aiMatrix4x4& node_global_transform)
|
2012-07-26 20:53:31 +00:00
|
|
|
{
|
2012-07-27 20:36:40 +00:00
|
|
|
|
2012-07-26 20:53:31 +00:00
|
|
|
aiBone* const bone = new aiBone();
|
|
|
|
bones.push_back(bone);
|
|
|
|
|
|
|
|
bone->mName = FixNodeName(cl.TargetNode()->Name());
|
2012-07-27 20:36:40 +00:00
|
|
|
|
2012-07-26 21:45:07 +00:00
|
|
|
bone->mOffsetMatrix = cl.TransformLink();
|
|
|
|
bone->mOffsetMatrix.Inverse();
|
2012-07-26 20:53:31 +00:00
|
|
|
|
2012-07-27 20:36:40 +00:00
|
|
|
bone->mOffsetMatrix = bone->mOffsetMatrix * node_global_transform;
|
|
|
|
|
2012-07-26 20:53:31 +00:00
|
|
|
bone->mNumWeights = static_cast<unsigned int>(out_indices.size());
|
|
|
|
aiVertexWeight* cursor = bone->mWeights = new aiVertexWeight[out_indices.size()];
|
|
|
|
|
|
|
|
const size_t no_index_sentinel = std::numeric_limits<size_t>::max();
|
|
|
|
const WeightArray& weights = cl.GetWeights();
|
|
|
|
|
|
|
|
const size_t c = index_out_indices.size();
|
|
|
|
for (size_t i = 0; i < c; ++i) {
|
|
|
|
const size_t index_index = index_out_indices[i];
|
|
|
|
|
|
|
|
if (index_index == no_index_sentinel) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t cc = count_out_indices[i];
|
|
|
|
for (size_t j = 0; j < cc; ++j) {
|
|
|
|
aiVertexWeight& out_weight = *cursor++;
|
|
|
|
|
|
|
|
out_weight.mVertexId = static_cast<unsigned int>(out_indices[index_index + j]);
|
|
|
|
out_weight.mWeight = weights[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-04 13:07:57 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-10 20:33:57 +00:00
|
|
|
void ConvertMaterialForMesh(aiMesh* out, const Model& model, const MeshGeometry& geo,
|
|
|
|
MatIndexArray::value_type materialIndex)
|
2012-07-04 13:07:57 +00:00
|
|
|
{
|
|
|
|
// locate source materials for this mesh
|
2012-07-04 14:33:21 +00:00
|
|
|
const std::vector<const Material*>& mats = model.GetMaterials();
|
2012-08-10 21:26:49 +00:00
|
|
|
if (static_cast<unsigned int>(materialIndex) >= mats.size() || materialIndex < 0) {
|
2012-07-05 14:37:13 +00:00
|
|
|
FBXImporter::LogError("material index out of bounds, setting default material");
|
2012-07-04 13:07:57 +00:00
|
|
|
out->mMaterialIndex = GetDefaultMaterial();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-04 14:33:21 +00:00
|
|
|
const Material* const mat = mats[materialIndex];
|
|
|
|
MaterialMap::const_iterator it = materials_converted.find(mat);
|
|
|
|
if (it != materials_converted.end()) {
|
|
|
|
out->mMaterialIndex = (*it).second;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-27 07:11:27 +00:00
|
|
|
out->mMaterialIndex = ConvertMaterial(*mat, &geo);
|
2012-07-04 14:33:21 +00:00
|
|
|
materials_converted[mat] = out->mMaterialIndex;
|
2012-07-04 13:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
unsigned int GetDefaultMaterial()
|
|
|
|
{
|
|
|
|
if (defaultMaterialIndex) {
|
|
|
|
return defaultMaterialIndex - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
aiMaterial* out_mat = new aiMaterial();
|
|
|
|
materials.push_back(out_mat);
|
|
|
|
|
|
|
|
const aiColor3D diffuse = aiColor3D(0.8f,0.8f,0.8f);
|
|
|
|
out_mat->AddProperty(&diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
|
|
|
|
aiString s;
|
|
|
|
s.Set(AI_DEFAULT_MATERIAL_NAME);
|
|
|
|
|
|
|
|
out_mat->AddProperty(&s,AI_MATKEY_NAME);
|
|
|
|
|
|
|
|
defaultMaterialIndex = static_cast<unsigned int>(materials.size());
|
|
|
|
return defaultMaterialIndex - 1;
|
2012-06-28 18:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-02 17:59:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Material -> aiMaterial
|
2014-05-27 07:11:27 +00:00
|
|
|
unsigned int ConvertMaterial(const Material& material, const MeshGeometry* const mesh)
|
2012-07-02 17:59:31 +00:00
|
|
|
{
|
|
|
|
const PropertyTable& props = material.Props();
|
|
|
|
|
|
|
|
// generate empty output material
|
|
|
|
aiMaterial* out_mat = new aiMaterial();
|
2012-07-04 14:33:21 +00:00
|
|
|
materials_converted[&material] = static_cast<unsigned int>(materials.size());
|
|
|
|
|
2012-07-02 17:59:31 +00:00
|
|
|
materials.push_back(out_mat);
|
|
|
|
|
|
|
|
aiString str;
|
|
|
|
|
2012-07-06 15:08:02 +00:00
|
|
|
// stip Material:: prefix
|
|
|
|
std::string name = material.Name();
|
|
|
|
if(name.substr(0,10) == "Material::") {
|
|
|
|
name = name.substr(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set material name if not empty - this could happen
|
|
|
|
// and there should be no key for it in this case.
|
|
|
|
if(name.length()) {
|
|
|
|
str.Set(name);
|
|
|
|
out_mat->AddProperty(&str,AI_MATKEY_NAME);
|
|
|
|
}
|
2012-07-02 17:59:31 +00:00
|
|
|
|
2012-07-03 18:41:21 +00:00
|
|
|
// shading stuff and colors
|
2012-07-02 17:59:31 +00:00
|
|
|
SetShadingPropertiesCommon(out_mat,props);
|
2012-07-03 18:41:21 +00:00
|
|
|
|
|
|
|
// texture assignments
|
2014-05-27 07:11:27 +00:00
|
|
|
SetTextureProperties(out_mat,material.Textures(), mesh);
|
|
|
|
SetTextureProperties(out_mat,material.LayeredTextures(), mesh);
|
2012-07-04 13:07:57 +00:00
|
|
|
|
|
|
|
return static_cast<unsigned int>(materials.size() - 1);
|
2012-07-03 18:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-11 00:24:59 +00:00
|
|
|
void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures,
|
|
|
|
const std::string& propName,
|
2014-05-27 07:11:27 +00:00
|
|
|
aiTextureType target, const MeshGeometry* const mesh)
|
2012-07-03 18:41:21 +00:00
|
|
|
{
|
|
|
|
TextureMap::const_iterator it = textures.find(propName);
|
|
|
|
if(it == textures.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Texture* const tex = (*it).second;
|
2014-03-31 08:30:04 +00:00
|
|
|
if(tex !=0 )
|
|
|
|
{
|
|
|
|
aiString path;
|
|
|
|
path.Set(tex->RelativeFilename());
|
|
|
|
|
|
|
|
out_mat->AddProperty(&path,_AI_MATKEY_TEXTURE_BASE,target,0);
|
|
|
|
|
|
|
|
aiUVTransform uvTrafo;
|
|
|
|
// XXX handle all kinds of UV transformations
|
|
|
|
uvTrafo.mScaling = tex->UVScaling();
|
|
|
|
uvTrafo.mTranslation = tex->UVTranslation();
|
|
|
|
out_mat->AddProperty(&uvTrafo,1,_AI_MATKEY_UVTRANSFORM_BASE,target,0);
|
|
|
|
|
|
|
|
const PropertyTable& props = tex->Props();
|
|
|
|
|
|
|
|
int uvIndex = 0;
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
const std::string& uvSet = PropertyGet<std::string>(props,"UVSet",ok);
|
|
|
|
if(ok) {
|
|
|
|
// "default" is the name which usually appears in the FbxFileTexture template
|
|
|
|
if(uvSet != "default" && uvSet.length()) {
|
|
|
|
// this is a bit awkward - we need to find a mesh that uses this
|
|
|
|
// material and scan its UV channels for the given UV name because
|
|
|
|
// assimp references UV channels by index, not by name.
|
|
|
|
|
|
|
|
// XXX: the case that UV channels may appear in different orders
|
|
|
|
// in meshes is unhandled. A possible solution would be to sort
|
|
|
|
// the UV channels alphabetically, but this would have the side
|
|
|
|
// effect that the primary (first) UV channel would sometimes
|
|
|
|
// be moved, causing trouble when users read only the first
|
|
|
|
// UV channel and ignore UV channel assignments altogether.
|
|
|
|
|
|
|
|
const unsigned int matIndex = static_cast<unsigned int>(std::distance(materials.begin(),
|
|
|
|
std::find(materials.begin(),materials.end(),out_mat)
|
|
|
|
));
|
|
|
|
|
2012-07-03 18:41:21 +00:00
|
|
|
|
2014-05-27 07:11:27 +00:00
|
|
|
uvIndex = -1;
|
|
|
|
if (!mesh)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) {
|
|
|
|
const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first);
|
|
|
|
if(!mesh) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MatIndexArray& mats = mesh->GetMaterialIndices();
|
|
|
|
if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = -1;
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
|
|
|
if(mesh->GetTextureCoords(i).empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const std::string& name = mesh->GetTextureCoordChannelName(i);
|
|
|
|
if(name == uvSet) {
|
|
|
|
index = static_cast<int>(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(index == -1) {
|
|
|
|
FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(uvIndex == -1) {
|
|
|
|
uvIndex = index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FBXImporter::LogWarn("the UV channel named " + uvSet +
|
|
|
|
" appears at different positions in meshes, results will be wrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-31 08:30:04 +00:00
|
|
|
int index = -1;
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
|
|
|
if(mesh->GetTextureCoords(i).empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const std::string& name = mesh->GetTextureCoordChannelName(i);
|
|
|
|
if(name == uvSet) {
|
|
|
|
index = static_cast<int>(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(index == -1) {
|
|
|
|
FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
|
|
|
|
}
|
2012-07-03 18:41:21 +00:00
|
|
|
|
2014-03-31 08:30:04 +00:00
|
|
|
if(uvIndex == -1) {
|
|
|
|
uvIndex = index;
|
2012-07-03 18:41:21 +00:00
|
|
|
}
|
2014-05-27 07:11:27 +00:00
|
|
|
}
|
2012-07-03 18:41:21 +00:00
|
|
|
|
|
|
|
if(uvIndex == -1) {
|
2014-03-31 08:30:04 +00:00
|
|
|
FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel");
|
|
|
|
uvIndex = 0;
|
2012-07-03 18:41:21 +00:00
|
|
|
}
|
2012-07-04 14:33:21 +00:00
|
|
|
}
|
2012-07-03 18:41:21 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 08:30:04 +00:00
|
|
|
out_mat->AddProperty(&uvIndex,1,_AI_MATKEY_UVWSRC_BASE,target,0);
|
|
|
|
}
|
2012-07-03 18:41:21 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 10:46:36 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void TrySetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures,
|
|
|
|
const std::string& propName,
|
2014-05-27 07:11:27 +00:00
|
|
|
aiTextureType target, const MeshGeometry* const mesh)
|
2014-03-24 10:46:36 +00:00
|
|
|
{
|
|
|
|
LayeredTextureMap::const_iterator it = layeredTextures.find(propName);
|
|
|
|
if(it == layeredTextures.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Texture* const tex = (*it).second->getTexture();
|
|
|
|
|
2012-07-03 18:41:21 +00:00
|
|
|
aiString path;
|
|
|
|
path.Set(tex->RelativeFilename());
|
|
|
|
|
|
|
|
out_mat->AddProperty(&path,_AI_MATKEY_TEXTURE_BASE,target,0);
|
|
|
|
|
|
|
|
aiUVTransform uvTrafo;
|
|
|
|
// XXX handle all kinds of UV transformations
|
|
|
|
uvTrafo.mScaling = tex->UVScaling();
|
|
|
|
uvTrafo.mTranslation = tex->UVTranslation();
|
|
|
|
out_mat->AddProperty(&uvTrafo,1,_AI_MATKEY_UVTRANSFORM_BASE,target,0);
|
|
|
|
|
|
|
|
const PropertyTable& props = tex->Props();
|
|
|
|
|
|
|
|
int uvIndex = 0;
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
const std::string& uvSet = PropertyGet<std::string>(props,"UVSet",ok);
|
|
|
|
if(ok) {
|
|
|
|
// "default" is the name which usually appears in the FbxFileTexture template
|
|
|
|
if(uvSet != "default" && uvSet.length()) {
|
|
|
|
// this is a bit awkward - we need to find a mesh that uses this
|
|
|
|
// material and scan its UV channels for the given UV name because
|
|
|
|
// assimp references UV channels by index, not by name.
|
|
|
|
|
|
|
|
// XXX: the case that UV channels may appear in different orders
|
|
|
|
// in meshes is unhandled. A possible solution would be to sort
|
|
|
|
// the UV channels alphabetically, but this would have the side
|
|
|
|
// effect that the primary (first) UV channel would sometimes
|
|
|
|
// be moved, causing trouble when users read only the first
|
|
|
|
// UV channel and ignore UV channel assignments altogether.
|
|
|
|
|
2012-07-26 02:04:29 +00:00
|
|
|
const unsigned int matIndex = static_cast<unsigned int>(std::distance(materials.begin(),
|
2012-07-03 18:41:21 +00:00
|
|
|
std::find(materials.begin(),materials.end(),out_mat)
|
2014-03-24 10:46:36 +00:00
|
|
|
));
|
2012-07-03 18:41:21 +00:00
|
|
|
|
2014-05-27 07:11:27 +00:00
|
|
|
uvIndex = -1;
|
|
|
|
if (!mesh)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const MeshMap::value_type& v,meshes_converted) {
|
|
|
|
const MeshGeometry* const mesh = dynamic_cast<const MeshGeometry*> (v.first);
|
|
|
|
if(!mesh) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-03 18:41:21 +00:00
|
|
|
|
2014-05-27 07:11:27 +00:00
|
|
|
const MatIndexArray& mats = mesh->GetMaterialIndices();
|
|
|
|
if(std::find(mats.begin(),mats.end(),matIndex) == mats.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-03 18:41:21 +00:00
|
|
|
|
2014-05-27 07:11:27 +00:00
|
|
|
int index = -1;
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
|
|
|
if(mesh->GetTextureCoords(i).empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const std::string& name = mesh->GetTextureCoordChannelName(i);
|
|
|
|
if(name == uvSet) {
|
|
|
|
index = static_cast<int>(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(index == -1) {
|
|
|
|
FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(uvIndex == -1) {
|
|
|
|
uvIndex = index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FBXImporter::LogWarn("the UV channel named " + uvSet +
|
|
|
|
" appears at different positions in meshes, results will be wrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-03 18:41:21 +00:00
|
|
|
int index = -1;
|
|
|
|
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
|
|
|
if(mesh->GetTextureCoords(i).empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const std::string& name = mesh->GetTextureCoordChannelName(i);
|
|
|
|
if(name == uvSet) {
|
|
|
|
index = static_cast<int>(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(index == -1) {
|
2012-07-04 14:33:21 +00:00
|
|
|
FBXImporter::LogWarn("did not find UV channel named " + uvSet + " in a mesh using this material");
|
2012-07-03 18:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(uvIndex == -1) {
|
|
|
|
uvIndex = index;
|
|
|
|
}
|
2014-05-27 07:11:27 +00:00
|
|
|
}
|
2012-07-04 14:33:21 +00:00
|
|
|
|
|
|
|
if(uvIndex == -1) {
|
|
|
|
FBXImporter::LogWarn("failed to resolve UV channel " + uvSet + ", using first UV channel");
|
|
|
|
uvIndex = 0;
|
|
|
|
}
|
2012-07-03 18:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out_mat->AddProperty(&uvIndex,1,_AI_MATKEY_UVWSRC_BASE,target,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-05-27 07:11:27 +00:00
|
|
|
void SetTextureProperties(aiMaterial* out_mat, const TextureMap& textures, const MeshGeometry* const mesh)
|
2012-07-03 18:41:21 +00:00
|
|
|
{
|
2014-05-27 07:11:27 +00:00
|
|
|
TrySetTextureProperties(out_mat, textures, "DiffuseColor", aiTextureType_DIFFUSE, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "AmbientColor", aiTextureType_AMBIENT, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "EmissiveColor", aiTextureType_EMISSIVE, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "SpecularColor", aiTextureType_SPECULAR, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "TransparentColor", aiTextureType_OPACITY, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "ReflectionColor", aiTextureType_REFLECTION, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "DisplacementColor", aiTextureType_DISPLACEMENT, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "NormalMap", aiTextureType_NORMALS, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "Bump", aiTextureType_HEIGHT, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, textures, "ShininessExponent", aiTextureType_SHININESS, mesh);
|
2012-07-02 17:59:31 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 10:46:36 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-05-27 07:11:27 +00:00
|
|
|
void SetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures, const MeshGeometry* const mesh)
|
2014-03-24 10:46:36 +00:00
|
|
|
{
|
2014-05-27 07:11:27 +00:00
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "DiffuseColor", aiTextureType_DIFFUSE, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "AmbientColor", aiTextureType_AMBIENT, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "EmissiveColor", aiTextureType_EMISSIVE, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "SpecularColor", aiTextureType_SPECULAR, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "TransparentColor", aiTextureType_OPACITY, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "ReflectionColor", aiTextureType_REFLECTION, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "DisplacementColor", aiTextureType_DISPLACEMENT, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "NormalMap", aiTextureType_NORMALS, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "Bump", aiTextureType_HEIGHT, mesh);
|
|
|
|
TrySetTextureProperties(out_mat, layeredTextures, "ShininessExponent", aiTextureType_SHININESS, mesh);
|
2014-03-24 10:46:36 +00:00
|
|
|
}
|
2012-07-02 21:31:48 +00:00
|
|
|
|
2012-07-03 18:41:21 +00:00
|
|
|
|
2012-07-02 17:59:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-11 00:24:59 +00:00
|
|
|
aiColor3D GetColorPropertyFromMaterial(const PropertyTable& props, const std::string& baseName,
|
|
|
|
bool& result)
|
2012-07-02 17:59:31 +00:00
|
|
|
{
|
2012-07-02 21:31:48 +00:00
|
|
|
result = true;
|
|
|
|
|
2012-07-02 17:59:31 +00:00
|
|
|
bool ok;
|
2012-07-02 21:31:48 +00:00
|
|
|
const aiVector3D& Diffuse = PropertyGet<aiVector3D>(props,baseName,ok);
|
2012-07-02 17:59:31 +00:00
|
|
|
if(ok) {
|
2012-07-02 21:31:48 +00:00
|
|
|
return aiColor3D(Diffuse.x,Diffuse.y,Diffuse.z);
|
2012-07-02 17:59:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-02 21:31:48 +00:00
|
|
|
aiVector3D DiffuseColor = PropertyGet<aiVector3D>(props,baseName + "Color",ok);
|
2012-07-02 17:59:31 +00:00
|
|
|
if(ok) {
|
2012-07-02 21:31:48 +00:00
|
|
|
float DiffuseFactor = PropertyGet<float>(props,baseName + "Factor",ok);
|
2012-07-02 17:59:31 +00:00
|
|
|
if(ok) {
|
|
|
|
DiffuseColor *= DiffuseFactor;
|
|
|
|
}
|
|
|
|
|
2012-07-02 21:31:48 +00:00
|
|
|
return aiColor3D(DiffuseColor.x,DiffuseColor.y,DiffuseColor.z);
|
2012-07-02 17:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-02 21:31:48 +00:00
|
|
|
result = false;
|
|
|
|
return aiColor3D(0.0f,0.0f,0.0f);
|
|
|
|
}
|
2012-07-02 17:59:31 +00:00
|
|
|
|
2012-07-02 21:31:48 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void SetShadingPropertiesCommon(aiMaterial* out_mat, const PropertyTable& props)
|
|
|
|
{
|
|
|
|
// set shading properties. There are various, redundant ways in which FBX materials
|
|
|
|
// specify their shading settings (depending on shading models, prop
|
|
|
|
// template etc.). No idea which one is right in a particular context.
|
|
|
|
// Just try to make sense of it - there's no spec to verify this against,
|
|
|
|
// so why should we.
|
|
|
|
bool ok;
|
|
|
|
const aiColor3D& Diffuse = GetColorPropertyFromMaterial(props,"Diffuse",ok);
|
|
|
|
if(ok) {
|
2012-07-05 12:26:26 +00:00
|
|
|
out_mat->AddProperty(&Diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
|
2012-07-02 21:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const aiColor3D& Emissive = GetColorPropertyFromMaterial(props,"Emissive",ok);
|
2012-07-02 17:59:31 +00:00
|
|
|
if(ok) {
|
|
|
|
out_mat->AddProperty(&Emissive,1,AI_MATKEY_COLOR_EMISSIVE);
|
|
|
|
}
|
|
|
|
|
2012-07-02 21:31:48 +00:00
|
|
|
const aiColor3D& Ambient = GetColorPropertyFromMaterial(props,"Ambient",ok);
|
2012-07-02 17:59:31 +00:00
|
|
|
if(ok) {
|
|
|
|
out_mat->AddProperty(&Ambient,1,AI_MATKEY_COLOR_AMBIENT);
|
|
|
|
}
|
|
|
|
|
2012-07-02 21:31:48 +00:00
|
|
|
const aiColor3D& Specular = GetColorPropertyFromMaterial(props,"Specular",ok);
|
2012-07-02 17:59:31 +00:00
|
|
|
if(ok) {
|
|
|
|
out_mat->AddProperty(&Specular,1,AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const float Opacity = PropertyGet<float>(props,"Opacity",ok);
|
|
|
|
if(ok) {
|
|
|
|
out_mat->AddProperty(&Opacity,1,AI_MATKEY_OPACITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
const float Reflectivity = PropertyGet<float>(props,"Reflectivity",ok);
|
|
|
|
if(ok) {
|
|
|
|
out_mat->AddProperty(&Reflectivity,1,AI_MATKEY_REFLECTIVITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
const float Shininess = PropertyGet<float>(props,"Shininess",ok);
|
|
|
|
if(ok) {
|
|
|
|
out_mat->AddProperty(&Shininess,1,AI_MATKEY_SHININESS_STRENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
const float ShininessExponent = PropertyGet<float>(props,"ShininessExponent",ok);
|
|
|
|
if(ok) {
|
|
|
|
out_mat->AddProperty(&ShininessExponent,1,AI_MATKEY_SHININESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-25 15:31:15 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// get the number of fps for a FrameRate enumerated value
|
|
|
|
static double FrameRateToDouble(FileGlobalSettings::FrameRate fp, double customFPSVal = -1.0)
|
|
|
|
{
|
|
|
|
switch(fp) {
|
|
|
|
case FileGlobalSettings::FrameRate_DEFAULT:
|
|
|
|
return 1.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_120:
|
|
|
|
return 120.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_100:
|
|
|
|
return 100.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_60:
|
|
|
|
return 60.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_50:
|
|
|
|
return 50.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_48:
|
|
|
|
return 48.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_30:
|
|
|
|
case FileGlobalSettings::FrameRate_30_DROP:
|
|
|
|
return 30.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_NTSC_DROP_FRAME:
|
|
|
|
case FileGlobalSettings::FrameRate_NTSC_FULL_FRAME:
|
|
|
|
return 29.9700262;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_PAL:
|
|
|
|
return 25.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_CINEMA:
|
|
|
|
return 24.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_1000:
|
|
|
|
return 1000.0;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_CINEMA_ND:
|
|
|
|
return 23.976;
|
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_CUSTOM:
|
|
|
|
return customFPSVal;
|
2013-04-08 17:45:09 +00:00
|
|
|
|
|
|
|
case FileGlobalSettings::FrameRate_MAX: // this is to silence compiler warnings
|
|
|
|
break;
|
2012-08-25 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ai_assert(false);
|
|
|
|
return -1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// convert animation data to aiAnimation et al
|
|
|
|
void ConvertAnimations()
|
|
|
|
{
|
2012-08-25 15:31:15 +00:00
|
|
|
// first of all determine framerate
|
|
|
|
const FileGlobalSettings::FrameRate fps = doc.GlobalSettings().TimeMode();
|
|
|
|
const float custom = doc.GlobalSettings().CustomFrameRate();
|
|
|
|
anim_fps = FrameRateToDouble(fps, custom);
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
const std::vector<const AnimationStack*>& animations = doc.AnimationStacks();
|
|
|
|
BOOST_FOREACH(const AnimationStack* stack, animations) {
|
|
|
|
ConvertAnimationStack(*stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-25 20:24:08 +00:00
|
|
|
// rename a node already partially converted. fixed_name is a string previously returned by
|
|
|
|
// FixNodeName, new_name specifies the string FixNodeName should return on all further invocations
|
|
|
|
// which would previously have returned the old value.
|
|
|
|
//
|
|
|
|
// this also updates names in node animations, cameras and light sources and is thus slow.
|
|
|
|
//
|
|
|
|
// NOTE: the caller is responsible for ensuring that the new name is unique and does
|
|
|
|
// not collide with any other identifiers. The best way to ensure this is to only
|
|
|
|
// append to the old name, which is guaranteed to match these requirements.
|
|
|
|
void RenameNode(const std::string& fixed_name, const std::string& new_name)
|
|
|
|
{
|
|
|
|
ai_assert(node_names.find(fixed_name) != node_names.end());
|
|
|
|
ai_assert(node_names.find(new_name) == node_names.end());
|
|
|
|
|
|
|
|
renamed_nodes[fixed_name] = new_name;
|
|
|
|
|
|
|
|
const aiString fn(fixed_name);
|
|
|
|
|
|
|
|
BOOST_FOREACH(aiCamera* cam, cameras) {
|
|
|
|
if (cam->mName == fn) {
|
|
|
|
cam->mName.Set(new_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(aiLight* light, lights) {
|
|
|
|
if (light->mName == fn) {
|
|
|
|
light->mName.Set(new_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(aiAnimation* anim, animations) {
|
|
|
|
for (unsigned int i = 0; i < anim->mNumChannels; ++i) {
|
|
|
|
aiNodeAnim* const na = anim->mChannels[i];
|
|
|
|
if (na->mNodeName == fn) {
|
|
|
|
na->mNodeName.Set(new_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// takes a fbx node name and returns the identifier to be used in the assimp output scene.
|
|
|
|
// the function is guaranteed to provide consistent results over multiple invocations
|
|
|
|
// UNLESS RenameNode() is called for a particular node name.
|
2012-07-21 02:15:10 +00:00
|
|
|
std::string FixNodeName(const std::string& name)
|
|
|
|
{
|
2012-07-22 00:12:38 +00:00
|
|
|
// strip Model:: prefix, avoiding ambiguities (i.e. don't strip if
|
|
|
|
// this causes ambiguities, well possible between empty identifiers,
|
|
|
|
// such as "Model::" and ""). Make sure the behaviour is consistent
|
|
|
|
// across multiple calls to FixNodeName().
|
|
|
|
if(name.substr(0,7) == "Model::") {
|
|
|
|
std::string temp = name.substr(7);
|
|
|
|
|
|
|
|
const NodeNameMap::const_iterator it = node_names.find(temp);
|
|
|
|
if (it != node_names.end()) {
|
|
|
|
if (!(*it).second) {
|
|
|
|
return FixNodeName(name + "_");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node_names[temp] = true;
|
2012-08-25 20:24:08 +00:00
|
|
|
|
|
|
|
const NameNameMap::const_iterator rit = renamed_nodes.find(temp);
|
|
|
|
return rit == renamed_nodes.end() ? temp : (*rit).second;
|
2012-07-22 00:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const NodeNameMap::const_iterator it = node_names.find(name);
|
|
|
|
if (it != node_names.end()) {
|
|
|
|
if ((*it).second) {
|
|
|
|
return FixNodeName(name + "_");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node_names[name] = false;
|
2012-08-25 20:24:08 +00:00
|
|
|
|
|
|
|
const NameNameMap::const_iterator rit = renamed_nodes.find(name);
|
|
|
|
return rit == renamed_nodes.end() ? name : (*rit).second;
|
2012-07-21 02:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::map<const AnimationCurveNode*, const AnimationLayer*> LayerMap;
|
|
|
|
|
2012-07-27 02:55:56 +00:00
|
|
|
// XXX: better use multi_map ..
|
|
|
|
typedef std::map<std::string, std::vector<const AnimationCurveNode*> > NodeMap;
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ConvertAnimationStack(const AnimationStack& st)
|
2012-07-26 02:13:16 +00:00
|
|
|
{
|
|
|
|
const AnimationLayerList& layers = st.Layers();
|
|
|
|
if(layers.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
aiAnimation* const anim = new aiAnimation();
|
|
|
|
animations.push_back(anim);
|
|
|
|
|
|
|
|
// strip AnimationStack:: prefix
|
|
|
|
std::string name = st.Name();
|
|
|
|
if(name.substr(0,16) == "AnimationStack::") {
|
|
|
|
name = name.substr(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
anim->mName.Set(name);
|
|
|
|
|
|
|
|
// need to find all nodes for which we need to generate node animations -
|
|
|
|
// it may happen that we need to merge multiple layers, though.
|
|
|
|
NodeMap node_map;
|
|
|
|
|
|
|
|
// reverse mapping from curves to layers, much faster than querying
|
|
|
|
// the FBX DOM for it.
|
|
|
|
LayerMap layer_map;
|
2012-08-26 16:40:04 +00:00
|
|
|
|
|
|
|
const char* prop_whitelist[] = {
|
|
|
|
"Lcl Scaling",
|
|
|
|
"Lcl Rotation",
|
|
|
|
"Lcl Translation"
|
|
|
|
};
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
BOOST_FOREACH(const AnimationLayer* layer, layers) {
|
|
|
|
ai_assert(layer);
|
|
|
|
|
2012-08-26 16:40:04 +00:00
|
|
|
const AnimationCurveNodeList& nodes = layer->Nodes(prop_whitelist, 3);
|
2012-07-21 02:15:10 +00:00
|
|
|
BOOST_FOREACH(const AnimationCurveNode* node, nodes) {
|
|
|
|
ai_assert(node);
|
|
|
|
|
2012-07-26 01:58:42 +00:00
|
|
|
const Model* const model = dynamic_cast<const Model*>(node->Target());
|
|
|
|
// this can happen - it could also be a NodeAttribute (i.e. for camera animations)
|
|
|
|
if(!model) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
const std::string& name = FixNodeName(model->Name());
|
|
|
|
node_map[name].push_back(node);
|
|
|
|
|
|
|
|
layer_map[node] = layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate node animations
|
|
|
|
std::vector<aiNodeAnim*> node_anims;
|
|
|
|
|
2012-07-21 21:48:52 +00:00
|
|
|
double min_time = 1e10;
|
|
|
|
double max_time = -1e10;
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
try {
|
|
|
|
BOOST_FOREACH(const NodeMap::value_type& kv, node_map) {
|
2012-07-27 13:53:17 +00:00
|
|
|
GenerateNodeAnimations(node_anims,
|
|
|
|
kv.first,
|
|
|
|
kv.second,
|
|
|
|
layer_map,
|
2012-07-27 15:57:19 +00:00
|
|
|
max_time,
|
|
|
|
min_time);
|
2012-07-27 13:53:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(std::exception&) {
|
|
|
|
std::for_each(node_anims.begin(), node_anims.end(), Util::delete_fun<aiNodeAnim>());
|
|
|
|
throw;
|
|
|
|
}
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
if(node_anims.size()) {
|
|
|
|
anim->mChannels = new aiNodeAnim*[node_anims.size()]();
|
|
|
|
anim->mNumChannels = static_cast<unsigned int>(node_anims.size());
|
2012-07-21 20:29:42 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
std::swap_ranges(node_anims.begin(),node_anims.end(),anim->mChannels);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// empty animations would fail validation, so drop them
|
|
|
|
delete anim;
|
|
|
|
animations.pop_back();
|
2012-08-26 15:45:09 +00:00
|
|
|
FBXImporter::LogInfo("ignoring empty AnimationStack (using IK?): " + name);
|
2012-07-27 13:53:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
// for some mysterious reason, mDuration is simply the maximum key -- the
|
|
|
|
// validator always assumes animations to start at zero.
|
|
|
|
anim->mDuration = max_time /*- min_time */;
|
2012-08-25 15:31:15 +00:00
|
|
|
anim->mTicksPerSecond = anim_fps;
|
2012-07-27 13:53:17 +00:00
|
|
|
}
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void GenerateNodeAnimations(std::vector<aiNodeAnim*>& node_anims,
|
|
|
|
const std::string& fixed_name,
|
|
|
|
const std::vector<const AnimationCurveNode*>& curves,
|
|
|
|
const LayerMap& layer_map,
|
|
|
|
double& max_time,
|
|
|
|
double& min_time)
|
|
|
|
{
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
NodeMap node_property_map;
|
|
|
|
ai_assert(curves.size());
|
2012-07-21 20:29:42 +00:00
|
|
|
|
2012-07-27 16:45:02 +00:00
|
|
|
// sanity check whether the input is ok
|
2013-11-21 12:05:48 +00:00
|
|
|
#ifdef ASSIMP_BUILD_DEBUG
|
2012-07-27 16:45:02 +00:00
|
|
|
{ const Object* target = NULL;
|
|
|
|
BOOST_FOREACH(const AnimationCurveNode* node, curves) {
|
|
|
|
if(!target) {
|
|
|
|
target = node->Target();
|
|
|
|
}
|
|
|
|
ai_assert(node->Target() == target);
|
|
|
|
}}
|
|
|
|
#endif
|
|
|
|
|
2013-08-09 23:52:15 +00:00
|
|
|
const AnimationCurveNode* curve_node = NULL;
|
2012-07-27 13:53:17 +00:00
|
|
|
BOOST_FOREACH(const AnimationCurveNode* node, curves) {
|
|
|
|
ai_assert(node);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
if (node->TargetProperty().empty()) {
|
2012-08-09 00:10:13 +00:00
|
|
|
FBXImporter::LogWarn("target property for animation curve not set: " + node->Name());
|
2012-07-27 13:53:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
curve_node = node;
|
|
|
|
if (node->Curves().empty()) {
|
2012-08-09 00:10:13 +00:00
|
|
|
FBXImporter::LogWarn("no animation curves assigned to AnimationCurveNode: " + node->Name());
|
2012-07-27 13:53:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-27 13:42:06 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
node_property_map[node->TargetProperty()].push_back(node);
|
|
|
|
}
|
2012-07-27 13:42:06 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
ai_assert(curve_node);
|
2012-07-27 16:45:02 +00:00
|
|
|
ai_assert(curve_node->TargetAsModel());
|
|
|
|
|
|
|
|
const Model& target = *curve_node->TargetAsModel();
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
// check for all possible transformation components
|
|
|
|
NodeMap::const_iterator chain[TransformationComp_MAXIMUM];
|
2012-07-26 01:11:14 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
bool has_any = false;
|
|
|
|
bool has_complex = false;
|
2012-07-21 20:29:42 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i) {
|
|
|
|
const TransformationComp comp = static_cast<TransformationComp>(i);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
// inverse pivots don't exist in the input, we just generate them
|
|
|
|
if (comp == TransformationComp_RotationPivotInverse || comp == TransformationComp_ScalingPivotInverse) {
|
|
|
|
chain[i] = node_property_map.end();
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-21 20:29:42 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
chain[i] = node_property_map.find(NameTransformationCompProperty(comp));
|
|
|
|
if (chain[i] != node_property_map.end()) {
|
2012-07-27 16:45:02 +00:00
|
|
|
|
|
|
|
// check if this curves contains redundant information by looking
|
|
|
|
// up the corresponding node's transformation chain.
|
2012-08-11 00:24:59 +00:00
|
|
|
if (doc.Settings().optimizeEmptyAnimationCurves &&
|
|
|
|
IsRedundantAnimationData(target, comp, (*chain[i]).second)) {
|
|
|
|
|
2012-07-27 16:45:02 +00:00
|
|
|
FBXImporter::LogDebug("dropping redundant animation channel for node " + target.Name());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
has_any = true;
|
2012-07-27 02:55:56 +00:00
|
|
|
|
2013-12-29 19:02:16 +00:00
|
|
|
if (comp != TransformationComp_Rotation && comp != TransformationComp_Scaling && comp != TransformationComp_Translation &&
|
|
|
|
comp != TransformationComp_GeometricScaling && comp != TransformationComp_GeometricRotation && comp != TransformationComp_GeometricTranslation )
|
|
|
|
{
|
2012-07-27 14:18:47 +00:00
|
|
|
has_complex = true;
|
2012-07-21 02:15:10 +00:00
|
|
|
}
|
2012-07-27 13:53:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-21 20:29:42 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
if (!has_any) {
|
|
|
|
FBXImporter::LogWarn("ignoring node animation, did not find any transformation key frames");
|
|
|
|
return;
|
|
|
|
}
|
2012-07-27 13:31:02 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
// this needs to play nicely with GenerateTransformationNodeChain() which will
|
|
|
|
// be invoked _later_ (animations come first). If this node has only rotation,
|
|
|
|
// scaling and translation _and_ there are no animated other components either,
|
|
|
|
// we can use a single node and also a single node animation channel.
|
|
|
|
if (!has_complex && !NeedsComplexTransformationChain(target)) {
|
2012-07-27 13:31:02 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
aiNodeAnim* const nd = GenerateSimpleNodeAnim(fixed_name, target, chain,
|
|
|
|
node_property_map.end(),
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
2012-08-20 16:33:54 +00:00
|
|
|
min_time,
|
|
|
|
true // input is TRS order, assimp is SRT
|
2012-07-27 13:53:17 +00:00
|
|
|
);
|
2012-07-27 13:31:02 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
ai_assert(nd);
|
|
|
|
node_anims.push_back(nd);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-27 13:31:02 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
// otherwise, things get gruesome and we need separate animation channels
|
2012-07-27 14:18:47 +00:00
|
|
|
// for each part of the transformation chain. Remember which channels
|
|
|
|
// we generated and pass this information to the node conversion
|
|
|
|
// code to avoid nodes that have identity transform, but non-identity
|
|
|
|
// animations, being dropped.
|
|
|
|
unsigned int flags = 0, bit = 0x1;
|
|
|
|
for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i, bit <<= 1) {
|
2012-07-27 13:53:17 +00:00
|
|
|
const TransformationComp comp = static_cast<TransformationComp>(i);
|
|
|
|
|
|
|
|
if (chain[i] != node_property_map.end()) {
|
2012-07-27 14:18:47 +00:00
|
|
|
flags |= bit;
|
|
|
|
|
|
|
|
ai_assert(comp != TransformationComp_RotationPivotInverse);
|
|
|
|
ai_assert(comp != TransformationComp_ScalingPivotInverse);
|
2012-07-27 13:53:17 +00:00
|
|
|
|
|
|
|
const std::string& chain_name = NameTransformationChainNode(fixed_name, comp);
|
|
|
|
|
|
|
|
aiNodeAnim* na;
|
|
|
|
switch(comp)
|
|
|
|
{
|
|
|
|
case TransformationComp_Rotation:
|
|
|
|
case TransformationComp_PreRotation:
|
|
|
|
case TransformationComp_PostRotation:
|
2013-12-29 19:02:16 +00:00
|
|
|
case TransformationComp_GeometricRotation:
|
2012-07-27 13:53:17 +00:00
|
|
|
na = GenerateRotationNodeAnim(chain_name,
|
|
|
|
target,
|
|
|
|
(*chain[i]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
2012-08-09 00:10:13 +00:00
|
|
|
min_time);
|
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TransformationComp_RotationOffset:
|
|
|
|
case TransformationComp_RotationPivot:
|
|
|
|
case TransformationComp_ScalingOffset:
|
|
|
|
case TransformationComp_ScalingPivot:
|
|
|
|
case TransformationComp_Translation:
|
2013-12-29 19:02:16 +00:00
|
|
|
case TransformationComp_GeometricTranslation:
|
2012-07-27 13:53:17 +00:00
|
|
|
na = GenerateTranslationNodeAnim(chain_name,
|
|
|
|
target,
|
|
|
|
(*chain[i]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
|
|
|
min_time);
|
|
|
|
|
2012-07-27 14:18:47 +00:00
|
|
|
// pivoting requires us to generate an implicit inverse channel to undo the pivot translation
|
2012-07-27 13:53:17 +00:00
|
|
|
if (comp == TransformationComp_RotationPivot) {
|
2012-08-11 00:24:59 +00:00
|
|
|
const std::string& invName = NameTransformationChainNode(fixed_name,
|
|
|
|
TransformationComp_RotationPivotInverse);
|
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
aiNodeAnim* const inv = GenerateTranslationNodeAnim(invName,
|
|
|
|
target,
|
|
|
|
(*chain[i]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
|
|
|
min_time,
|
|
|
|
true);
|
|
|
|
|
|
|
|
ai_assert(inv);
|
|
|
|
node_anims.push_back(inv);
|
2012-07-27 14:18:47 +00:00
|
|
|
|
|
|
|
ai_assert(TransformationComp_RotationPivotInverse > i);
|
|
|
|
flags |= bit << (TransformationComp_RotationPivotInverse - i);
|
2012-07-27 13:53:17 +00:00
|
|
|
}
|
|
|
|
else if (comp == TransformationComp_ScalingPivot) {
|
2012-08-11 00:24:59 +00:00
|
|
|
const std::string& invName = NameTransformationChainNode(fixed_name,
|
|
|
|
TransformationComp_ScalingPivotInverse);
|
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
aiNodeAnim* const inv = GenerateTranslationNodeAnim(invName,
|
|
|
|
target,
|
|
|
|
(*chain[i]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
|
|
|
min_time,
|
|
|
|
true);
|
|
|
|
|
|
|
|
ai_assert(inv);
|
|
|
|
node_anims.push_back(inv);
|
2012-07-27 14:18:47 +00:00
|
|
|
|
|
|
|
ai_assert(TransformationComp_RotationPivotInverse > i);
|
|
|
|
flags |= bit << (TransformationComp_RotationPivotInverse - i);
|
2012-07-27 13:31:02 +00:00
|
|
|
}
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
break;
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
case TransformationComp_Scaling:
|
2013-12-29 19:02:16 +00:00
|
|
|
case TransformationComp_GeometricScaling:
|
2012-07-27 13:53:17 +00:00
|
|
|
na = GenerateScalingNodeAnim(chain_name,
|
|
|
|
target,
|
|
|
|
(*chain[i]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
2012-08-09 00:10:13 +00:00
|
|
|
min_time);
|
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
break;
|
2012-07-21 21:48:52 +00:00
|
|
|
|
2012-07-27 13:53:17 +00:00
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ai_assert(na);
|
|
|
|
node_anims.push_back(na);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-07-27 14:18:47 +00:00
|
|
|
|
|
|
|
node_anim_chain_bits[fixed_name] = flags;
|
2012-07-21 02:15:10 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 02:55:56 +00:00
|
|
|
|
2012-07-27 16:45:02 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool IsRedundantAnimationData(const Model& target,
|
|
|
|
TransformationComp comp,
|
|
|
|
const std::vector<const AnimationCurveNode*>& curves)
|
|
|
|
{
|
|
|
|
ai_assert(curves.size());
|
|
|
|
|
|
|
|
// look for animation nodes with
|
|
|
|
// * sub channels for all relevant components set
|
|
|
|
// * one key/value pair per component
|
|
|
|
// * combined values match up the corresponding value in the bind pose node transformation
|
|
|
|
// only such nodes are 'redundant' for this function.
|
|
|
|
|
|
|
|
if (curves.size() > 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AnimationCurveNode& nd = *curves.front();
|
|
|
|
const AnimationCurveMap& sub_curves = nd.Curves();
|
|
|
|
|
|
|
|
const AnimationCurveMap::const_iterator dx = sub_curves.find("d|X");
|
|
|
|
const AnimationCurveMap::const_iterator dy = sub_curves.find("d|Y");
|
|
|
|
const AnimationCurveMap::const_iterator dz = sub_curves.find("d|Z");
|
|
|
|
|
|
|
|
if (dx == sub_curves.end() || dy == sub_curves.end() || dz == sub_curves.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KeyValueList& vx = (*dx).second->GetValues();
|
|
|
|
const KeyValueList& vy = (*dy).second->GetValues();
|
|
|
|
const KeyValueList& vz = (*dz).second->GetValues();
|
|
|
|
|
|
|
|
if(vx.size() != 1 || vy.size() != 1 || vz.size() != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const aiVector3D dyn_val = aiVector3D(vx[0], vy[0], vz[0]);
|
|
|
|
const aiVector3D& static_val = PropertyGet<aiVector3D>(target.Props(),
|
|
|
|
NameTransformationCompProperty(comp),
|
|
|
|
TransformationCompDefaultValue(comp)
|
|
|
|
);
|
|
|
|
|
|
|
|
const float epsilon = 1e-6f;
|
|
|
|
return (dyn_val - static_val).SquareLength() < epsilon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 13:31:02 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiNodeAnim* GenerateRotationNodeAnim(const std::string& name,
|
|
|
|
const Model& target,
|
|
|
|
const std::vector<const AnimationCurveNode*>& curves,
|
|
|
|
const LayerMap& layer_map,
|
|
|
|
double& max_time,
|
|
|
|
double& min_time)
|
|
|
|
{
|
|
|
|
ScopeGuard<aiNodeAnim> na(new aiNodeAnim());
|
|
|
|
na->mNodeName.Set(name);
|
|
|
|
|
2012-08-11 00:17:22 +00:00
|
|
|
ConvertRotationKeys(na, curves, layer_map, max_time,min_time, target.RotationOrder());
|
2012-07-27 13:31:02 +00:00
|
|
|
|
|
|
|
// dummy scaling key
|
|
|
|
na->mScalingKeys = new aiVectorKey[1];
|
|
|
|
na->mNumScalingKeys = 1;
|
|
|
|
|
|
|
|
na->mScalingKeys[0].mTime = 0.;
|
|
|
|
na->mScalingKeys[0].mValue = aiVector3D(1.0f,1.0f,1.0f);
|
|
|
|
|
|
|
|
// dummy position key
|
|
|
|
na->mPositionKeys = new aiVectorKey[1];
|
|
|
|
na->mNumPositionKeys = 1;
|
|
|
|
|
|
|
|
na->mPositionKeys[0].mTime = 0.;
|
|
|
|
na->mPositionKeys[0].mValue = aiVector3D();
|
|
|
|
|
|
|
|
return na.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiNodeAnim* GenerateScalingNodeAnim(const std::string& name,
|
2014-08-18 00:09:06 +00:00
|
|
|
const Model& /*target*/,
|
2012-07-27 13:31:02 +00:00
|
|
|
const std::vector<const AnimationCurveNode*>& curves,
|
|
|
|
const LayerMap& layer_map,
|
|
|
|
double& max_time,
|
|
|
|
double& min_time)
|
|
|
|
{
|
|
|
|
ScopeGuard<aiNodeAnim> na(new aiNodeAnim());
|
|
|
|
na->mNodeName.Set(name);
|
|
|
|
|
|
|
|
ConvertScaleKeys(na, curves, layer_map, max_time,min_time);
|
|
|
|
|
|
|
|
// dummy rotation key
|
|
|
|
na->mRotationKeys = new aiQuatKey[1];
|
|
|
|
na->mNumRotationKeys = 1;
|
|
|
|
|
|
|
|
na->mRotationKeys[0].mTime = 0.;
|
|
|
|
na->mRotationKeys[0].mValue = aiQuaternion();
|
|
|
|
|
|
|
|
// dummy position key
|
|
|
|
na->mPositionKeys = new aiVectorKey[1];
|
|
|
|
na->mNumPositionKeys = 1;
|
|
|
|
|
|
|
|
na->mPositionKeys[0].mTime = 0.;
|
|
|
|
na->mPositionKeys[0].mValue = aiVector3D();
|
|
|
|
|
|
|
|
return na.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiNodeAnim* GenerateTranslationNodeAnim(const std::string& name,
|
2014-08-18 00:09:06 +00:00
|
|
|
const Model& /*target*/,
|
2012-07-27 13:31:02 +00:00
|
|
|
const std::vector<const AnimationCurveNode*>& curves,
|
|
|
|
const LayerMap& layer_map,
|
|
|
|
double& max_time,
|
2012-07-27 13:42:06 +00:00
|
|
|
double& min_time,
|
|
|
|
bool inverse = false)
|
2012-07-27 13:31:02 +00:00
|
|
|
{
|
|
|
|
ScopeGuard<aiNodeAnim> na(new aiNodeAnim());
|
|
|
|
na->mNodeName.Set(name);
|
|
|
|
|
|
|
|
ConvertTranslationKeys(na, curves, layer_map, max_time,min_time);
|
|
|
|
|
2012-07-27 13:42:06 +00:00
|
|
|
if (inverse) {
|
|
|
|
for (unsigned int i = 0; i < na->mNumPositionKeys; ++i) {
|
|
|
|
na->mPositionKeys[i].mValue *= -1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 13:31:02 +00:00
|
|
|
// dummy scaling key
|
|
|
|
na->mScalingKeys = new aiVectorKey[1];
|
|
|
|
na->mNumScalingKeys = 1;
|
|
|
|
|
|
|
|
na->mScalingKeys[0].mTime = 0.;
|
|
|
|
na->mScalingKeys[0].mValue = aiVector3D(1.0f,1.0f,1.0f);
|
|
|
|
|
|
|
|
// dummy rotation key
|
|
|
|
na->mRotationKeys = new aiQuatKey[1];
|
|
|
|
na->mNumRotationKeys = 1;
|
|
|
|
|
|
|
|
na->mRotationKeys[0].mTime = 0.;
|
|
|
|
na->mRotationKeys[0].mValue = aiQuaternion();
|
|
|
|
|
|
|
|
return na.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-27 02:55:56 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// generate node anim, extracting only Rotation, Scaling and Translation from the given chain
|
|
|
|
aiNodeAnim* GenerateSimpleNodeAnim(const std::string& name,
|
|
|
|
const Model& target,
|
|
|
|
NodeMap::const_iterator chain[TransformationComp_MAXIMUM],
|
|
|
|
NodeMap::const_iterator iter_end,
|
|
|
|
const LayerMap& layer_map,
|
|
|
|
double& max_time,
|
2012-08-20 16:33:54 +00:00
|
|
|
double& min_time,
|
|
|
|
bool reverse_order = false)
|
2012-07-27 02:55:56 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
ScopeGuard<aiNodeAnim> na(new aiNodeAnim());
|
|
|
|
na->mNodeName.Set(name);
|
|
|
|
|
|
|
|
const PropertyTable& props = target.Props();
|
|
|
|
|
2012-08-20 17:15:59 +00:00
|
|
|
// need to convert from TRS order to SRT?
|
2012-08-20 16:33:54 +00:00
|
|
|
if(reverse_order) {
|
2012-08-20 17:15:59 +00:00
|
|
|
|
|
|
|
aiVector3D def_scale, def_translate;
|
|
|
|
aiQuaternion def_rot;
|
|
|
|
|
|
|
|
KeyFrameListList scaling;
|
|
|
|
KeyFrameListList translation;
|
|
|
|
KeyFrameListList rotation;
|
|
|
|
|
|
|
|
if(chain[TransformationComp_Scaling] != iter_end) {
|
|
|
|
scaling = GetKeyframeList((*chain[TransformationComp_Scaling]).second);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
def_scale = PropertyGet(props,"Lcl Scaling",aiVector3D(1.f,1.f,1.f));
|
|
|
|
}
|
2012-07-27 02:55:56 +00:00
|
|
|
|
2012-08-20 17:15:59 +00:00
|
|
|
if(chain[TransformationComp_Translation] != iter_end) {
|
|
|
|
translation = GetKeyframeList((*chain[TransformationComp_Translation]).second);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
def_translate = PropertyGet(props,"Lcl Translation",aiVector3D(0.f,0.f,0.f));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chain[TransformationComp_Rotation] != iter_end) {
|
|
|
|
rotation = GetKeyframeList((*chain[TransformationComp_Rotation]).second);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
def_rot = EulerToQuaternion(PropertyGet(props,"Lcl Rotation",aiVector3D(0.f,0.f,0.f)),
|
|
|
|
target.RotationOrder());
|
|
|
|
}
|
2012-08-20 16:33:54 +00:00
|
|
|
|
|
|
|
KeyFrameListList joined;
|
|
|
|
joined.insert(joined.end(), scaling.begin(), scaling.end());
|
|
|
|
joined.insert(joined.end(), translation.begin(), translation.end());
|
|
|
|
joined.insert(joined.end(), rotation.begin(), rotation.end());
|
2012-07-27 02:55:56 +00:00
|
|
|
|
2012-08-20 16:33:54 +00:00
|
|
|
const KeyTimeList& times = GetKeyTimeList(joined);
|
|
|
|
|
|
|
|
aiQuatKey* out_quat = new aiQuatKey[times.size()];
|
|
|
|
aiVectorKey* out_scale = new aiVectorKey[times.size()];
|
|
|
|
aiVectorKey* out_translation = new aiVectorKey[times.size()];
|
|
|
|
|
|
|
|
ConvertTransformOrder_TRStoSRT(out_quat, out_scale, out_translation,
|
|
|
|
scaling,
|
|
|
|
translation,
|
|
|
|
rotation,
|
|
|
|
times,
|
2012-07-27 02:55:56 +00:00
|
|
|
max_time,
|
2012-08-11 00:17:22 +00:00
|
|
|
min_time,
|
2012-08-20 17:15:59 +00:00
|
|
|
target.RotationOrder(),
|
|
|
|
def_scale,
|
|
|
|
def_translate,
|
|
|
|
def_rot);
|
2012-07-27 02:55:56 +00:00
|
|
|
|
2012-08-20 16:33:54 +00:00
|
|
|
// XXX remove duplicates / redundant keys which this operation did
|
|
|
|
// likely produce if not all three channels were equally dense.
|
2012-07-27 02:55:56 +00:00
|
|
|
|
2012-08-20 16:33:54 +00:00
|
|
|
na->mNumScalingKeys = static_cast<unsigned int>(times.size());
|
|
|
|
na->mNumRotationKeys = na->mNumScalingKeys;
|
|
|
|
na->mNumPositionKeys = na->mNumScalingKeys;
|
|
|
|
|
|
|
|
na->mScalingKeys = out_scale;
|
|
|
|
na->mRotationKeys = out_quat;
|
|
|
|
na->mPositionKeys = out_translation;
|
2012-07-27 02:55:56 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2012-08-20 16:33:54 +00:00
|
|
|
// if a particular transformation is not given, grab it from
|
|
|
|
// the corresponding node to meet the semantics of aiNodeAnim,
|
|
|
|
// which requires all of rotation, scaling and translation
|
|
|
|
// to be set.
|
|
|
|
if(chain[TransformationComp_Scaling] != iter_end) {
|
|
|
|
ConvertScaleKeys(na, (*chain[TransformationComp_Scaling]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
|
|
|
min_time);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
na->mScalingKeys = new aiVectorKey[1];
|
|
|
|
na->mNumScalingKeys = 1;
|
|
|
|
|
|
|
|
na->mScalingKeys[0].mTime = 0.;
|
|
|
|
na->mScalingKeys[0].mValue = PropertyGet(props,"Lcl Scaling",
|
|
|
|
aiVector3D(1.f,1.f,1.f));
|
|
|
|
}
|
2012-07-27 02:55:56 +00:00
|
|
|
|
2012-08-20 16:33:54 +00:00
|
|
|
if(chain[TransformationComp_Rotation] != iter_end) {
|
|
|
|
ConvertRotationKeys(na, (*chain[TransformationComp_Rotation]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
|
|
|
min_time,
|
|
|
|
target.RotationOrder());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
na->mRotationKeys = new aiQuatKey[1];
|
|
|
|
na->mNumRotationKeys = 1;
|
|
|
|
|
|
|
|
na->mRotationKeys[0].mTime = 0.;
|
|
|
|
na->mRotationKeys[0].mValue = EulerToQuaternion(
|
|
|
|
PropertyGet(props,"Lcl Rotation",aiVector3D(0.f,0.f,0.f)),
|
|
|
|
target.RotationOrder());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chain[TransformationComp_Translation] != iter_end) {
|
|
|
|
ConvertTranslationKeys(na, (*chain[TransformationComp_Translation]).second,
|
|
|
|
layer_map,
|
|
|
|
max_time,
|
|
|
|
min_time);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
na->mPositionKeys = new aiVectorKey[1];
|
|
|
|
na->mNumPositionKeys = 1;
|
|
|
|
|
|
|
|
na->mPositionKeys[0].mTime = 0.;
|
|
|
|
na->mPositionKeys[0].mValue = PropertyGet(props,"Lcl Translation",
|
|
|
|
aiVector3D(0.f,0.f,0.f));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-07-27 02:55:56 +00:00
|
|
|
return na.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
// key (time), value, mapto (component index)
|
2012-07-26 02:03:55 +00:00
|
|
|
typedef boost::tuple< const KeyTimeList*, const KeyValueList*, unsigned int > KeyFrameList;
|
2012-07-21 02:15:10 +00:00
|
|
|
typedef std::vector<KeyFrameList> KeyFrameListList;
|
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
KeyFrameListList GetKeyframeList(const std::vector<const AnimationCurveNode*>& nodes)
|
|
|
|
{
|
|
|
|
KeyFrameListList inputs;
|
|
|
|
inputs.reserve(nodes.size()*3);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const AnimationCurveNode* node, nodes) {
|
|
|
|
ai_assert(node);
|
|
|
|
|
|
|
|
const AnimationCurveMap& curves = node->Curves();
|
|
|
|
BOOST_FOREACH(const AnimationCurveMap::value_type& kv, curves) {
|
|
|
|
|
|
|
|
unsigned int mapto;
|
|
|
|
if (kv.first == "d|X") {
|
|
|
|
mapto = 0;
|
|
|
|
}
|
|
|
|
else if (kv.first == "d|Y") {
|
|
|
|
mapto = 1;
|
|
|
|
}
|
|
|
|
else if (kv.first == "d|Z") {
|
|
|
|
mapto = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FBXImporter::LogWarn("ignoring scale animation curve, did not recognize target component");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AnimationCurve* const curve = kv.second;
|
|
|
|
ai_assert(curve->GetKeys().size() == curve->GetValues().size() && curve->GetKeys().size());
|
|
|
|
|
|
|
|
inputs.push_back(boost::make_tuple(&curve->GetKeys(), &curve->GetValues(), mapto));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputs; // pray for NRVO :-)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-07-21 19:28:02 +00:00
|
|
|
KeyTimeList GetKeyTimeList(const KeyFrameListList& inputs)
|
2012-07-21 02:15:10 +00:00
|
|
|
{
|
2012-07-21 18:02:00 +00:00
|
|
|
ai_assert(inputs.size());
|
|
|
|
|
2012-07-21 14:19:11 +00:00
|
|
|
// reserve some space upfront - it is likely that the keyframe lists
|
|
|
|
// have matching time values, so max(of all keyframe lists) should
|
|
|
|
// be a good estimate.
|
2012-07-21 19:28:02 +00:00
|
|
|
KeyTimeList keys;
|
2012-07-21 14:19:11 +00:00
|
|
|
|
|
|
|
size_t estimate = 0;
|
|
|
|
BOOST_FOREACH(const KeyFrameList& kfl, inputs) {
|
|
|
|
estimate = std::max(estimate, kfl.get<0>()->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.reserve(estimate);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
std::vector<unsigned int> next_pos;
|
|
|
|
next_pos.resize(inputs.size(),0);
|
|
|
|
|
|
|
|
const size_t count = inputs.size();
|
|
|
|
while(true) {
|
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
uint64_t min_tick = std::numeric_limits<uint64_t>::max();
|
2012-07-21 02:15:10 +00:00
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
const KeyFrameList& kfl = inputs[i];
|
|
|
|
|
|
|
|
if (kfl.get<0>()->size() > next_pos[i] && kfl.get<0>()->at(next_pos[i]) < min_tick) {
|
|
|
|
min_tick = kfl.get<0>()->at(next_pos[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
if (min_tick == std::numeric_limits<uint64_t>::max()) {
|
2012-07-21 02:15:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
keys.push_back(min_tick);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
const KeyFrameList& kfl = inputs[i];
|
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
|
|
|
|
while(kfl.get<0>()->size() > next_pos[i] && kfl.get<0>()->at(next_pos[i]) == min_tick) {
|
2012-07-21 02:15:10 +00:00
|
|
|
++next_pos[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-11 00:24:59 +00:00
|
|
|
void InterpolateKeys(aiVectorKey* valOut,const KeyTimeList& keys, const KeyFrameListList& inputs,
|
|
|
|
const bool geom,
|
2012-08-25 15:31:15 +00:00
|
|
|
double& max_time,
|
|
|
|
double& min_time)
|
2012-07-21 21:48:52 +00:00
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
{
|
|
|
|
ai_assert(keys.size());
|
|
|
|
ai_assert(valOut);
|
|
|
|
|
|
|
|
std::vector<unsigned int> next_pos;
|
|
|
|
const size_t count = inputs.size();
|
|
|
|
|
|
|
|
next_pos.resize(inputs.size(),0);
|
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
BOOST_FOREACH(KeyTimeList::value_type time, keys) {
|
2012-07-21 02:15:10 +00:00
|
|
|
float result[3] = {0.0f, 0.0f, 0.0f};
|
|
|
|
if(geom) {
|
|
|
|
result[0] = result[1] = result[2] = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
const KeyFrameList& kfl = inputs[i];
|
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
const size_t ksize = kfl.get<0>()->size();
|
|
|
|
if (ksize > next_pos[i] && kfl.get<0>()->at(next_pos[i]) == time) {
|
2012-07-21 02:15:10 +00:00
|
|
|
++next_pos[i];
|
|
|
|
}
|
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
const size_t id0 = next_pos[i]>0 ? next_pos[i]-1 : 0;
|
|
|
|
const size_t id1 = next_pos[i]==ksize ? ksize-1 : next_pos[i];
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
// use lerp for interpolation
|
2012-07-21 19:28:02 +00:00
|
|
|
const KeyValueList::value_type valueA = kfl.get<1>()->at(id0);
|
|
|
|
const KeyValueList::value_type valueB = kfl.get<1>()->at(id1);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
const KeyTimeList::value_type timeA = kfl.get<0>()->at(id0);
|
|
|
|
const KeyTimeList::value_type timeB = kfl.get<0>()->at(id1);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-07-21 19:28:02 +00:00
|
|
|
// do the actual interpolation in double-precision arithmetics
|
|
|
|
// because it is a bit sensitive to rounding errors.
|
|
|
|
const double factor = timeB == timeA ? 0. : static_cast<double>((time - timeA) / (timeB - timeA));
|
|
|
|
const float interpValue = static_cast<float>(valueA + (valueB - valueA) * factor);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
if(geom) {
|
|
|
|
result[kfl.get<2>()] *= interpValue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result[kfl.get<2>()] += interpValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 15:31:15 +00:00
|
|
|
// magic value to convert fbx times to seconds
|
|
|
|
valOut->mTime = CONVERT_FBX_TIME(time) * anim_fps;
|
2012-07-21 21:48:52 +00:00
|
|
|
|
2012-08-25 15:31:15 +00:00
|
|
|
min_time = std::min(min_time, valOut->mTime);
|
|
|
|
max_time = std::max(max_time, valOut->mTime);
|
2012-07-21 21:48:52 +00:00
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
valOut->mValue.x = result[0];
|
|
|
|
valOut->mValue.y = result[1];
|
|
|
|
valOut->mValue.z = result[2];
|
|
|
|
|
|
|
|
++valOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-20 16:33:54 +00:00
|
|
|
void InterpolateKeys(aiQuatKey* valOut,const KeyTimeList& keys, const KeyFrameListList& inputs,
|
|
|
|
const bool geom,
|
2012-07-21 21:48:52 +00:00
|
|
|
double& maxTime,
|
2012-08-11 00:17:22 +00:00
|
|
|
double& minTime,
|
|
|
|
Model::RotOrder order)
|
2012-07-21 02:15:10 +00:00
|
|
|
{
|
|
|
|
ai_assert(keys.size());
|
|
|
|
ai_assert(valOut);
|
|
|
|
|
|
|
|
boost::scoped_array<aiVectorKey> temp(new aiVectorKey[keys.size()]);
|
2012-07-21 21:48:52 +00:00
|
|
|
InterpolateKeys(temp.get(),keys,inputs,geom,maxTime, minTime);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
2012-08-26 18:10:01 +00:00
|
|
|
aiMatrix4x4 m;
|
|
|
|
|
|
|
|
aiQuaternion lastq;
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
for (size_t i = 0, c = keys.size(); i < c; ++i) {
|
|
|
|
|
|
|
|
valOut[i].mTime = temp[i].mTime;
|
2012-08-26 18:10:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
GetRotationMatrix(order, temp[i].mValue, m);
|
|
|
|
aiQuaternion quat = aiQuaternion(aiMatrix3x3(m));
|
|
|
|
|
|
|
|
// take shortest path by checking the inner product
|
|
|
|
// http://www.3dkingdoms.com/weekly/weekly.php?a=36
|
|
|
|
if (quat.x * lastq.x + quat.y * lastq.y + quat.z * lastq.z + quat.w * lastq.w < 0)
|
|
|
|
{
|
|
|
|
quat.x = -quat.x;
|
|
|
|
quat.y = -quat.y;
|
|
|
|
quat.z = -quat.z;
|
|
|
|
quat.w = -quat.w;
|
|
|
|
}
|
|
|
|
lastq = quat;
|
|
|
|
|
|
|
|
valOut[i].mValue = quat;
|
2012-07-21 02:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-20 16:33:54 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ConvertTransformOrder_TRStoSRT(aiQuatKey* out_quat, aiVectorKey* out_scale,
|
|
|
|
aiVectorKey* out_translation,
|
|
|
|
const KeyFrameListList& scaling,
|
|
|
|
const KeyFrameListList& translation,
|
|
|
|
const KeyFrameListList& rotation,
|
|
|
|
const KeyTimeList& times,
|
|
|
|
double& maxTime,
|
|
|
|
double& minTime,
|
2012-08-20 17:15:59 +00:00
|
|
|
Model::RotOrder order,
|
|
|
|
const aiVector3D& def_scale,
|
|
|
|
const aiVector3D& def_translate,
|
|
|
|
const aiQuaternion& def_rotation)
|
2012-08-20 16:33:54 +00:00
|
|
|
{
|
2012-08-20 17:15:59 +00:00
|
|
|
if (rotation.size()) {
|
|
|
|
InterpolateKeys(out_quat, times, rotation, false, maxTime, minTime, order);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (size_t i = 0; i < times.size(); ++i) {
|
2012-08-25 15:31:15 +00:00
|
|
|
out_quat[i].mTime = CONVERT_FBX_TIME(times[i]) * anim_fps;
|
2012-08-20 17:15:59 +00:00
|
|
|
out_quat[i].mValue = def_rotation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scaling.size()) {
|
|
|
|
InterpolateKeys(out_scale, times, scaling, true, maxTime, minTime);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (size_t i = 0; i < times.size(); ++i) {
|
2012-08-25 15:31:15 +00:00
|
|
|
out_scale[i].mTime = CONVERT_FBX_TIME(times[i]) * anim_fps;
|
2012-08-20 17:15:59 +00:00
|
|
|
out_scale[i].mValue = def_scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (translation.size()) {
|
|
|
|
InterpolateKeys(out_translation, times, translation, false, maxTime, minTime);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (size_t i = 0; i < times.size(); ++i) {
|
2012-08-25 15:31:15 +00:00
|
|
|
out_translation[i].mTime = CONVERT_FBX_TIME(times[i]) * anim_fps;
|
2012-08-20 17:15:59 +00:00
|
|
|
out_translation[i].mValue = def_translate;
|
|
|
|
}
|
|
|
|
}
|
2012-08-20 16:33:54 +00:00
|
|
|
|
|
|
|
const size_t count = times.size();
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
aiQuaternion& r = out_quat[i].mValue;
|
|
|
|
aiVector3D& s = out_scale[i].mValue;
|
|
|
|
aiVector3D& t = out_translation[i].mValue;
|
|
|
|
|
|
|
|
aiMatrix4x4 mat, temp;
|
|
|
|
aiMatrix4x4::Translation(t, mat);
|
|
|
|
mat *= aiMatrix4x4( r.GetMatrix() );
|
|
|
|
mat *= aiMatrix4x4::Scaling(s, temp);
|
|
|
|
|
|
|
|
mat.Decompose(s, r, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-21 20:29:42 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// euler xyz -> quat
|
2012-08-11 00:17:22 +00:00
|
|
|
aiQuaternion EulerToQuaternion(const aiVector3D& rot, Model::RotOrder order)
|
2012-07-21 20:29:42 +00:00
|
|
|
{
|
2012-07-27 19:09:45 +00:00
|
|
|
aiMatrix4x4 m;
|
2012-08-11 00:17:22 +00:00
|
|
|
GetRotationMatrix(order, rot, m);
|
2012-07-21 20:29:42 +00:00
|
|
|
|
|
|
|
return aiQuaternion(aiMatrix3x3(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-21 02:15:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-08-18 00:09:06 +00:00
|
|
|
void ConvertScaleKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes, const LayerMap& /*layers*/,
|
2012-07-21 21:48:52 +00:00
|
|
|
double& maxTime,
|
|
|
|
double& minTime)
|
2012-07-21 02:15:10 +00:00
|
|
|
{
|
|
|
|
ai_assert(nodes.size());
|
|
|
|
|
|
|
|
// XXX for now, assume scale should be blended geometrically (i.e. two
|
|
|
|
// layers should be multiplied with each other). There is a FBX
|
|
|
|
// property in the layer to specify the behaviour, though.
|
|
|
|
|
|
|
|
const KeyFrameListList& inputs = GetKeyframeList(nodes);
|
|
|
|
const KeyTimeList& keys = GetKeyTimeList(inputs);
|
|
|
|
|
|
|
|
na->mNumScalingKeys = static_cast<unsigned int>(keys.size());
|
|
|
|
na->mScalingKeys = new aiVectorKey[keys.size()];
|
2012-07-21 21:48:52 +00:00
|
|
|
InterpolateKeys(na->mScalingKeys, keys, inputs, true, maxTime, minTime);
|
2012-07-21 02:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-11 00:17:22 +00:00
|
|
|
void ConvertTranslationKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes,
|
2014-08-18 00:09:06 +00:00
|
|
|
const LayerMap& /*layers*/,
|
2012-07-21 21:48:52 +00:00
|
|
|
double& maxTime,
|
|
|
|
double& minTime)
|
2012-07-21 02:15:10 +00:00
|
|
|
{
|
|
|
|
ai_assert(nodes.size());
|
|
|
|
|
|
|
|
// XXX see notes in ConvertScaleKeys()
|
|
|
|
const KeyFrameListList& inputs = GetKeyframeList(nodes);
|
|
|
|
const KeyTimeList& keys = GetKeyTimeList(inputs);
|
|
|
|
|
|
|
|
na->mNumPositionKeys = static_cast<unsigned int>(keys.size());
|
|
|
|
na->mPositionKeys = new aiVectorKey[keys.size()];
|
2012-07-21 21:48:52 +00:00
|
|
|
InterpolateKeys(na->mPositionKeys, keys, inputs, false, maxTime, minTime);
|
2012-07-21 02:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2012-08-11 00:17:22 +00:00
|
|
|
void ConvertRotationKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes,
|
2014-08-18 00:09:06 +00:00
|
|
|
const LayerMap& /*layers*/,
|
2012-07-21 21:48:52 +00:00
|
|
|
double& maxTime,
|
2012-08-11 00:17:22 +00:00
|
|
|
double& minTime,
|
|
|
|
Model::RotOrder order)
|
2012-07-21 02:15:10 +00:00
|
|
|
{
|
|
|
|
ai_assert(nodes.size());
|
|
|
|
|
|
|
|
// XXX see notes in ConvertScaleKeys()
|
|
|
|
const std::vector< KeyFrameList >& inputs = GetKeyframeList(nodes);
|
2012-07-21 19:28:02 +00:00
|
|
|
const KeyTimeList& keys = GetKeyTimeList(inputs);
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
na->mNumRotationKeys = static_cast<unsigned int>(keys.size());
|
|
|
|
na->mRotationKeys = new aiQuatKey[keys.size()];
|
2012-08-11 00:17:22 +00:00
|
|
|
InterpolateKeys(na->mRotationKeys, keys, inputs, false, maxTime, minTime, order);
|
2012-07-21 02:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// copy generated meshes, animations, lights, cameras and textures to the output scene
|
|
|
|
void TransferDataToScene()
|
|
|
|
{
|
|
|
|
ai_assert(!out->mMeshes && !out->mNumMeshes);
|
|
|
|
|
|
|
|
// note: the trailing () ensures initialization with NULL - not
|
|
|
|
// many C++ users seem to know this, so pointing it out to avoid
|
|
|
|
// confusion why this code works.
|
|
|
|
|
2012-08-25 16:17:11 +00:00
|
|
|
if(meshes.size()) {
|
|
|
|
out->mMeshes = new aiMesh*[meshes.size()]();
|
|
|
|
out->mNumMeshes = static_cast<unsigned int>(meshes.size());
|
2012-07-02 17:59:31 +00:00
|
|
|
|
2012-08-25 16:17:11 +00:00
|
|
|
std::swap_ranges(meshes.begin(),meshes.end(),out->mMeshes);
|
|
|
|
}
|
2012-07-02 17:59:31 +00:00
|
|
|
|
2012-07-02 21:31:48 +00:00
|
|
|
if(materials.size()) {
|
|
|
|
out->mMaterials = new aiMaterial*[materials.size()]();
|
|
|
|
out->mNumMaterials = static_cast<unsigned int>(materials.size());
|
2012-07-02 17:59:31 +00:00
|
|
|
|
2012-07-02 21:31:48 +00:00
|
|
|
std::swap_ranges(materials.begin(),materials.end(),out->mMaterials);
|
|
|
|
}
|
2012-07-21 02:15:10 +00:00
|
|
|
|
|
|
|
if(animations.size()) {
|
|
|
|
out->mAnimations = new aiAnimation*[animations.size()]();
|
|
|
|
out->mNumAnimations = static_cast<unsigned int>(animations.size());
|
|
|
|
|
|
|
|
std::swap_ranges(animations.begin(),animations.end(),out->mAnimations);
|
|
|
|
}
|
2012-08-10 23:51:04 +00:00
|
|
|
|
|
|
|
if(lights.size()) {
|
|
|
|
out->mLights = new aiLight*[lights.size()]();
|
|
|
|
out->mNumLights = static_cast<unsigned int>(lights.size());
|
|
|
|
|
|
|
|
std::swap_ranges(lights.begin(),lights.end(),out->mLights);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cameras.size()) {
|
|
|
|
out->mCameras = new aiCamera*[cameras.size()]();
|
|
|
|
out->mNumCameras = static_cast<unsigned int>(cameras.size());
|
|
|
|
|
|
|
|
std::swap_ranges(cameras.begin(),cameras.end(),out->mCameras);
|
|
|
|
}
|
2012-06-28 18:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2012-07-04 13:07:57 +00:00
|
|
|
// 0: not assigned yet, others: index is value - 1
|
|
|
|
unsigned int defaultMaterialIndex;
|
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
std::vector<aiMesh*> meshes;
|
2012-07-02 17:59:31 +00:00
|
|
|
std::vector<aiMaterial*> materials;
|
2012-07-21 02:15:10 +00:00
|
|
|
std::vector<aiAnimation*> animations;
|
2012-08-10 23:51:04 +00:00
|
|
|
std::vector<aiLight*> lights;
|
|
|
|
std::vector<aiCamera*> cameras;
|
2012-06-28 18:02:09 +00:00
|
|
|
|
2012-07-04 14:33:21 +00:00
|
|
|
typedef std::map<const Material*, unsigned int> MaterialMap;
|
|
|
|
MaterialMap materials_converted;
|
2012-07-04 13:07:57 +00:00
|
|
|
|
2012-07-06 17:32:41 +00:00
|
|
|
typedef std::map<const Geometry*, std::vector<unsigned int> > MeshMap;
|
2012-07-04 14:33:21 +00:00
|
|
|
MeshMap meshes_converted;
|
2012-07-03 18:41:21 +00:00
|
|
|
|
2012-07-27 16:45:02 +00:00
|
|
|
// fixed node name -> which trafo chain components have animations?
|
2012-07-27 14:18:47 +00:00
|
|
|
typedef std::map<std::string, unsigned int> NodeAnimBitMap;
|
|
|
|
NodeAnimBitMap node_anim_chain_bits;
|
|
|
|
|
2012-07-27 16:45:02 +00:00
|
|
|
// name -> has had its prefix_stripped?
|
|
|
|
typedef std::map<std::string, bool> NodeNameMap;
|
|
|
|
NodeNameMap node_names;
|
|
|
|
|
2012-08-25 20:24:08 +00:00
|
|
|
typedef std::map<std::string, std::string> NameNameMap;
|
|
|
|
NameNameMap renamed_nodes;
|
|
|
|
|
2012-08-25 15:31:15 +00:00
|
|
|
double anim_fps;
|
2012-07-27 14:18:47 +00:00
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
aiScene* const out;
|
|
|
|
const FBX::Document& doc;
|
|
|
|
};
|
|
|
|
|
2012-07-03 23:45:15 +00:00
|
|
|
//} // !anon
|
2012-06-28 18:02:09 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ConvertToAssimpScene(aiScene* out, const Document& doc)
|
|
|
|
{
|
|
|
|
Converter converter(out,doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // !FBX
|
|
|
|
} // !Assimp
|
|
|
|
|
|
|
|
#endif
|