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

pascal/3d/demo_3d.pas · 3325 字节 · 下载原文

(* demo_3d.pas —— Pascal 3D 层:**加载 glTF 立方体并画 20 帧**
  demo_3d.pas -- Pascal 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/pascal/3d/demo_3d.pas
  同目录的 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).

  ◆ 本层的两条约定
  * Two conventions of this layer
    ① 宿主 UI 接口在 `Lib/pascal/vml_ui.pas`(`uses vml_ui` 拉进来),3D 接口在
       ① the host UI API lives in Lib/pascal/vml_ui.pas (pulled in by uses vml_ui); the 3D API lives in
       `Lib/pascal/vml3d.pas`(`uses vml3d`)。两个 `uses` 都**必需** —— Pascal 的
       Lib/pascal/vml3d.pas (uses vml3d). Both uses clauses are **required**: in Pascal a zero-arg function
       零参函数与变量语法同形,只有声明能分清 `ui_scr_get_w` 是调用还是变量。
       and a variable look identical; only a declaration tells ui_scr_get_w apart from a variable.
    ② 颜色写 `$AARRGGBB`(Pascal 的 `$` 十六进制前缀);3D 的几何量是 `Single`,
       ② write colours as $AARRGGBB ($ is Pascal's hex prefix); 3D geometry is Single,
       直接写 `0.0` / `1.5` 这类实数字面量即可(本前端把它们按 4 字节 float 压栈)。
       so real literals such as 0.0 / 1.5 work as-is (this frontend pushes them as 4-byte floats).

  ◆ 有界:固定 20 帧,每帧 `ui_msg_fetch_wait(40)` 最多等 40ms(约 0.8 秒),然后关窗。
  * Bounded: exactly 20 frames, each waiting at most 40 ms in ui_msg_fetch_wait (about 0.8 s), then close.
*)

program Demo3d;

uses vml_ui, vml3d;

var
  w, h, vp, sc, k, i: integer;

begin
  { ① 开窗:尺寸传 0 ⇒ 宿主用真实可用区 }
  { 1) Open the window: pass size 0 ⇒ the host uses the real available area }
  k := ui_win_open_ex('demo_3d', 0, 0, 0, 0);

  { ② 尺寸必须开窗之后问(开窗前只能估算) }
  { 2) The size must be asked 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 }
  vp := ui_3d_viewport_create(0, 0, w, h, 0);
  k := ui_3d_viewport_set_camera(vp, 0.0, 1.5, 4.0, 0.0, 0.0, 0.0);
  k := ui_3d_viewport_set_fov(vp, 50.0, 0.1, 100.0);
  k := ui_3d_viewport_set_bg(vp, $FF14202F);
  k := ui_3d_viewport_set_light(vp, 0.4, 0.9, 0.6, $FFFFFFFF, 0.3);

  { ④ 2D 层清成透明 —— 3D 视口是透过 2D 层看的,不清就是一片黑 }
  { 4) Clear the 2D layer to transparent -- the 3D viewport shows through it; an opaque layer is a black screen }
  k := 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
  begin
    k := ui_2d_present();
    k := ui_msg_fetch_wait(40);
  end;

  { ⑦ 关窗 }
  { 7) Close the window }
  k := ui_win_close();
end.