This commit is contained in:
dqn 2025-09-08 15:51:25 +08:00
parent 0808672ba8
commit 72c0fdc1cb
5 changed files with 43 additions and 25 deletions

View File

@ -7,9 +7,9 @@ using namespace std;
extern "C" { extern "C" {
// 定义给Rust调用的接口函数 // 定义给Rust调用的接口函数
void fbx_parse(const char* fbx_path) void fbx_parse(const FttContext* ctx)
{ {
printf("%s\n", fbx_path); printf("%s\n", ctx->fbx_file);
FbxManager* lSdkManager = FbxManager::Create(); FbxManager* lSdkManager = FbxManager::Create();
if (lSdkManager){ if (lSdkManager){
printf("FbxManager created\n"); printf("FbxManager created\n");
@ -23,7 +23,7 @@ extern "C" {
printf("FbxScene creation failed.\n"); printf("FbxScene creation failed.\n");
} }
FbxImporter* lImporter = FbxImporter::Create(lSdkManager, ""); FbxImporter* lImporter = FbxImporter::Create(lSdkManager, "");
bool s = lImporter->Initialize(fbx_path, -1, lSdkManager->GetIOSettings()); bool s = lImporter->Initialize(ctx->fbx_file, -1, lSdkManager->GetIOSettings());
if (s){ if (s){
printf("FbxImporter initialized\n"); printf("FbxImporter initialized\n");
} else{ } else{
@ -34,16 +34,15 @@ extern "C" {
FbxNode* lRootNode = lScene->GetRootNode(); FbxNode* lRootNode = lScene->GetRootNode();
if (lRootNode) { if (lRootNode) {
each_node(lRootNode, 0); each_node(ctx, lRootNode, 0);
} else { } else {
printf("No root node found in the scene.\n"); printf("No root node found in the scene.\n");
} }
} }
void load_fbx_mesh_to_db(const char* fbx_path) void load_fbx_mesh_to_db(const FttContext* ctx)
{ {
FbxNode* root = get_fbx_root_node(fbx_path); FbxNode* root = get_fbx_root_node(ctx->fbx_file);
each_node(ctx, root, 0);
each_node(root, 0);
} }
} }

View File

@ -55,7 +55,7 @@ FbxNode* get_fbx_root_node(const char* fbx_path)
} }
void each_node(FbxNode* parent_node, int level) void each_node(const FttContext* ctx, FbxNode* parent_node, int level)
{ {
// info // info
print_node_info(parent_node, level); print_node_info(parent_node, level);
@ -67,12 +67,8 @@ void each_node(FbxNode* parent_node, int level)
{ {
std::vector<Triangle> triangles; std::vector<Triangle> triangles;
int count = load_triangles(attr, triangles); int count = load_triangles(attr, triangles);
printf("triangles: %ld;", triangles.size()); printf("triangles: %ld\n", triangles.size());
std::cout << "(" // save data to database
<< get<0>(triangles[0]) << ", "
<< get<1>(triangles[0]) << ", "
<< get<2>(triangles[0]) << ")"
<< std::endl;
} }
} }
@ -83,7 +79,7 @@ void each_node(FbxNode* parent_node, int level)
for (int i = 0; i < parent_node->GetChildCount(); i++) for (int i = 0; i < parent_node->GetChildCount(); i++)
{ {
FbxNode* child = parent_node->GetChild(i); FbxNode* child = parent_node->GetChild(i);
each_node(child, level + 1); each_node(ctx, child, level + 1);
} }
} }
} }

View File

@ -1,14 +1,12 @@
#include <fbxsdk.h> #include <fbxsdk.h>
#include <vector> #include <vector>
#include <tools.h>
#include <tuple>
using namespace std; using namespace std;
using Triangle = std::tuple<int, int, int>; using Triangle = std::tuple<int, int, int>;
struct Context {
};
/** /**
* FBX节点树结构 * FBX节点树结构
* @param parent_node nullptr则函数不执行任何操作 * @param parent_node nullptr则函数不执行任何操作
@ -16,7 +14,7 @@ struct Context {
* @note 访parent_node的所有直接子节点和间接子节点 * @note 访parent_node的所有直接子节点和间接子节点
* @warning parent_node无效 * @warning parent_node无效
*/ */
void each_node(FbxNode* parent_node, int level); void each_node(const FttContext* ctx, FbxNode* parent_node, int level);
/** /**
* FBX节点的基本信息 * FBX节点的基本信息
* @param node nullptr时输出警告日志 * @param node nullptr时输出警告日志

5
cpp_src/include/tools.h Normal file
View File

@ -0,0 +1,5 @@
typedef struct {
char* fbx_file;
char* out_dir;
} FttContext;

View File

@ -1,19 +1,39 @@
use std::{ffi::CString, os::raw::c_char};
fn main() { fn main() {
convert_fbx_to_3dtiles("/mnt/f/BaiduNetdiskDownload/绿科模型/绿科-三维视图-{三维}111.fbx"); convert_fbx_to_3dtiles("/root/data/绿科-三维视图-{三维}111.fbx", "/root/data/out");
}
#[repr(C)]
pub struct FttContext {
pub fbx_file: *mut c_char,
pub out_dir: *mut c_char
} }
unsafe extern "C" { unsafe extern "C" {
pub unsafe fn fbx_parse(name_in: *const u8); pub unsafe fn fbx_parse(name_in: *const u8);
pub unsafe fn load_fbx_mesh_to_db(path: *const u8); pub unsafe fn load_fbx_mesh_to_db(ctx: *const FttContext);
} }
fn convert_fbx_to_3dtiles(fbx_path: &str) { fn convert_fbx_to_3dtiles(fbx_path: &str, out_dir: &str) {
let ctx = &FttContext{
fbx_file: CString::new(fbx_path).unwrap().into_raw(),
out_dir: CString::new(out_dir).unwrap().into_raw()
};
unsafe { unsafe {
// fbx_parse(fbx_path.as_ptr() as *const u8); // fbx_parse(fbx_path.as_ptr() as *const u8);
// 1. load to disk // 1. load to disk
load_fbx_mesh_to_db(fbx_path.as_ptr() as *const u8); // fbx_path.as_ptr() as *const u8
load_fbx_mesh_to_db(ctx);
// free
let _ = CString::from_raw(ctx.fbx_file);
let _ = CString::from_raw(ctx.out_dir);
} }
// unsafe { // unsafe {