/* demo_3d.c —— 第 5 层：3D（`ui_3d_*`，号段 600–634）的最小示例
 * demo_3d.c -- Layer 5: the smallest 3D example (`ui_3d_*`, opcodes 600-634)
 *
 * 与同目录 `cube_min.c` 的区别：那个用"手写顶点缓冲 + mesh_create"验接线，
 * 这个走**最常用的那条路** —— 直接从 `box.glb`（CC0）载入一个场景，
 * 开窗 → 建视口 → 相机/光照 → 画 20 帧 → 关窗。
 * Unlike `cube_min.c` (hand-built vertex buffer + mesh_create), this takes the
 * **most common path**: load a scene straight from `box.glb` (CC0), then
 * open window -> create viewport -> camera/light -> 20 frames -> close.
 *
 * 跑法 / How to run:
 *   vml run examples/c/3d/demo_3d.c
 * 桌面出帧 / Desktop frames:
 *   vmlcli Examples/c/3d/demo_3d.c --frames /tmp/d3_frames
 *
 * ⚠ 2D 层必须清成透明：3D 视口是**透过** 2D 层看的，不清就是一片黑。
 * ⚠ The 2D layer must be cleared to transparent: the 3D viewport shows **through**
 *   it, and an opaque layer is a black screen.
 */

#include <vml_ui.h>
#include <vml_3d.h>
#include <stdio.h>

int main(void) {
    int w;
    int h;
    int vp;
    int scene;
    int i;

    ui_win_open_ex("demo_3d.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);

    /* ⚠ 尺寸要在**开窗之后**问（先问会矮一截，视口会贴左上角）。
     * ⚠ Ask for the size **after** opening (asking earlier comes up short and
     *   pins the viewport to the corner). */
    w = ui_scr_get_w();
    h = ui_scr_get_h();

    vp = ui_3d_viewport_create(0, 0, w, h, 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);                                  /* 透明，见文件头 */

    scene = ui_3d_gltf_load(vp, "box.glb");
    if (scene <= 0) {
        printf("demo_3d: 载入 box.glb 失败 (scene=%d)\n", scene);
    }

    for (i = 0; i < 20; i = i + 1) {
        ui_2d_present();
        ui_msg_fetch_wait(40);
    }

    ui_win_close();
    return 0;
}
