141 lines
4.1 KiB
C++
141 lines
4.1 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<unsigned int> indices, std::vector<V3> &vertices)
|
||
{
|
||
if (!attr || attr->GetAttributeType() != FbxNodeAttribute::eMesh)
|
||
return false;
|
||
FbxMesh *mesh = static_cast<FbxMesh *>(attr);
|
||
|
||
|
||
unsigned int index_p = 0;
|
||
FbxVector4* control_points = mesh->GetControlPoints();
|
||
for (int p = 0; p < mesh->GetPolygonCount(); p++)
|
||
{
|
||
if (mesh->GetPolygonSize(p) != 3) {
|
||
// 不是三角形
|
||
return index_p;
|
||
}
|
||
for (int p_vertex = 0; p_vertex < mesh->GetPolygonSize(p); p_vertex++)
|
||
{
|
||
int vertex_indexed = mesh->GetPolygonVertex(p, p_vertex);
|
||
FbxVector4 control_point = control_points[vertex_indexed];
|
||
V3 v = V3(control_point.mData[0], control_point.mData[1], control_point.mData[2]);
|
||
vertices.push_back(v);
|
||
indices.push_back(index_p++);
|
||
}
|
||
}
|
||
|
||
return index_p;
|
||
}
|
||
|
||
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<unsigned int> indices;
|
||
std::vector<V3> vertices;
|
||
|
||
int count = load_triangles(attr, indices, vertices);
|
||
|
||
uint id = attr->GetUniqueID();
|
||
uint pid = parent_node->GetUniqueID();
|
||
|
||
printf("[%d]vertices: %ld\n", count);
|
||
// save data to database
|
||
sqlite3 *db = ctx->db;
|
||
// todo save mesh into db
|
||
|
||
save_mesh_to_table(ctx, ctx->db,
|
||
id,
|
||
pid,
|
||
attr->GetName(),
|
||
vertices.size(), indices.size(),
|
||
parent_node->GetNameOnly().Buffer());
|
||
|
||
save_mesh_vertices(ctx, id, vertices);
|
||
save_mesh_indices(ctx, id, indices);
|
||
}
|
||
}
|
||
|
||
// 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)
|
||
{
|
||
// todo 打印一下节点信息
|
||
// 1. 平移、旋转、缩放等矩阵
|
||
// 2. 名称,id, pid等
|
||
// 3. 节点其他properties
|
||
|
||
|
||
// 打印一下节点信息
|
||
printf("|");
|
||
for (int i = 0; i < level; i++)
|
||
{
|
||
printf("-");
|
||
}
|
||
printf("> ");
|
||
FbxNode* parent = node->GetParent();
|
||
uint parent_id = 0;
|
||
if (parent != NULL) {
|
||
parent_id = parent->GetUniqueID();
|
||
}
|
||
printf("[%lld/%lld]: %s\n", node->GetUniqueID(), parent_id, node->GetNameOnly().Buffer());
|
||
} |