This commit is contained in:
dengqn 2025-09-10 14:06:27 +08:00
parent 91b0644129
commit cfe1fa46e0
3 changed files with 43 additions and 3 deletions

View File

@ -68,8 +68,11 @@ void create_tables(sqlite3 *db)
const char *sql_indices = R"( const char *sql_indices = R"(
CREATE TABLE IF NOT EXISTS indices ( CREATE TABLE IF NOT EXISTS indices (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
index_id INTEGER NOT NULL,
mesh_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 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<V3> vertices)
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
return 0; return 0;
} }
int save_mesh_indices(FttContext* ctx, uint mesh_id, std::vector<V3> 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;
}

View File

@ -96,7 +96,8 @@ void each_node(FttContext *ctx, FbxNode *parent_node, int level)
vertices.size(), triangles.size(), vertices.size(), triangles.size(),
parent_node->GetNameOnly().Buffer()); parent_node->GetNameOnly().Buffer());
save_mesh_vertices(ctx, id, vertices); save_mesh_vertices(ctx, id, triangles);
save_mesh_indices(ctx, id, vertices);
} }
} }

View File

@ -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_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<V3> vertices); int save_mesh_vertices(FttContext* ctx, uint mesh_id, std::vector<V3> vertices);
int save_mesh_indices(FttContext* ctx, uint mesh_id, std::vector<V3> indices);