← 都来码 / Dolaima← 例程索引d/3d/demo_3d.d 原文

d/3d/demo_3d.d · 1954 字节 · 下载原文

// demo_3d.d —— 第 5 层:3D 视口(`ui_3d_*`,独立库 vml3d)
// demo_3d.d -- Layer 5: 3D viewport (`ui_3d_*`, the separate vml3d library)
//
// 开窗 → 建视口(相机/视场/背景/灯光)→ 从同目录 `box.glb`(CC0)载入 → 20 帧 → 关窗。
// Open window -> viewport (camera/FOV/background/light) -> load `box.glb` (CC0) -> 20 frames -> close.
//
// 跑法 / How to run:
//   手机  vml run examples/d/3d/demo_3d.d
//   桌面  vmlcli Examples/d/3d/demo_3d.d --frames /tmp/f3d
//
// ⚠ 声明来自 `Lib/d/vml3d.d`(GenLib `-u`,DocTable:只有注释)。**不能带函数体**:
//    带体会生成同名空壳把库实现顶掉(E215),视口恒返回 0。
// ⚠ Declarations come from Lib/d/vml3d.d (GenLib `-u`, DocTable: comments only).
//    **No function bodies** — a body shadows the library (E215) and the viewport returns 0.
// ⚠ 几何量是 4 字节 float ⇒ 字面量要带 `f` 后缀(`0.0` 是 double,会错位)。
// ⚠ Geometry is 4-byte float, so literals carry the `f` suffix (`0.0` is a double and misaligns).
// ⚠ 3D 透过 2D 层显示 ⇒ 先 `ui_2d_clear(0)`。
// ⚠ 3D shows through the 2D layer, so `ui_2d_clear(0)` comes first.

#include <vml_ui.d>
#include <vml3d.d>

void main() {
    ui_win_open_ex("demo_3d (D)", 0, 0, 0, 0);
    int w = ui_scr_get_w();
    int h = ui_scr_get_h();
    int vp = ui_3d_viewport_create(0, 0, w, h, 0);
    if (vp > 0) {
        ui_3d_viewport_set_camera(vp, 0.0f, 1.5f, 4.0f, 0.0f, 0.0f, 0.0f);
        ui_3d_viewport_set_fov(vp, 50.0f, 0.1f, 100.0f);
        ui_3d_viewport_set_bg(vp, -15458257);              // 0xFF14202F
        ui_3d_viewport_set_light(vp, 0.4f, 0.9f, 0.6f, -1, 0.3f);
        ui_2d_clear(0);
        ui_3d_gltf_load(vp, "box.glb");
        int i = 0;
        while (i < 20) {
            ui_2d_present();
            ui_msg_fetch_wait(40);
            i = i + 1;
        }
    }
    ui_win_close();
}