ladder/3d/demo_3d.ld · 3737 字节 · 下载原文
(* demo_3d.ld —— Ladder 3D 层:**加载 glTF 立方体并画 20 帧**
demo_3d.ld -- Ladder 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/ladder/3d/demo_3d.ld
同目录的 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).
## 本前端的三条写法要求
## Three coding rules of this frontend
① 变量**必须**在 VAR 段声明;语句以 `;` 结尾;注释用**圆括号星号**(本文件里不写
1. Variables **must** be declared in the VAR section; statements end with `;`; comments use
裸的开注释记号,因为那门前端的块注释是嵌套计深的,漏闭一个会把整个文件吞掉)。
the paren-star form (no bare opener is written in this file, because block comments nest here
and one missing close swallows the whole file).
② 3D 接口没有独立声明文件,直接按名字写(`ui_3d_*`);vml3d 由 `Libs=` 自动挂上。
2. There is no separate 3D declaration file; call by name (ui_3d_xxx), vml3d is auto-linked via Libs=.
③ **浮点形参按 float32 的位模式传**:Ladder 的数值一律是整数,库调用把实参按 32 位整数压栈
3. **Float parameters are passed as float32 bit patterns**: Ladder numbers are all integers and library
⇒ 1.5 要写 1069547520(十六进制 0x3FC00000)。下面每个数后面都标了它对应的浮点值;
calls push them as 32-bit ints => 1.5 is written as 1069547520 (hex 0x3FC00000). Each carries its float value;
颜色 `0xFF14202F` 这类十六进制**整数**是正常的,整数加减乘除也正常。
colour literals such as 0xFF14202F are ordinary hex **integers**, and integer + - * / work.
*)
PROGRAM Demo3d
VAR
W : INT;
H : INT;
VP : INT;
SC : INT;
K : INT;
I : INT;
END_VAR
BEGIN
(* ① 开窗:尺寸传 0 ⇒ 宿主用真实可用区
1. Open the window: pass size 0 => the host uses the real available area *)
ui_win_open_ex("demo_3d", 0, 0, 0, 0);
(* ② 尺寸必须开窗之后问(开窗前只能估算)
2. Ask for the size after opening (before that it is only an estimate) *)
W := ui_scr_get_w();
H := ui_scr_get_h();
(* ③ 建视口 + 相机 / 视野 / 背景 / 光
3. Create the viewport plus camera / fov / background / light
camera: eye=(0.0, 1.5, 4.0) target=(0,0,0) -> bits 0 / 1069547520 / 1082130432 *)
VP := ui_3d_viewport_create(0, 0, W, H, 0);
ui_3d_viewport_set_camera(VP, 0, 1069547520, 1082130432, 0, 0, 0);
(* fov: 50.0 / near 0.1 / far 100.0 *)
ui_3d_viewport_set_fov(VP, 1112014848, 1036831949, 1120403456);
(* 背景 0xFF14202F *)
ui_3d_viewport_set_bg(VP, 0xFF14202F);
(* 光方向 (0.4, 0.9, 0.6)、白色 0xFFFFFFFF、环境 0.3 *)
ui_3d_viewport_set_light(VP, 1053609165, 1063675494, 1058642330, 0xFFFFFFFF, 1050253722);
(* ④ 2D 层清成透明 —— 3D 视口是透过 2D 层看的
4. Clear the 2D layer to transparent -- the 3D viewport shows through it *)
ui_2d_clear(0);
(* ⑤ 载入同目录的立方体
5. Load the cube sitting next to this file *)
SC := ui_3d_gltf_load(VP, "box.glb");
(* ⑥ 画 20 帧(每帧一次 present,再等最多 40ms)
6. Draw 20 frames (one present per frame, then wait up to 40 ms) *)
FOR I := 1 TO 20 DO
ui_2d_present();
ui_msg_fetch_wait(40);
END_FOR;
(* ⑦ 关窗
7. Close the window *)
ui_win_close();
PRINT_STR "Ladder 3D demo: done"
PRINT_CHAR 10
END_PROGRAM