diff --git a/cpp_src/db_ops.cpp b/cpp_src/db_ops.cpp index d6e7480..6be7882 100644 --- a/cpp_src/db_ops.cpp +++ b/cpp_src/db_ops.cpp @@ -68,8 +68,11 @@ void create_tables(sqlite3 *db) const char *sql_indices = R"( CREATE TABLE IF NOT EXISTS indices ( id INTEGER PRIMARY KEY AUTOINCREMENT, + index_id INTEGER NOT NULL, mesh_id INTEGER NOT NULL, - value INTEGER NOT NULL, + position_x REAL NOT NULL, + position_y REAL NOT NULL, + position_z REAL NOT NULL, FOREIGN KEY(mesh_id) REFERENCES meshes(id) ON DELETE CASCADE ); )"; @@ -189,3 +192,38 @@ int save_mesh_vertices(FttContext *ctx, uint mesh_id, std::vector vertices) sqlite3_finalize(stmt); return 0; } + +int save_mesh_indices(FttContext* ctx, uint mesh_id, std::vector indices) { + if (!ctx->db) return -1; + + sqlite3_stmt *stmt = nullptr; + const char *sql = "INSERT OR IGNORE INTO indices (mesh_id, index_id, position_x, position_y, position_z) VALUES (?, ?, ?, ?, ?);"; + int rc = sqlite3_prepare_v2(ctx->db, sql, -1, &stmt, nullptr); + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL prepare failed: %s\n", sqlite3_errmsg(ctx->db)); + return rc; + } + + sqlite3_exec(ctx->db, "BEGIN TRANSACTION;", nullptr, nullptr, nullptr); // 开启事务 + + for (int i = 0; i < indices.size(); i++) { + sqlite3_reset(stmt); // 重置语句 + sqlite3_bind_int64(stmt, 1, mesh_id); + sqlite3_bind_int64(stmt, 2, i); // vert_id使用索引 + sqlite3_bind_double(stmt, 3, std::get<0>(indices[i])); // 修正为double类型绑定 + sqlite3_bind_double(stmt, 4, std::get<1>(indices[i])); + sqlite3_bind_double(stmt, 5, std::get<2>(indices[i])); + + rc = sqlite3_step(stmt); + if (rc != SQLITE_DONE && rc != SQLITE_CONSTRAINT) { + fprintf(stderr, "SQL step failed at vertex %d: %s\n", i, sqlite3_errmsg(ctx->db)); + sqlite3_exec(ctx->db, "ROLLBACK;", nullptr, nullptr, nullptr); + sqlite3_finalize(stmt); + return rc; + } + } + + sqlite3_exec(ctx->db, "COMMIT;", nullptr, nullptr, nullptr); // 提交事务 + sqlite3_finalize(stmt); + return 0; +} diff --git a/cpp_src/fbx_wrap.cpp b/cpp_src/fbx_wrap.cpp index c844189..17124bb 100644 --- a/cpp_src/fbx_wrap.cpp +++ b/cpp_src/fbx_wrap.cpp @@ -96,7 +96,8 @@ void each_node(FttContext *ctx, FbxNode *parent_node, int level) vertices.size(), triangles.size(), parent_node->GetNameOnly().Buffer()); - save_mesh_vertices(ctx, id, vertices); + save_mesh_vertices(ctx, id, triangles); + save_mesh_indices(ctx, id, vertices); } } diff --git a/cpp_src/include/db_ops.h b/cpp_src/include/db_ops.h index 5c14f4c..cc9928f 100644 --- a/cpp_src/include/db_ops.h +++ b/cpp_src/include/db_ops.h @@ -34,4 +34,5 @@ void create_tables(sqlite3* db); */ int save_mesh_to_table(FttContext* ctx, sqlite3 *db, uint id, const std::string &name, int vertex_count, int index_count, const std::string &fbx_id); -int save_mesh_vertices(FttContext* ctx, uint mesh_id, std::vector vertices); \ No newline at end of file +int save_mesh_vertices(FttContext* ctx, uint mesh_id, std::vector vertices); +int save_mesh_indices(FttContext* ctx, uint mesh_id, std::vector indices); \ No newline at end of file