<?php
// demo_3d.php —— PHP 3D 层：**加载 glTF 立方体并画 20 帧**
// demo_3d.php -- PHP 3D layer: **load a glTF cube and draw 20 frames**
//
// 跑法（桌面 vmlcli）：
// How to run (desktop vmlcli):
//   dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/php/3d/demo_3d.php
// 同目录的 box.glb 是 CC0 立方体（复制自 Examples/c/3d/models/BoxVertexColors.glb）。
// box.glb next to this file is a CC0 cube (copied from Examples/c/3d/models/BoxVertexColors.glb).
//
// 本前端的写法要求：
// Coding rules of this frontend:
//   ① 3D 接口没有独立声明文件，直接按名字写；vml3d 由 `Libs=` 自动挂上。
//   ① there is no separate 3D declaration file; call by name (vml3d is auto-linked via Libs=).
//   ② **浮点形参按 float32 的位模式传**：库调用实参一律压 R0，而浮点字面量的值在 D0
//   ② **float parameters are passed as float32 bit patterns**: library-call arguments push R0, while a
//      ⇒ 直接写 1.5 送出去的是别的寄存器。所以 1.5 写成 1069547520（0x3FC00000）。
//      float literal's value lives in D0 => writing 1.5 sends the wrong register. So 1.5 is written as 1069547520.
//      每个数后面都标了它对应的浮点值。
//      Each carries its float value.

$w = 0;
$h = 0;
$vp = 0;
$sc = 0;
$k = 0;

$k = ui_win_open_ex("demo_3d", 0, 0, 0, 0);
$w = ui_scr_get_w();
$h = ui_scr_get_h();
$vp = ui_3d_viewport_create(0, 0, $w, $h, 0);
$k = ui_3d_viewport_set_camera($vp, 0, 1069547520, 1082130432, 0, 0, 0);
$k = ui_3d_viewport_set_fov($vp, 1112014848, 1036831949, 1120403456);
$k = ui_3d_viewport_set_bg($vp, -15458257);                  // 0xFF14202F
$k = ui_3d_viewport_set_light($vp, 1053609165, 1063675494, 1058642330, -1, 1050253722);
$k = ui_2d_clear(0);
$sc = ui_3d_gltf_load($vp, "box.glb");

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

$k = ui_win_close();
echo "PHP 3D demo: done", chr(10);
