125 lines
3.7 KiB
C++
125 lines
3.7 KiB
C++
#include <fbx_wrap.h>
|
|
#include <stdio.h>
|
|
#include <iostream>
|
|
#include <db_ops.h>
|
|
#include <tools.h>
|
|
|
|
using namespace std;
|
|
|
|
int load_triangles(fbxsdk::FbxNodeAttribute *attr, std::vector<V3> &indices, std::vector<V3> &triangles)
|
|
{
|
|
if (!attr || attr->GetAttributeType() != FbxNodeAttribute::eMesh)
|
|
return false;
|
|
FbxMesh *mesh = static_cast<FbxMesh *>(attr);
|
|
|
|
int polyCount = mesh->GetPolygonCount();
|
|
for (int i = 0; i < polyCount; ++i)
|
|
{
|
|
int vertexCount = mesh->GetPolygonSize(i);
|
|
std::vector<int> polyIndices;
|
|
for (int j = 0; j < vertexCount; ++j)
|
|
{
|
|
polyIndices.push_back(mesh->GetPolygonVertex(i, j));
|
|
}
|
|
for (int j = 2; j < vertexCount; ++j)
|
|
{
|
|
indices.push_back({polyIndices[0], polyIndices[j - 1], polyIndices[j]});
|
|
for (int k : {0, j - 1, j})
|
|
{
|
|
FbxVector4 pos = mesh->GetControlPointAt(polyIndices[k]);
|
|
triangles.push_back({(float)pos[0], (float)pos[1], (float)pos[2]});
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
FbxNode *get_fbx_root_node(const char *fbx_path)
|
|
{
|
|
printf("loading fbx file: %s\n", fbx_path);
|
|
FbxManager *lSdkManager = FbxManager::Create();
|
|
if (lSdkManager)
|
|
{
|
|
printf("FbxManager created successfully.\n");
|
|
}
|
|
else
|
|
{
|
|
printf("FbxManager creation failed.\n");
|
|
}
|
|
FbxScene *lScene = FbxScene::Create(lSdkManager, "myScene");
|
|
if (lScene)
|
|
{
|
|
printf("FbxScene created successfully.\n");
|
|
}
|
|
else
|
|
{
|
|
printf("FbxScene creation failed.\n");
|
|
}
|
|
FbxImporter *lImporter = FbxImporter::Create(lSdkManager, "");
|
|
bool s = lImporter->Initialize(fbx_path, -1, lSdkManager->GetIOSettings());
|
|
if (s)
|
|
{
|
|
printf("FbxImporter initialized successfully.\n");
|
|
}
|
|
else
|
|
{
|
|
printf("FbxImporter initialization failed.\n");
|
|
}
|
|
lImporter->Import(lScene);
|
|
lImporter->Destroy();
|
|
|
|
return lScene->GetRootNode();
|
|
}
|
|
|
|
void each_node(FttContext *ctx, FbxNode *parent_node, int level)
|
|
{
|
|
// info
|
|
print_node_info(parent_node, level);
|
|
// data
|
|
for (int i = 0; i < parent_node->GetNodeAttributeCount(); i++)
|
|
{
|
|
FbxNodeAttribute *attr = parent_node->GetNodeAttributeByIndex(i);
|
|
if (attr && attr->GetAttributeType() == FbxNodeAttribute::eMesh)
|
|
{
|
|
std::vector<V3> vertices;
|
|
std::vector<V3> triangles;
|
|
int count = load_triangles(attr, vertices, triangles);
|
|
uint id = attr->GetUniqueID();
|
|
printf("[%d]triangles: %ld\n", id, triangles.size());
|
|
// save data to database
|
|
sqlite3 *db = ctx->db;
|
|
// todo save mesh into db
|
|
|
|
save_mesh_to_table(ctx, ctx->db,
|
|
id,
|
|
attr->GetName(),
|
|
vertices.size(), triangles.size(),
|
|
parent_node->GetNameOnly().Buffer());
|
|
|
|
save_mesh_vertices(ctx, id, triangles);
|
|
save_mesh_indices(ctx, id, vertices);
|
|
}
|
|
}
|
|
|
|
// children
|
|
if (parent_node->GetChildCount())
|
|
{
|
|
for (int i = 0; i < parent_node->GetChildCount(); i++)
|
|
{
|
|
FbxNode *child = parent_node->GetChild(i);
|
|
each_node(ctx, child, level + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void print_node_info(FbxNode *node, int level)
|
|
{
|
|
// 打印一下节点信息
|
|
printf("|");
|
|
for (int i = 0; i < level; i++)
|
|
{
|
|
printf("-");
|
|
}
|
|
printf("> ");
|
|
printf("[%lld]: %s\n", node->GetUniqueID(), node->GetNameOnly().Buffer());
|
|
} |