1
01 quickstart
茂之钳 edited this page 2026-08-01 21:27:53 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

快速开始

5 分钟从零到第一个 GLB 模型。


安装

git clone https://github.com/your-org/ViewDesignEngine.git
cd ViewDesignEngine

mkdir build && cd build
cmake .. -DBUILD_EXAMPLES=ON
cmake --build . -j$(nproc)

第一个 SDF 形状

创建文件 hello_vde.cpp:

#include <vde/sdf/sdf_primitives.h>
#include <vde/sdf/sdf_operations.h>
#include <vde/sdf/sdf_to_mesh.h>
#include <vde/mesh/marching_cubes.h>
#include <vde/mesh/halfedge_mesh.h>
#include <vde/foundation/io_gltf.h>
#include <iostream>

using namespace vde::core;
using namespace vde::sdf;
using namespace vde::mesh;
using namespace vde::foundation;

int main() {
    // 1. 定义 SDF 函数
    auto shape = [](double x, double y, double z) -> double {
        Point3D p(x, y, z);
        double d_sphere = sphere(p, 1.2);
        double d_box    = box(p, Point3D(1.5, 1.5, 0.5));
        // 平滑并集 (k=0.3 控制融合程度)
        return op_smooth_union(d_sphere, d_box, 0.3);
    };

    // 2. Marching Cubes → 三角形网格
    Point3D bmin(-2.5, -2.5, -2.5);
    Point3D bmax( 2.5,  2.5,  2.5);
    auto mc = marching_cubes(shape, 0.0, bmin, bmax, 64);

    // 3. 构建半边网格
    HalfedgeMesh mesh;
    mesh.build_from_triangles(mc.vertices, mc.triangles);

    // 4. 导出 GLB
    GltfOptions opts;
    opts.binary = true;
    opts.include_normals = true;
    write_gltf("hello_vde.glb", mesh, opts);

    std::cout << "Done! Open hello_vde.glb in viewer/index.html\n";
    return 0;
}

编译运行:

g++ -std=c++17 -I../include hello_vde.cpp -L. -lvde -o hello_vde
./hello_vde

导出 GLB

GLB(glTF Binary)可直接在以下工具中打开:

  • 本项目自带的 viewer/index.html(拖放即可)
  • Blender(File → Import → glTF 2.0)
  • 在线工具:gltf-viewer.donmccurdy.com
  • Windows/macOS 自带 3D Viewer

下一步