2012-03-23 02:28:46 +00:00
|
|
|
//
|
|
|
|
|
// KRResource+fbx.cpp
|
|
|
|
|
// KREngine
|
|
|
|
|
//
|
|
|
|
|
// Created by Kearwood Gilbert on 12-03-22.
|
|
|
|
|
// Copyright (c) 2012 Kearwood Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <vector.h>
|
|
|
|
|
|
|
|
|
|
#include <fbxsdk.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "KRResource.h"
|
2012-10-03 19:55:34 +00:00
|
|
|
#include "KRModel.h"
|
2012-03-28 21:58:55 +00:00
|
|
|
#include "KRMaterial.h"
|
2012-04-05 23:09:41 +00:00
|
|
|
#include "KRLight.h"
|
|
|
|
|
#include "KRPointLight.h"
|
|
|
|
|
#include "KRDirectionalLight.h"
|
|
|
|
|
#include "KRSpotLight.h"
|
2012-04-12 00:43:53 +00:00
|
|
|
#include "KRNode.h"
|
|
|
|
|
#include "KRScene.h"
|
2012-11-29 21:28:49 +00:00
|
|
|
#include "KRQuaternion.h"
|
2012-03-23 02:28:46 +00:00
|
|
|
|
|
|
|
|
#ifdef IOS_REF
|
|
|
|
|
#undef IOS_REF
|
|
|
|
|
#define IOS_REF (*(pSdkManager->GetIOSettings()))
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void InitializeSdkObjects(KFbxSdkManager*& pSdkManager, KFbxScene*& pScene);
|
|
|
|
|
void DestroySdkObjects(KFbxSdkManager* pSdkManager);
|
|
|
|
|
bool LoadScene(KFbxSdkManager* pSdkManager, KFbxDocument* pScene, const char* pFilename);
|
2012-12-01 02:03:18 +00:00
|
|
|
KRAnimation *LoadAnimation(KRContext &context, FbxAnimStack* pAnimStack);
|
|
|
|
|
KRAnimationLayer *LoadAnimationLayer(KRContext &context, FbxAnimLayer *pAnimLayer);
|
2012-11-30 00:32:03 +00:00
|
|
|
void LoadNode(KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode);
|
2012-12-01 02:03:18 +00:00
|
|
|
//void BakeNode(KFbxNode* pNode);
|
2012-11-30 00:32:03 +00:00
|
|
|
KRNode *LoadMesh(KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode);
|
2012-11-03 02:57:35 +00:00
|
|
|
KRNode *LoadLight(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode);
|
2012-03-23 02:28:46 +00:00
|
|
|
|
2012-11-29 21:28:49 +00:00
|
|
|
const float KRAKEN_FBX_ANIMATION_FRAMERATE = 30.0f; // FINDME - This should be configurable
|
|
|
|
|
|
2012-09-05 18:14:08 +00:00
|
|
|
std::vector<KRResource *> KRResource::LoadFbx(KRContext &context, const std::string& path)
|
2012-03-23 02:28:46 +00:00
|
|
|
{
|
2012-11-29 21:28:49 +00:00
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
std::vector<KRResource *> resources;
|
2012-09-05 18:14:08 +00:00
|
|
|
KRScene *pScene = new KRScene(context, KRResource::GetFileBase(path));
|
2012-04-12 00:43:53 +00:00
|
|
|
resources.push_back(pScene);
|
|
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
KFbxSdkManager* lSdkManager = NULL;
|
2012-04-12 00:43:53 +00:00
|
|
|
KFbxScene* pFbxScene = NULL;
|
2012-03-23 02:28:46 +00:00
|
|
|
bool lResult;
|
2012-11-30 00:32:03 +00:00
|
|
|
FbxGeometryConverter *pGeometryConverter = NULL;
|
2012-03-23 02:28:46 +00:00
|
|
|
|
|
|
|
|
// Prepare the FBX SDK.
|
2012-04-12 00:43:53 +00:00
|
|
|
InitializeSdkObjects(lSdkManager, pFbxScene);
|
2012-03-23 02:28:46 +00:00
|
|
|
|
|
|
|
|
// Initialize Geometry Converter
|
2012-11-30 00:32:03 +00:00
|
|
|
pGeometryConverter = new FbxGeometryConverter(lSdkManager);
|
2012-03-23 02:28:46 +00:00
|
|
|
|
|
|
|
|
// Load the scene.
|
2012-04-12 00:43:53 +00:00
|
|
|
lResult = LoadScene(lSdkManager, pFbxScene, path.c_str());
|
2012-03-23 02:28:46 +00:00
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
// ----====---- Bake pivots into transforms, as Kraken doesn't support them directly ----====----
|
2012-03-23 02:28:46 +00:00
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
printf("Baking pivots...\n");
|
2012-04-12 00:43:53 +00:00
|
|
|
KFbxNode* pNode = pFbxScene->GetRootNode();
|
2012-12-01 02:03:18 +00:00
|
|
|
if(pNode) {
|
|
|
|
|
pNode->ResetPivotSetAndConvertAnimation();
|
|
|
|
|
}
|
2012-03-23 02:28:46 +00:00
|
|
|
|
2012-12-02 09:31:01 +00:00
|
|
|
// ----====---- Import Animation Layers ----====----
|
|
|
|
|
|
|
|
|
|
int animation_count = pFbxScene->GetSrcObjectCount(FBX_TYPE(FbxAnimStack));
|
|
|
|
|
for(int i = 0; i < animation_count; i++) {
|
|
|
|
|
// FbxAnimStack* pAnimStack = FbxCast<FbxAnimStack>(pFbxScene->GetSrcObject(FBX_TYPE(FbxAnimStack), i));
|
|
|
|
|
resources.push_back(LoadAnimation(context, pFbxScene->GetSrcObject<FbxAnimStack>(i)));
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
// ----====---- Import Scene Graph Nodes ----====----
|
2012-03-23 02:28:46 +00:00
|
|
|
if(pNode)
|
|
|
|
|
{
|
2012-12-01 02:03:18 +00:00
|
|
|
for(int i = 0; i < pNode->GetChildCount(); i++)
|
2012-03-23 02:28:46 +00:00
|
|
|
{
|
2012-04-12 00:43:53 +00:00
|
|
|
LoadNode(pScene->getRootNode(), resources, pGeometryConverter, pNode->GetChild(i));
|
2012-03-23 02:28:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-02 09:31:01 +00:00
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
DestroySdkObjects(lSdkManager);
|
|
|
|
|
|
|
|
|
|
return resources;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InitializeSdkObjects(KFbxSdkManager*& pSdkManager, KFbxScene*& pScene)
|
|
|
|
|
{
|
|
|
|
|
// The first thing to do is to create the FBX SDK manager which is the
|
|
|
|
|
// object allocator for almost all the classes in the SDK.
|
|
|
|
|
pSdkManager = KFbxSdkManager::Create();
|
|
|
|
|
|
|
|
|
|
if (!pSdkManager)
|
|
|
|
|
{
|
|
|
|
|
printf("Unable to create the FBX SDK manager\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create an IOSettings object
|
|
|
|
|
KFbxIOSettings * ios = KFbxIOSettings::Create(pSdkManager, IOSROOT );
|
|
|
|
|
pSdkManager->SetIOSettings(ios);
|
|
|
|
|
|
|
|
|
|
// Load plugins from the executable directory
|
2012-11-30 00:32:03 +00:00
|
|
|
KString lPath = FbxGetApplicationDirectory();
|
2012-03-23 02:28:46 +00:00
|
|
|
#if defined(KARCH_ENV_WIN)
|
|
|
|
|
KString lExtension = "dll";
|
|
|
|
|
#elif defined(KARCH_ENV_MACOSX)
|
|
|
|
|
KString lExtension = "dylib";
|
|
|
|
|
#elif defined(KARCH_ENV_LINUX)
|
|
|
|
|
KString lExtension = "so";
|
|
|
|
|
#endif
|
|
|
|
|
pSdkManager->LoadPluginsDirectory(lPath.Buffer(), lExtension.Buffer());
|
|
|
|
|
|
|
|
|
|
// Create the entity that will hold the scene.
|
|
|
|
|
pScene = KFbxScene::Create(pSdkManager,"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DestroySdkObjects(KFbxSdkManager* pSdkManager)
|
|
|
|
|
{
|
|
|
|
|
// Delete the FBX SDK manager. All the objects that have been allocated
|
|
|
|
|
// using the FBX SDK manager and that haven't been explicitly destroyed
|
|
|
|
|
// are automatically destroyed at the same time.
|
|
|
|
|
if (pSdkManager) pSdkManager->Destroy();
|
|
|
|
|
pSdkManager = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool LoadScene(KFbxSdkManager* pSdkManager, KFbxDocument* pScene, const char* pFilename)
|
|
|
|
|
{
|
|
|
|
|
int lFileMajor, lFileMinor, lFileRevision;
|
|
|
|
|
int lSDKMajor, lSDKMinor, lSDKRevision;
|
|
|
|
|
//int lFileFormat = -1;
|
2012-12-01 02:03:18 +00:00
|
|
|
int lAnimStackCount;
|
2012-03-23 02:28:46 +00:00
|
|
|
bool lStatus;
|
|
|
|
|
char lPassword[1024];
|
|
|
|
|
|
|
|
|
|
// Get the file version number generate by the FBX SDK.
|
|
|
|
|
KFbxSdkManager::GetFileFormatVersion(lSDKMajor, lSDKMinor, lSDKRevision);
|
|
|
|
|
|
|
|
|
|
// Create an importer.
|
|
|
|
|
KFbxImporter* lImporter = KFbxImporter::Create(pSdkManager,"");
|
|
|
|
|
|
|
|
|
|
// Initialize the importer by providing a filename.
|
|
|
|
|
const bool lImportStatus = lImporter->Initialize(pFilename, -1, pSdkManager->GetIOSettings());
|
|
|
|
|
lImporter->GetFileVersion(lFileMajor, lFileMinor, lFileRevision);
|
|
|
|
|
|
|
|
|
|
if( !lImportStatus )
|
|
|
|
|
{
|
|
|
|
|
printf("Call to KFbxImporter::Initialize() failed.\n");
|
|
|
|
|
printf("Error returned: %s\n\n", lImporter->GetLastErrorString());
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
if (lImporter->GetLastErrorID() == FbxIOBase::eFileVersionNotSupportedYet ||
|
|
|
|
|
lImporter->GetLastErrorID() == FbxIOBase::eFileVersionNotSupportedAnymore)
|
2012-03-23 02:28:46 +00:00
|
|
|
{
|
|
|
|
|
printf("FBX version number for this FBX SDK is %d.%d.%d\n", lSDKMajor, lSDKMinor, lSDKRevision);
|
|
|
|
|
printf("FBX version number for file %s is %d.%d.%d\n\n", pFilename, lFileMajor, lFileMinor, lFileRevision);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("FBX version number for this FBX SDK is %d.%d.%d\n", lSDKMajor, lSDKMinor, lSDKRevision);
|
|
|
|
|
|
2012-11-29 21:28:49 +00:00
|
|
|
if(!lImporter->IsFBX()) {
|
|
|
|
|
printf("ERROR Unrecognized FBX File\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("FBX version number for file %s is %d.%d.%d\n\n", pFilename, lFileMajor, lFileMinor, lFileRevision);
|
|
|
|
|
|
|
|
|
|
// From this point, it is possible to access animation stack information without
|
|
|
|
|
// the expense of loading the entire file.
|
|
|
|
|
|
|
|
|
|
printf("Animation Stack Information\n");
|
|
|
|
|
|
|
|
|
|
lAnimStackCount = lImporter->GetAnimStackCount();
|
|
|
|
|
|
|
|
|
|
printf(" Number of Animation Stacks: %d\n", lAnimStackCount);
|
|
|
|
|
printf(" Current Animation Stack: \"%s\"\n", lImporter->GetActiveAnimStackName().Buffer());
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
|
2012-11-29 21:28:49 +00:00
|
|
|
// Set the import states. By default, the import states are always set to
|
|
|
|
|
// true. The code below shows how to change these states.
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_MATERIAL, true);
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_TEXTURE, true);
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_LINK, true);
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_SHAPE, true);
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_GOBO, true);
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_ANIMATION, true);
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_GLOBAL_SETTINGS, true);
|
|
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
// Import the scene.
|
|
|
|
|
lStatus = lImporter->Import(pScene);
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
if(lStatus == false && lImporter->GetLastErrorID() == FbxIOBase::ePasswordError)
|
2012-03-23 02:28:46 +00:00
|
|
|
{
|
|
|
|
|
printf("Please enter password: ");
|
|
|
|
|
|
|
|
|
|
lPassword[0] = '\0';
|
|
|
|
|
|
|
|
|
|
scanf("%s", lPassword);
|
|
|
|
|
KString lString(lPassword);
|
|
|
|
|
|
|
|
|
|
IOS_REF.SetStringProp(IMP_FBX_PASSWORD, lString);
|
|
|
|
|
IOS_REF.SetBoolProp(IMP_FBX_PASSWORD_ENABLE, true);
|
|
|
|
|
|
|
|
|
|
lStatus = lImporter->Import(pScene);
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
if(lStatus == false && lImporter->GetLastErrorID() == FbxIOBase::ePasswordError)
|
2012-03-23 02:28:46 +00:00
|
|
|
{
|
|
|
|
|
printf("\nPassword is wrong, import aborted.\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
// // ----====---- Start: Bake pivots into transforms, as Kraken doesn't support them directly ----====----
|
|
|
|
|
//
|
|
|
|
|
// printf("Baking pivots...\n");
|
|
|
|
|
// KFbxNode* pNode = ((KFbxScene*)pScene)->GetRootNode();
|
|
|
|
|
//// BakeNode(pNode);
|
|
|
|
|
//
|
|
|
|
|
// for(i = 0; i < lAnimStackCount; i++)
|
|
|
|
|
// {
|
|
|
|
|
// KFbxTakeInfo* lTakeInfo = lImporter->GetTakeInfo(i);
|
|
|
|
|
//
|
|
|
|
|
// printf(" Animation: \"%s\"\n", lTakeInfo->mName.Buffer());
|
|
|
|
|
//
|
|
|
|
|
// //pNode->ConvertPivotAnimationRecursive(lTakeInfo->mName.Buffer(), KFbxNode::eDestinationPivot, KRAKEN_FBX_ANIMATION_FRAMERATE);
|
|
|
|
|
// pNode->ResetPivotSetAndConvertAnimation();
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
//// pNode->ConvertPivotAnimationRecursive(NULL, KFbxNode::eDestinationPivot, KRAKEN_FBX_ANIMATION_FRAMERATE);
|
|
|
|
|
//// pNode->UpdatePropertiesFromPivotsAndLimits();
|
|
|
|
|
//
|
|
|
|
|
// // ----====---- End: Bake pivots into transforms, as Kraken doesn't support them directly ----====----
|
|
|
|
|
|
|
|
|
|
// // ----====---- Bake pivots into transforms, as Kraken doesn't support them directly ----====----
|
|
|
|
|
//
|
|
|
|
|
// printf("Baking pivots...\n");
|
|
|
|
|
// KFbxNode* pNode = ((KFbxScene*)pScene)->GetRootNode();
|
|
|
|
|
// if(pNode) {
|
|
|
|
|
// pNode->ResetPivotSetAndConvertAnimation();
|
|
|
|
|
// }
|
2012-11-29 21:28:49 +00:00
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
// Destroy the importer.
|
|
|
|
|
lImporter->Destroy();
|
|
|
|
|
|
|
|
|
|
return lStatus;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
KRAnimation *LoadAnimation(KRContext &context, FbxAnimStack* pAnimStack)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("Loading animation: \"%s\"\n", pAnimStack->GetName());
|
|
|
|
|
|
|
|
|
|
KRAnimation *new_animation = new KRAnimation(context, pAnimStack->GetName());
|
|
|
|
|
int cLayers = pAnimStack->GetMemberCount<FbxAnimLayer>();
|
|
|
|
|
for(int iLayer=0; iLayer < cLayers; iLayer++) {
|
|
|
|
|
new_animation->addLayer(LoadAnimationLayer(context, pAnimStack->GetMember<FbxAnimLayer>(iLayer)));
|
2012-11-29 21:28:49 +00:00
|
|
|
}
|
2012-12-01 02:03:18 +00:00
|
|
|
return new_animation;
|
2012-11-29 21:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
|
|
|
|
|
KRAnimationLayer *LoadAnimationLayer(KRContext &context, FbxAnimLayer *pAnimLayer)
|
|
|
|
|
{
|
|
|
|
|
KRAnimationLayer *new_layer = new KRAnimationLayer(context);
|
|
|
|
|
new_layer->setName(pAnimLayer->GetName());
|
2012-12-02 09:31:01 +00:00
|
|
|
new_layer->setWeight(pAnimLayer->Weight.Get() / 100.0f);
|
2012-12-01 02:03:18 +00:00
|
|
|
switch(pAnimLayer->BlendMode.Get()) {
|
|
|
|
|
case FbxAnimLayer::eBlendAdditive:
|
|
|
|
|
new_layer->setBlendMode(KRAnimationLayer::KRENGINE_ANIMATION_BLEND_MODE_ADDITIVE);
|
|
|
|
|
break;
|
|
|
|
|
case FbxAnimLayer::eBlendOverride:
|
|
|
|
|
new_layer->setBlendMode(KRAnimationLayer::KRENGINE_ANIMATION_BLEND_MODE_OVERRIDE);
|
|
|
|
|
break;
|
|
|
|
|
case FbxAnimLayer::eBlendOverridePassthrough:
|
|
|
|
|
new_layer->setBlendMode(KRAnimationLayer::KRENGINE_ANIMATION_BLEND_MODE_OVERRIDE_PASSTHROUGH);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch(pAnimLayer->RotationAccumulationMode.Get()) {
|
|
|
|
|
case FbxAnimLayer::eRotationByLayer:
|
|
|
|
|
new_layer->setRotationAccumulationMode(KRAnimationLayer::KRENGINE_ANIMATION_ROTATION_ACCUMULATION_BY_LAYER);
|
|
|
|
|
break;
|
|
|
|
|
case FbxAnimLayer::eRotationByChannel:
|
|
|
|
|
new_layer->setRotationAccumulationMode(KRAnimationLayer::KRENGINE_ANIMATION_ROTATION_ACCUMULATION_BY_CHANNEL);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch(pAnimLayer->ScaleAccumulationMode.Get()) {
|
|
|
|
|
case FbxAnimLayer::eScaleAdditive:
|
|
|
|
|
new_layer->setScaleAccumulationMode(KRAnimationLayer::KRENGINE_ANIMATION_SCALE_ACCUMULATION_ADDITIVE);
|
|
|
|
|
break;
|
|
|
|
|
case FbxAnimLayer::eScaleMultiply:
|
|
|
|
|
new_layer->setScaleAccumulationMode(KRAnimationLayer::KRENGINE_ANIMATION_SCALE_ACCUMULATION_MULTIPLY);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-12-02 09:31:01 +00:00
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
return new_layer;
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
//void BakeNode(KFbxNode *pNode) {
|
|
|
|
|
//
|
|
|
|
|
// pNode->SetPivotState(KFbxNode::eSourcePivot, KFbxNode::ePivotActive);
|
|
|
|
|
// pNode->SetPivotState(KFbxNode::eDestinationPivot, KFbxNode::ePivotActive);
|
|
|
|
|
//
|
|
|
|
|
// // Pass the current value to the source pivot.
|
|
|
|
|
//// * - Rotation offset (Roff)
|
|
|
|
|
//// * - Rotation pivot (Rp)
|
|
|
|
|
//// * - Pre-rotation (Rpre)
|
|
|
|
|
//// * - Post-rotation (Rpost)
|
|
|
|
|
//// * - Scaling offset (Soff)
|
|
|
|
|
//// * - Scaling pivot (Sp)
|
|
|
|
|
//// * - Geometric translation (Gt)
|
|
|
|
|
//// * - Geometric rotation (Gr)
|
|
|
|
|
//// * - Geometric scaling (Gs)
|
|
|
|
|
// /*
|
|
|
|
|
// pNode->SetPostRotation(KFbxNode::eSourcePivot, pNode->PostRotation.Get());
|
|
|
|
|
// pNode->SetPreRotation(KFbxNode::eSourcePivot, pNode->PreRotation.Get());
|
|
|
|
|
// pNode->SetRotationOffset(KFbxNode::eSourcePivot, pNode->RotationOffset.Get());
|
|
|
|
|
// pNode->SetScalingOffset(KFbxNode::eSourcePivot, pNode->ScalingOffset.Get());
|
|
|
|
|
// pNode->SetRotationPivot(KFbxNode::eSourcePivot, pNode->RotationPivot.Get());
|
|
|
|
|
// pNode->SetScalingPivot(KFbxNode::eSourcePivot, pNode->ScalingPivot.Get());
|
|
|
|
|
// pNode->SetGeometricRotation(KFbxNode::eSourcePivot, pNode->GeometricRotation.Get());
|
|
|
|
|
// pNode->SetGeometricTranslation(KFbxNode::eSourcePivot, pNode->GeometricTranslation.Get());
|
|
|
|
|
// pNode->SetGeometricScaling(KFbxNode::eSourcePivot, pNode->GeometricScaling.Get());
|
|
|
|
|
// pNode->SetRotationOrder(KFbxNode::eSourcePivot, pNode->RotationOrder.Get());
|
|
|
|
|
// */
|
|
|
|
|
//
|
|
|
|
|
// // We want to set all these to 0 and bake them into the transforms.
|
|
|
|
|
// KFbxVector4 lZero(0.0, 0.0, 0.0);
|
|
|
|
|
// KFbxVector4 lOne(1.0, 1.0, 1.0);
|
|
|
|
|
// pNode->SetPostRotation(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetPreRotation(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetRotationOffset(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetScalingOffset(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetRotationPivot(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetScalingPivot(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetGeometricRotation(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetGeometricTranslation(KFbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetGeometricScaling(KFbxNode::eDestinationPivot, lOne);
|
|
|
|
|
// pNode->SetRotationOrder(KFbxNode::eDestinationPivot, eEULER_XYZ);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// /*
|
|
|
|
|
// FbxVector4 lZero(0,0,0);
|
|
|
|
|
// FbxVector4 lOne(1,1,1);
|
|
|
|
|
// pNode->SetPivotState(FbxNode::eSourcePivot, FbxNode::ePivotActive);
|
|
|
|
|
// pNode->SetPivotState(FbxNode::eDestinationPivot, FbxNode::ePivotActive);
|
|
|
|
|
//
|
|
|
|
|
// EFbxRotationOrder lRotationOrder;
|
|
|
|
|
// pNode->GetRotationOrder(FbxNode::eSourcePivot , lRotationOrder);
|
|
|
|
|
// pNode->SetRotationOrder(FbxNode::eDestinationPivot , lRotationOrder);
|
|
|
|
|
//
|
|
|
|
|
// //For cameras and lights (without targets) let's compensate the postrotation.
|
|
|
|
|
// if( pNode->GetCamera() || pNode->GetLight() )
|
|
|
|
|
// {
|
|
|
|
|
// if( !pNode->GetTarget() )
|
|
|
|
|
// {
|
|
|
|
|
// FbxVector4 lRV(90, 0, 0);
|
|
|
|
|
// if( pNode->GetCamera() )
|
|
|
|
|
// lRV.Set(0, 90, 0);
|
|
|
|
|
//
|
|
|
|
|
// FbxVector4 prV = pNode->GetPostRotation(FbxNode::eSourcePivot);
|
|
|
|
|
// FbxAMatrix lSourceR;
|
|
|
|
|
// FbxAMatrix lR(lZero, lRV, lOne);
|
|
|
|
|
// FbxVector4 res = prV;
|
|
|
|
|
//
|
|
|
|
|
// // Rotation order don't affect post rotation, so just use the default XYZ order
|
|
|
|
|
// FbxRotationOrder rOrder;
|
|
|
|
|
// rOrder.V2M(lSourceR, res);
|
|
|
|
|
//
|
|
|
|
|
// lR = lSourceR * lR;
|
|
|
|
|
// rOrder.M2V(res, lR);
|
|
|
|
|
// prV = res;
|
|
|
|
|
// pNode->SetPostRotation(FbxNode::eSourcePivot, prV);
|
|
|
|
|
// pNode->SetRotationActive(true);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // Point light do not need to be adjusted (since they radiate in all the directions).
|
|
|
|
|
// if( pNode->GetLight() && pNode->GetLight()->LightType.Get() == FbxLight::ePoint )
|
|
|
|
|
// {
|
|
|
|
|
// pNode->SetPostRotation(FbxNode::eSourcePivot, FbxVector4(0,0,0,0));
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// // apply Pre rotations only on bones / end of chains
|
|
|
|
|
// if( pNode->GetNodeAttribute() && pNode->GetNodeAttribute()->GetAttributeType() == FbxNodeAttribute::eSkeleton
|
|
|
|
|
// || (pNode->GetMarker() && pNode->GetMarker()->GetType() == FbxMarker::eEffectorFK)
|
|
|
|
|
// || (pNode->GetMarker() && pNode->GetMarker()->GetType() == FbxMarker::eEffectorIK) )
|
|
|
|
|
// {
|
|
|
|
|
// if( pNode->GetRotationActive() )
|
|
|
|
|
// {
|
|
|
|
|
// pNode->SetPreRotation(FbxNode::eDestinationPivot, pNode->GetPreRotation(FbxNode::eSourcePivot));
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // No pivots on bones
|
|
|
|
|
// pNode->SetRotationPivot(FbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetScalingPivot(FbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// pNode->SetRotationOffset(FbxNode::eDestinationPivot,lZero);
|
|
|
|
|
// pNode->SetScalingOffset(FbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// // any other type: no pre-rotation support but...
|
|
|
|
|
// pNode->SetPreRotation(FbxNode::eDestinationPivot, lZero);
|
|
|
|
|
//
|
|
|
|
|
// // support for rotation and scaling pivots.
|
|
|
|
|
// pNode->SetRotationPivot(FbxNode::eDestinationPivot, pNode->GetRotationPivot(FbxNode::eSourcePivot));
|
|
|
|
|
// pNode->SetScalingPivot(FbxNode::eDestinationPivot, pNode->GetScalingPivot(FbxNode::eSourcePivot));
|
|
|
|
|
// // Rotation and scaling offset are supported
|
|
|
|
|
// pNode->SetRotationOffset(FbxNode::eDestinationPivot, pNode->GetRotationOffset(FbxNode::eSourcePivot));
|
|
|
|
|
// pNode->SetScalingOffset(FbxNode::eDestinationPivot, pNode->GetScalingOffset(FbxNode::eSourcePivot));
|
|
|
|
|
// //
|
|
|
|
|
// // If we don't "support" scaling pivots, we can simply do:
|
|
|
|
|
// // pNode->SetRotationPivot(FbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// // pNode->SetScalingPivot(FbxNode::eDestinationPivot, lZero);
|
|
|
|
|
// }
|
|
|
|
|
// */
|
|
|
|
|
//
|
|
|
|
|
// // Bake child nodes
|
|
|
|
|
// for(int i = 0; i < pNode->GetChildCount(); i++)
|
|
|
|
|
// {
|
|
|
|
|
// BakeNode(pNode->GetChild(i));
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
void LoadNode(KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) {
|
2012-03-23 02:28:46 +00:00
|
|
|
KFbxVector4 lTmpVector;
|
2012-11-29 21:28:49 +00:00
|
|
|
pNode->UpdatePropertiesFromPivotsAndLimits();
|
|
|
|
|
// Transform = T * Roff * Rp * Rpre * R * Rpost * inverse(Rp) * Soff * Sp * S * inverse(Sp)
|
2012-12-02 09:31:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
fbxDouble3 local_rotation = pNode->LclRotation.Get(); // pNode->GetGeometricRotation(KFbxNode::eSourcePivot);
|
|
|
|
|
fbxDouble3 local_translation = pNode->LclTranslation.Get(); // pNode->GetGeometricTranslation(KFbxNode::eSourcePivot);
|
|
|
|
|
fbxDouble3 local_scale = pNode->LclScaling.Get(); // pNode->GetGeometricScaling(KFbxNode::eSourcePivot);
|
2012-09-13 20:09:19 +00:00
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
fbxDouble3 post_rotation = pNode->PostRotation.Get();
|
|
|
|
|
fbxDouble3 pre_rotation = pNode->PreRotation.Get();
|
|
|
|
|
fbxDouble3 rotation_offset = pNode->RotationOffset.Get();
|
|
|
|
|
fbxDouble3 scaling_offset = pNode->ScalingOffset.Get();
|
|
|
|
|
fbxDouble3 rotation_pivot = pNode->RotationPivot.Get();
|
|
|
|
|
fbxDouble3 scaling_pivot = pNode->ScalingPivot.Get();
|
|
|
|
|
fbxDouble3 geometric_rotation = pNode->GeometricRotation.Get();
|
|
|
|
|
fbxDouble3 geometric_translation = pNode->GeometricTranslation.Get();
|
|
|
|
|
fbxDouble3 geometric_scaling = pNode->GeometricScaling.Get();
|
|
|
|
|
ERotationOrder rotation_order = pNode->RotationOrder.Get();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KFbxVector4 lZero(0.0, 0.0, 0.0);
|
|
|
|
|
KFbxVector4 lOne(1.0, 1.0, 1.0);
|
|
|
|
|
|
|
|
|
|
assert(post_rotation == lZero);
|
|
|
|
|
assert(pre_rotation == lZero);
|
|
|
|
|
assert(rotation_offset == lZero);
|
|
|
|
|
assert(scaling_offset == lZero);
|
|
|
|
|
assert(rotation_pivot == lZero);
|
|
|
|
|
assert(scaling_pivot == lZero);
|
|
|
|
|
assert(geometric_rotation == lZero);
|
|
|
|
|
assert(geometric_translation == lZero);
|
|
|
|
|
assert(geometric_scaling == lOne);
|
|
|
|
|
assert(rotation_order == eEulerXYZ);
|
2012-11-29 21:28:49 +00:00
|
|
|
//
|
|
|
|
|
// bool rotation_active = pNode->RotationActive.Get();
|
|
|
|
|
|
|
|
|
|
// KRVector3 node_translation = KRVector3(local_translation[0] + rotation_offset[0] + rotation_pivot[0], local_translation[1] + rotation_offset[1] + rotation_pivot[1], local_translation[2] + rotation_offset[2] + rotation_pivot[2]); // T * Roff * Rp
|
|
|
|
|
KRVector3 node_translation = KRVector3(local_translation[0], local_translation[1], local_translation[2]); // T * Roff * Rp
|
|
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
// KRVector3 node_rotation = KRQuaternion(KRVector3(local_rotation[0], local_rotation[1], local_rotation[2]) / 180.0 * M_PI).euler(); // R
|
|
|
|
|
KRVector3 node_rotation = KRVector3(local_rotation[0], local_rotation[1], local_rotation[2]) / 180.0 * M_PI;
|
2012-11-29 21:28:49 +00:00
|
|
|
// if(rotation_active) {
|
|
|
|
|
// node_rotation = (KRQuaternion(KRVector3(post_rotation[0], post_rotation[1], post_rotation[2]) / 180.0 * M_PI) * KRQuaternion(KRVector3(local_rotation[0], local_rotation[1], local_rotation[2]) / 180.0 * M_PI) * KRQuaternion(KRVector3(pre_rotation[0], pre_rotation[1], pre_rotation[2]) / 180.0 * M_PI)).euler(); // Rpre * R * Rpost
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
KRVector3 node_scale = KRVector3(local_scale[0], local_scale[1], local_scale[2]);
|
|
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
// printf(" Local Translation: %f %f %f\n", local_translation[0], local_translation[1], local_translation[2]);
|
|
|
|
|
// printf(" Local Rotation: %f %f %f\n", local_rotation[0], local_rotation[1], local_rotation[2]);
|
|
|
|
|
// printf(" Local Scaling: %f %f %f\n", local_scale[0], local_scale[1], local_scale[2]);
|
2012-11-03 02:57:35 +00:00
|
|
|
|
|
|
|
|
KRNode *new_node = NULL;
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
KFbxNodeAttribute::EType attribute_type = (pNode->GetNodeAttribute()->GetAttributeType());
|
2012-03-23 02:28:46 +00:00
|
|
|
switch(attribute_type) {
|
2012-11-30 00:32:03 +00:00
|
|
|
case KFbxNodeAttribute::eMesh:
|
2012-11-03 02:57:35 +00:00
|
|
|
new_node = LoadMesh(parent_node, resources, pGeometryConverter, pNode);
|
2012-03-23 02:28:46 +00:00
|
|
|
break;
|
2012-11-30 00:32:03 +00:00
|
|
|
case KFbxNodeAttribute::eLight:
|
2012-11-03 02:57:35 +00:00
|
|
|
new_node = LoadLight(parent_node, resources, pNode);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
if(pNode->GetChildCount() > 0) {
|
|
|
|
|
// Create an empty node, for inheritence of transforms
|
|
|
|
|
new_node = new KRNode(parent_node->getScene(), pNode->GetName());
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-05 23:09:41 +00:00
|
|
|
break;
|
2012-03-23 02:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-11-03 02:57:35 +00:00
|
|
|
if(new_node != NULL) {
|
2012-11-29 21:28:49 +00:00
|
|
|
new_node->setLocalRotation(node_rotation);
|
|
|
|
|
new_node->setLocalTranslation(node_translation);
|
|
|
|
|
new_node->setLocalScale(node_scale);
|
2012-11-03 02:57:35 +00:00
|
|
|
parent_node->addChild(new_node);
|
|
|
|
|
|
|
|
|
|
// Load child nodes
|
|
|
|
|
for(int i = 0; i < pNode->GetChildCount(); i++)
|
|
|
|
|
{
|
|
|
|
|
LoadNode(new_node, resources, pGeometryConverter, pNode->GetChild(i));
|
|
|
|
|
}
|
2012-03-23 02:28:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
KRNode *LoadMesh(KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) {
|
2012-04-12 00:43:53 +00:00
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
printf("Mesh: %s\n", pNode->GetName());
|
|
|
|
|
KFbxMesh* pSourceMesh = (KFbxMesh*) pNode->GetNodeAttribute();
|
|
|
|
|
KFbxMesh* pMesh = pGeometryConverter->TriangulateMesh(pSourceMesh);
|
|
|
|
|
|
|
|
|
|
KFbxVector4* control_points = pMesh->GetControlPoints();
|
|
|
|
|
|
|
|
|
|
int polygon_count = pMesh->GetPolygonCount();
|
2012-03-28 19:06:23 +00:00
|
|
|
int uv_count = pMesh->GetElementUVCount();
|
|
|
|
|
int normal_count = pMesh->GetElementNormalCount();
|
|
|
|
|
int tangent_count = pMesh->GetElementTangentCount();
|
2012-03-28 19:34:52 +00:00
|
|
|
int elementmaterial_count = pMesh->GetElementMaterialCount();
|
|
|
|
|
int material_count = pNode->GetMaterialCount();
|
2012-03-23 02:28:46 +00:00
|
|
|
|
2012-03-28 19:06:23 +00:00
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
printf(" Polygon Count: %i (before triangulation: %i)\n", polygon_count, pSourceMesh->GetPolygonCount());
|
|
|
|
|
|
|
|
|
|
std::vector<KRVector3> vertices;
|
|
|
|
|
std::vector<KRVector2> uva;
|
2012-03-28 19:06:23 +00:00
|
|
|
std::vector<KRVector2> uvb;
|
2012-03-23 02:28:46 +00:00
|
|
|
std::vector<KRVector3> normals;
|
|
|
|
|
std::vector<KRVector3> tangents;
|
|
|
|
|
std::vector<int> submesh_lengths;
|
|
|
|
|
std::vector<int> submesh_starts;
|
|
|
|
|
std::vector<std::string> material_names;
|
|
|
|
|
|
2012-03-28 19:06:23 +00:00
|
|
|
int dest_vertex_id = 0;
|
|
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
for(int iMaterial=0; iMaterial < material_count; iMaterial++) {
|
2012-03-28 21:58:55 +00:00
|
|
|
KFbxSurfaceMaterial *pMaterial = pNode->GetMaterial(iMaterial);
|
2012-03-28 19:34:52 +00:00
|
|
|
int source_vertex_id = 0;
|
|
|
|
|
int mat_vertex_count = 0;
|
|
|
|
|
int mat_vertex_start = dest_vertex_id;
|
|
|
|
|
for(int iPolygon = 0; iPolygon < polygon_count; iPolygon++) {
|
|
|
|
|
int lPolygonSize = pMesh->GetPolygonSize(iPolygon);
|
|
|
|
|
if(lPolygonSize != 3) {
|
|
|
|
|
source_vertex_id += lPolygonSize;
|
|
|
|
|
printf(" Warning - Poly with %i vertices found. Expecting only triangles.", lPolygonSize);
|
|
|
|
|
} else {
|
|
|
|
|
// ----====---- Read SubMesh / Material Mapping ----====----
|
|
|
|
|
int iNewMaterial = -1;
|
|
|
|
|
for (int l = 0; l < elementmaterial_count; l++)
|
|
|
|
|
{
|
2012-11-30 00:32:03 +00:00
|
|
|
FbxGeometryElementMaterial* leMat = pMesh->GetElementMaterial(l);
|
2012-03-28 19:34:52 +00:00
|
|
|
if(leMat) {
|
2012-11-30 00:32:03 +00:00
|
|
|
if (leMat->GetReferenceMode() == FbxGeometryElement::eIndex || leMat->GetReferenceMode() == FbxGeometryElement::eIndexToDirect) {
|
2012-03-28 19:34:52 +00:00
|
|
|
int new_id = leMat->GetIndexArray().GetAt(iPolygon);
|
|
|
|
|
if(new_id >= 0) {
|
|
|
|
|
iNewMaterial = new_id;
|
|
|
|
|
}
|
2012-03-28 19:06:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
if(iMaterial == iNewMaterial) {
|
|
|
|
|
// ----====---- Read Vertex-level Attributes ----====----
|
|
|
|
|
for(int iVertex=0; iVertex<3; iVertex++) {
|
|
|
|
|
// ----====---- Read Vertex Position ----====----
|
|
|
|
|
int lControlPointIndex = pMesh->GetPolygonVertex(iPolygon, iVertex);
|
|
|
|
|
KFbxVector4 v = control_points[lControlPointIndex];
|
|
|
|
|
vertices.push_back(KRVector3(v[0], v[1], v[2]));
|
|
|
|
|
|
2012-04-05 20:12:39 +00:00
|
|
|
KRVector2 new_uva = KRVector2(0.0, 0.0);
|
|
|
|
|
KRVector2 new_uvb = KRVector2(0.0, 0.0);
|
|
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
|
|
|
|
|
// ----====---- Read UVs ----====----
|
2012-04-05 20:12:39 +00:00
|
|
|
|
|
|
|
|
KStringList uvNames;
|
|
|
|
|
pMesh->GetUVSetNames(uvNames);
|
|
|
|
|
if(uv_count >= 1) {
|
|
|
|
|
const char *setName = uvNames[0].Buffer();
|
|
|
|
|
KFbxVector2 uv;
|
|
|
|
|
if(pMesh->GetPolygonVertexUV(iPolygon, iVertex, setName, uv)) {
|
|
|
|
|
new_uva = KRVector2(uv[0], uv[1]);
|
|
|
|
|
}
|
2012-04-05 23:09:41 +00:00
|
|
|
uva.push_back(new_uva);
|
2012-04-05 20:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(uv_count >= 2) {
|
|
|
|
|
const char *setName = uvNames[1].Buffer();
|
|
|
|
|
KFbxVector2 uv;
|
|
|
|
|
if(pMesh->GetPolygonVertexUV(iPolygon, iVertex, setName, uv)) {
|
|
|
|
|
new_uvb = KRVector2(uv[0], uv[1]);
|
|
|
|
|
}
|
|
|
|
|
uvb.push_back(new_uvb);
|
2012-04-05 23:09:41 +00:00
|
|
|
}
|
2012-03-28 23:34:39 +00:00
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
// ----====---- Read Normals ----====----
|
2012-03-28 23:34:39 +00:00
|
|
|
|
|
|
|
|
KFbxVector4 new_normal;
|
|
|
|
|
if(pMesh->GetPolygonVertexNormal(iPolygon, iVertex, new_normal)) {
|
|
|
|
|
normals.push_back(KRVector3(new_normal[0], new_normal[1], new_normal[2]));
|
2012-03-28 19:06:23 +00:00
|
|
|
}
|
2012-03-28 19:34:52 +00:00
|
|
|
|
2012-03-28 23:34:39 +00:00
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
// ----====---- Read Tangents ----====----
|
|
|
|
|
for(int l = 0; l < tangent_count; ++l)
|
|
|
|
|
{
|
|
|
|
|
KFbxVector4 new_tangent;
|
2012-11-30 00:32:03 +00:00
|
|
|
FbxGeometryElementTangent* leTangent = pMesh->GetElementTangent(l);
|
2012-03-28 19:34:52 +00:00
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
if(leTangent->GetMappingMode() == FbxGeometryElement::eByPolygonVertex) {
|
2012-03-28 19:34:52 +00:00
|
|
|
switch (leTangent->GetReferenceMode()) {
|
2012-11-30 00:32:03 +00:00
|
|
|
case FbxGeometryElement::eDirect:
|
2012-03-28 23:34:39 +00:00
|
|
|
new_tangent = leTangent->GetDirectArray().GetAt(lControlPointIndex);
|
2012-03-28 19:34:52 +00:00
|
|
|
break;
|
2012-11-30 00:32:03 +00:00
|
|
|
case FbxGeometryElement::eIndexToDirect:
|
2012-03-28 19:34:52 +00:00
|
|
|
{
|
2012-03-28 23:34:39 +00:00
|
|
|
int id = leTangent->GetIndexArray().GetAt(lControlPointIndex);
|
2012-03-28 19:34:52 +00:00
|
|
|
new_tangent = leTangent->GetDirectArray().GetAt(id);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break; // other reference modes not shown here!
|
|
|
|
|
}
|
2012-03-28 19:06:23 +00:00
|
|
|
}
|
2012-03-28 19:34:52 +00:00
|
|
|
if(l == 0) {
|
|
|
|
|
tangents.push_back(KRVector3(new_tangent[0], new_tangent[1], new_tangent[2]));
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-28 19:06:23 +00:00
|
|
|
}
|
2012-03-28 23:34:39 +00:00
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
source_vertex_id++;
|
|
|
|
|
dest_vertex_id++;
|
|
|
|
|
mat_vertex_count++;
|
2012-03-28 19:06:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-03-23 02:28:46 +00:00
|
|
|
}
|
2012-03-28 19:34:52 +00:00
|
|
|
|
2012-03-28 21:58:55 +00:00
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
if(mat_vertex_count) {
|
2012-03-28 21:58:55 +00:00
|
|
|
// ----====---- Output last material / submesh details ----====----
|
2012-03-28 19:34:52 +00:00
|
|
|
submesh_starts.push_back(mat_vertex_start);
|
|
|
|
|
submesh_lengths.push_back(mat_vertex_count);
|
2012-03-28 21:58:55 +00:00
|
|
|
material_names.push_back(pMaterial->GetName());
|
|
|
|
|
printf(" %s: %i - %i\n", pMaterial->GetName(), mat_vertex_start, mat_vertex_count + mat_vertex_start - 1);
|
|
|
|
|
|
|
|
|
|
// ----====---- Output Material File ----====----
|
2012-09-05 18:14:08 +00:00
|
|
|
KRMaterial *new_material = new KRMaterial(parent_node->getContext(), pMaterial->GetName());
|
2012-03-28 21:58:55 +00:00
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
FbxPropertyT<FbxDouble3> lKFbxDouble3;
|
|
|
|
|
FbxPropertyT<FbxDouble> lKFbxDouble1;
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
if (pMaterial->GetClassId().Is(KFbxSurfacePhong::ClassId)) {
|
|
|
|
|
// We found a Phong material.
|
|
|
|
|
|
|
|
|
|
// Ambient Color
|
2012-11-30 00:32:03 +00:00
|
|
|
lKFbxDouble3 =((FbxSurfacePhong *) pMaterial)->Ambient;
|
2012-05-09 01:56:04 +00:00
|
|
|
new_material->setAmbient(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]));
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
// Diffuse Color
|
|
|
|
|
lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Diffuse;
|
2012-05-09 01:56:04 +00:00
|
|
|
new_material->setDiffuse(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]));
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
// Specular Color (unique to Phong materials)
|
|
|
|
|
lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Specular;
|
2012-05-09 01:56:04 +00:00
|
|
|
new_material->setSpecular(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]));
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
// Emissive Color
|
|
|
|
|
//lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Emissive;
|
|
|
|
|
|
|
|
|
|
// Transparency
|
|
|
|
|
lKFbxDouble1 =((KFbxSurfacePhong *) pMaterial)->TransparencyFactor;
|
2012-10-05 20:46:24 +00:00
|
|
|
new_material->setTransparency(lKFbxDouble1.Get());
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
// Shininess
|
|
|
|
|
lKFbxDouble1 =((KFbxSurfacePhong *) pMaterial)->Shininess;
|
|
|
|
|
new_material->setShininess(lKFbxDouble1.Get());
|
2012-05-08 23:39:52 +00:00
|
|
|
|
|
|
|
|
// Specular Factor
|
2012-04-11 19:59:08 +00:00
|
|
|
lKFbxDouble1 =((KFbxSurfacePhong *) pMaterial)->SpecularFactor;
|
|
|
|
|
double specular_factor = lKFbxDouble1.Get();
|
|
|
|
|
|
2012-10-12 00:02:24 +00:00
|
|
|
// Reflection factor
|
|
|
|
|
lKFbxDouble1 =((KFbxSurfacePhong *) pMaterial)->ReflectionFactor;
|
|
|
|
|
|
2012-05-08 23:39:52 +00:00
|
|
|
// Reflection color
|
|
|
|
|
lKFbxDouble3 =((KFbxSurfacePhong *) pMaterial)->Reflection;
|
|
|
|
|
|
2012-10-12 00:02:24 +00:00
|
|
|
// We modulate Relection color by reflection factor, as we only have one "reflection color" variable in Kraken
|
|
|
|
|
new_material->setReflection(KRVector3(lKFbxDouble3.Get()[0] * lKFbxDouble1.Get(), lKFbxDouble3.Get()[1] * lKFbxDouble1.Get(), lKFbxDouble3.Get()[2] * lKFbxDouble1.Get()));
|
|
|
|
|
|
2012-03-28 21:58:55 +00:00
|
|
|
} else if(pMaterial->GetClassId().Is(KFbxSurfaceLambert::ClassId) ) {
|
|
|
|
|
// We found a Lambert material.
|
|
|
|
|
|
|
|
|
|
// Ambient Color
|
|
|
|
|
lKFbxDouble3=((KFbxSurfaceLambert *)pMaterial)->Ambient;
|
2012-05-09 01:56:04 +00:00
|
|
|
new_material->setAmbient(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]));
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
// Diffuse Color
|
|
|
|
|
lKFbxDouble3 =((KFbxSurfaceLambert *)pMaterial)->Diffuse;
|
2012-05-09 01:56:04 +00:00
|
|
|
new_material->setDiffuse(KRVector3(lKFbxDouble3.Get()[0], lKFbxDouble3.Get()[1], lKFbxDouble3.Get()[2]));
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
// Emissive
|
|
|
|
|
//lKFbxDouble3 =((KFbxSurfaceLambert *)pMaterial)->Emissive;
|
|
|
|
|
|
|
|
|
|
// Opacity
|
|
|
|
|
lKFbxDouble1 =((KFbxSurfaceLambert *)pMaterial)->TransparencyFactor;
|
2012-10-05 20:46:24 +00:00
|
|
|
new_material->setTransparency(lKFbxDouble1.Get());
|
2012-03-28 21:58:55 +00:00
|
|
|
} else {
|
|
|
|
|
printf("Error! Unable to convert material: %s", pMaterial->GetName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KFbxProperty pProperty;
|
|
|
|
|
|
|
|
|
|
// Diffuse Map Texture
|
|
|
|
|
pProperty = pMaterial->FindProperty(KFbxSurfaceMaterial::sDiffuse);
|
|
|
|
|
if(pProperty.GetSrcObjectCount(KFbxLayeredTexture::ClassId) > 0) {
|
2012-04-11 19:59:08 +00:00
|
|
|
printf("Warning! Layered textures not supported.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int texture_count = pProperty.GetSrcObjectCount(KFbxTexture::ClassId);
|
|
|
|
|
if(texture_count > 1) {
|
|
|
|
|
printf("Error! Multiple diffuse textures not supported.\n");
|
|
|
|
|
} else if(texture_count == 1) {
|
2012-11-30 00:32:03 +00:00
|
|
|
KFbxTexture* pTexture = FbxCast <KFbxTexture> (pProperty.GetSrcObject(KFbxTexture::ClassId,0));
|
2012-04-11 19:59:08 +00:00
|
|
|
assert(!pTexture->GetSwapUV());
|
|
|
|
|
assert(pTexture->GetCroppingTop() == 0);
|
|
|
|
|
assert(pTexture->GetCroppingLeft() == 0);
|
|
|
|
|
assert(pTexture->GetCroppingRight() == 0);
|
|
|
|
|
assert(pTexture->GetCroppingBottom() == 0);
|
2012-11-30 00:32:03 +00:00
|
|
|
assert(pTexture->GetWrapModeU() == KFbxTexture::eRepeat);
|
|
|
|
|
assert(pTexture->GetWrapModeV() == KFbxTexture::eRepeat);
|
2012-04-11 19:59:08 +00:00
|
|
|
assert(pTexture->GetRotationU() == 0.0f);
|
|
|
|
|
assert(pTexture->GetRotationV() == 0.0f);
|
|
|
|
|
assert(pTexture->GetRotationW() == 0.0f);
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
KFbxFileTexture *pFileTexture = FbxCast<KFbxFileTexture>(pTexture);
|
2012-04-11 19:59:08 +00:00
|
|
|
if(pFileTexture) {
|
|
|
|
|
new_material->setDiffuseMap(KRResource::GetFileBase(pFileTexture->GetFileName()), KRVector2(pTexture->GetScaleU(), pTexture->GetScaleV()), KRVector2(pTexture->GetTranslationU(), pTexture->GetTranslationV()));
|
2012-03-28 21:58:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-04-11 19:59:08 +00:00
|
|
|
|
2012-03-28 21:58:55 +00:00
|
|
|
|
|
|
|
|
// Specular Map Texture
|
|
|
|
|
pProperty = pMaterial->FindProperty(KFbxSurfaceMaterial::sSpecular);
|
|
|
|
|
if(pProperty.GetSrcObjectCount(KFbxLayeredTexture::ClassId) > 0) {
|
2012-04-11 19:59:08 +00:00
|
|
|
printf("Warning! Layered textures not supported.\n");
|
|
|
|
|
}
|
|
|
|
|
texture_count = pProperty.GetSrcObjectCount(KFbxTexture::ClassId);
|
|
|
|
|
if(texture_count > 1) {
|
|
|
|
|
printf("Error! Multiple specular textures not supported.\n");
|
|
|
|
|
} else if(texture_count == 1) {
|
2012-11-30 00:32:03 +00:00
|
|
|
KFbxTexture* pTexture = FbxCast <KFbxTexture> (pProperty.GetSrcObject(KFbxTexture::ClassId,0));
|
|
|
|
|
KFbxFileTexture *pFileTexture = FbxCast<KFbxFileTexture>(pTexture);
|
2012-04-11 19:59:08 +00:00
|
|
|
if(pFileTexture) {
|
|
|
|
|
new_material->setSpecularMap(KRResource::GetFileBase(pFileTexture->GetFileName()), KRVector2(pTexture->GetScaleU(), pTexture->GetScaleV()), KRVector2(pTexture->GetTranslationU(), pTexture->GetTranslationV()));
|
2012-03-28 21:58:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normal Map Texture
|
|
|
|
|
pProperty = pMaterial->FindProperty(KFbxSurfaceMaterial::sNormalMap);
|
|
|
|
|
if(pProperty.GetSrcObjectCount(KFbxLayeredTexture::ClassId) > 0) {
|
2012-04-11 19:59:08 +00:00
|
|
|
printf("Warning! Layered textures not supported.\n");
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
|
|
|
|
|
texture_count = pProperty.GetSrcObjectCount<FbxTexture>();
|
2012-04-11 19:59:08 +00:00
|
|
|
if(texture_count > 1) {
|
|
|
|
|
printf("Error! Multiple normal map textures not supported.\n");
|
|
|
|
|
} else if(texture_count == 1) {
|
2012-12-01 02:03:18 +00:00
|
|
|
KFbxTexture* pTexture = pProperty.GetSrcObject<KFbxTexture>(0);
|
2012-11-30 00:32:03 +00:00
|
|
|
KFbxFileTexture *pFileTexture = FbxCast<KFbxFileTexture>(pTexture);
|
2012-04-11 19:59:08 +00:00
|
|
|
if(pFileTexture) {
|
|
|
|
|
new_material->setNormalMap(KRResource::GetFileBase(pFileTexture->GetFileName()), KRVector2(pTexture->GetScaleU(), pTexture->GetScaleV()), KRVector2(pTexture->GetTranslationU(), pTexture->GetTranslationV()));
|
2012-03-28 21:58:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool bFound = false;
|
|
|
|
|
for(vector<KRResource *>::iterator resource_itr = resources.begin(); resource_itr != resources.end(); resource_itr++) {
|
|
|
|
|
KRResource *pResource = (*resource_itr);
|
|
|
|
|
if(pResource->getName() == new_material->getName() && pResource->getExtension() == new_material->getExtension()) {
|
|
|
|
|
bFound = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(bFound) {
|
|
|
|
|
delete new_material;
|
|
|
|
|
} else {
|
|
|
|
|
resources.push_back(new_material);
|
|
|
|
|
}
|
2012-03-28 19:34:52 +00:00
|
|
|
}
|
2012-03-23 02:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-28 19:34:52 +00:00
|
|
|
|
2012-03-28 19:06:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----====---- Generate Output Mesh Object ----====----
|
2012-03-23 02:28:46 +00:00
|
|
|
|
2012-10-03 19:55:34 +00:00
|
|
|
KRModel *new_mesh = new KRModel(parent_node->getContext(), pNode->GetName());
|
2012-03-29 19:39:28 +00:00
|
|
|
new_mesh->LoadData(vertices, uva, uvb, normals, tangents, submesh_starts, submesh_lengths, material_names);
|
2012-03-23 02:28:46 +00:00
|
|
|
resources.push_back(new_mesh);
|
2012-11-02 20:50:45 +00:00
|
|
|
|
|
|
|
|
if(new_mesh->getLODCoverage() == 100) {
|
|
|
|
|
// If this is the full detail model, add an instance of it to the scene file
|
|
|
|
|
std::string light_map = pNode->GetName();
|
|
|
|
|
light_map.append("_lightmap");
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
KRInstance *new_instance = new KRInstance(parent_node->getScene(), pNode->GetName(), pNode->GetName(), light_map, 0.0f, true, false);
|
2012-11-03 02:57:35 +00:00
|
|
|
return new_instance;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
2012-11-02 20:50:45 +00:00
|
|
|
}
|
2012-11-03 02:57:35 +00:00
|
|
|
|
2012-03-23 02:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-03 02:57:35 +00:00
|
|
|
KRNode *LoadLight(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode) {
|
2012-04-12 00:43:53 +00:00
|
|
|
const GLfloat PI = 3.14159265;
|
|
|
|
|
const GLfloat d2r = PI * 2 / 360;
|
|
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
FbxLight* pLight = (FbxLight*) pNode->GetNodeAttribute();
|
2012-04-05 23:09:41 +00:00
|
|
|
const char *szName = pNode->GetName();
|
2012-04-06 01:07:23 +00:00
|
|
|
|
2012-11-30 00:32:03 +00:00
|
|
|
FbxDouble3 light_color = pLight->Color.Get();
|
|
|
|
|
FbxDouble light_intensity = pLight->Intensity.Get();
|
|
|
|
|
FbxDouble light_hotspot = pLight->InnerAngle.Get(); // light inner cone angle (in degrees). Also know as the HotSpot
|
|
|
|
|
FbxDouble light_coneangle = pLight->OuterAngle.Get(); // light outer cone angle (in degrees). Also known as the Falloff
|
2012-04-06 01:07:23 +00:00
|
|
|
KFbxLight::EDecayType light_decaytype = pLight->DecayType.Get(); // decay type
|
2012-11-30 00:32:03 +00:00
|
|
|
FbxDouble light_decaystart = pLight->DecayStart.Get(); // decay start distance
|
2012-04-06 01:07:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// KFbxLight::eNONE - does not attenuate with distance
|
|
|
|
|
// KFbxLight::eLINEAR - attenuation of 1/d
|
|
|
|
|
// KFbxLight::eQUADRATIC - attenuation of 1/d^2
|
2012-11-29 21:28:49 +00:00
|
|
|
// KFbxLight::eCUBIC - attenuation of
|
2012-04-12 00:43:53 +00:00
|
|
|
|
|
|
|
|
KRLight *new_light = NULL;
|
|
|
|
|
|
2012-04-05 23:09:41 +00:00
|
|
|
switch(pLight->LightType.Get()) {
|
2012-11-30 00:32:03 +00:00
|
|
|
case KFbxLight::ePoint:
|
2012-04-12 00:43:53 +00:00
|
|
|
{
|
2012-09-05 18:14:08 +00:00
|
|
|
KRPointLight *l = new KRPointLight(parent_node->getScene(), szName);
|
2012-04-12 00:43:53 +00:00
|
|
|
new_light = l;
|
|
|
|
|
|
|
|
|
|
}
|
2012-04-05 23:09:41 +00:00
|
|
|
break;
|
2012-11-30 00:32:03 +00:00
|
|
|
case KFbxLight::eDirectional:
|
2012-04-12 00:43:53 +00:00
|
|
|
{
|
2012-09-05 18:14:08 +00:00
|
|
|
KRDirectionalLight *l = new KRDirectionalLight(parent_node->getScene(), szName);
|
2012-04-12 00:43:53 +00:00
|
|
|
new_light = l;
|
|
|
|
|
}
|
2012-04-05 23:09:41 +00:00
|
|
|
break;
|
2012-11-30 00:32:03 +00:00
|
|
|
case KFbxLight::eSpot:
|
2012-04-12 00:43:53 +00:00
|
|
|
{
|
2012-09-05 18:14:08 +00:00
|
|
|
KRSpotLight *l = new KRSpotLight(parent_node->getScene(), szName);
|
2012-04-12 00:43:53 +00:00
|
|
|
l->setInnerAngle(light_hotspot * d2r);
|
|
|
|
|
l->setOuterAngle(light_coneangle * d2r);
|
|
|
|
|
new_light = l;
|
|
|
|
|
}
|
2012-04-05 23:09:41 +00:00
|
|
|
break;
|
2012-12-01 02:03:18 +00:00
|
|
|
case KFbxLight::eVolume:
|
2012-11-30 00:32:03 +00:00
|
|
|
case KFbxLight::eArea:
|
2012-04-05 23:09:41 +00:00
|
|
|
// Not supported yet
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-04-12 00:43:53 +00:00
|
|
|
|
|
|
|
|
if(new_light) {
|
|
|
|
|
new_light->setColor(KRVector3(light_color[0], light_color[1], light_color[2]));
|
|
|
|
|
new_light->setIntensity(light_intensity);
|
|
|
|
|
new_light->setDecayStart(light_decaystart);
|
|
|
|
|
}
|
2012-11-03 02:57:35 +00:00
|
|
|
return new_light;
|
2012-04-12 00:43:53 +00:00
|
|
|
|
2012-04-05 23:09:41 +00:00
|
|
|
}
|