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

forth/3d/demo_3d.fth · 2460 字节 · 下载原文

\ demo_3d.fth —— 第 5 层:3D 视口(`ui_3d_*`,独立库 vml3d)
\ demo_3d.fth — 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)
\ next to this file -> 20 frames -> close.
\
\ 跑法 / How to run:
\   手机  vml run examples/forth/3d/demo_3d.fth
\   桌面  vmlcli Examples/forth/3d/demo_3d.fth --frames /tmp/f3d
\
\ ⚠ 本前端**实参是倒序压的**(`ui_2d_rect(x,y,w,h,…)` 写成 `… h w y x ui_2d_rect`)。
\ ⚠ Arguments are pushed **in reverse** in this front end.
\
\ ⚠ 3D 的几何量是 4 字节 float。Forth 的浮点字面量走**独立浮点栈**(`PUSHF`),
\    C 库从主栈读不到 ⇒ 这里传 **float32 位模式整数**(右列是它表示的数值)。
\ ⚠ 3D geometry is 4-byte float. Forth float literals use a separate float stack
\    (`PUSHF`), which the C library cannot read, so **float32 bit patterns** are
\    passed as plain integers (values on the right).
\
\   0.0=0  1.5=1069547520  4.0=1082130432  50.0=1112014848  0.1=1036831949
\   100.0=1120403456  0.4=1053609165  0.9=1063675494  0.6=1058642330  0.3=1050253722
\
\ 声明来自 `Lib/forth/vml3d.fth` 与 `Lib/forth/vml_ui.fth`(GenLib 生成)。
\ Declarations come from Lib/forth/vml3d.fth and Lib/forth/vml_ui.fth (GenLib).

#include <vml_ui.fth>
#include <vml3d.fth>

VARIABLE W
VARIABLE H
VARIABLE VP

0 0 0 0 S" demo_3d (Forth)" DROP ui_win_open_ex DROP
ui_scr_get_w W !
ui_scr_get_h H !

\ ui_3d_viewport_create(x, y, w, h, flags) —— 倒序 flags h w y x
0 H @ W @ 0 0 ui_3d_viewport_create VP !

VP @ 0 > IF
  \ set_camera(vp, ex,ey,ez, tx,ty,tz) —— 倒序 tz ty tx ez ey ex vp
  0 0 0 1082130432 1069547520 0 VP @ ui_3d_viewport_set_camera DROP
  \ set_fov(vp, fov, znear, zfar) —— 倒序 zfar znear fov vp
  1120403456 1036831949 1112014848 VP @ ui_3d_viewport_set_fov DROP
  \ set_bg(vp, argb) —— 倒序 argb vp
  -15458257 VP @ ui_3d_viewport_set_bg DROP
  \ set_light(vp, dx,dy,dz, argb, ambient) —— 倒序 ambient argb dz dy dx vp
  1050253722 -1 1058642330 1063675494 1053609165 VP @ ui_3d_viewport_set_light DROP

  0 ui_2d_clear DROP
  S" box.glb" DROP VP @ ui_3d_gltf_load DROP

  20 0 DO
    ui_2d_present DROP
    40 ui_msg_fetch_wait DROP
  LOOP
THEN

ui_win_close DROP