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

ruby/3d/demo_3d.rb · 4126 字节 · 下载原文

# demo_3d.rb —— 第 5 层:**3D 视口**(`ui_3d_*`,独立库 vml3d)
# demo_3d.rb — Layer 5: **3D viewport** (`ui_3d_*`, the separate vml3d library)
#
# 这一层画在 2D 画布之上:视口自带相机 / 视场角 / 背景色 / 一盏方向光,
# 模型从同目录的 `box.glb`(CC0,glTF 2.0)装载;2D 那层只负责清屏。
# This layer draws on top of the 2D canvas: the viewport owns a camera, field of view,
# background colour and one directional light; the model comes from `box.glb` (CC0,
# glTF 2.0) next to this file, while the 2D layer only clears the canvas.
#
# 跑法 / How to run:
#   手机  vml run examples/ruby/3d/demo_3d.rb
#   桌面  vmlcli Examples/ruby/3d/demo_3d.rb --screen 640x480 --frames /tmp/f3d
#
# ⚠ 三点实测要求 / Three measured requirements:
#   ① 3D 是**透过** 2D 层显示的 ⇒ 必须 `ui_2d_clear(0)`;不透明底会把 3D 整片盖住。
#      The 3D output shows **through** the 2D layer, so `ui_2d_clear(0)` is mandatory.
#   ② `box.glb` 走**相对路径**——宿主沙箱根就是源文件所在目录。
#      `box.glb` uses a **relative** path: the sandbox root is the source-file directory.
#   ③ ⚠ **float 形参用「位模式整数」传**:本前端把浮点字面量按 **8 字节 double** 压栈,
#      而 `ui_3d_*` 是 C 侧接口、从主栈按 **4 字节 float** 读形参 ⇒ 直接写 `1.5` 会静默
#      错位。这里改传 IEEE-754 的 32 位位模式(4 字节一格,C 侧读到的正是那个 float)。
#      ⚠ **Pass float parameters as bit-pattern integers**: this frontend pushes float
#      literals as **8-byte doubles**, while `ui_3d_*` is a C-side interface reading
#      **4-byte floats** from the main stack — writing `1.5` silently misaligns them.
#      The IEEE-754 32-bit patterns are passed instead (one 4-byte slot each).
require 'vml_ui'
require 'vml3d'

frames = 20                 # 20 帧 × 40ms ≈ 0.8 秒 / 20 frames x 40ms ~ 0.8s

# float32 位模式常量(右侧是它们表示的数值)/ float32 bit patterns (values on the right)
f_0   = 0            # 0.0
f_1p5 = 1069547520   # 1.5
f_4   = 1082130432   # 4.0
f_50  = 1112014848   # 50.0
f_0p1 = 1036831949   # 0.1
f_100 = 1120403456   # 100.0
f_0p4 = 1053609165   # 0.4
f_0p9 = 1063675494   # 0.9
f_0p6 = 1058642330   # 0.6
f_0p3 = 1050253722   # 0.3

# ① 开窗:宽高传 0 = 交给宿主用真实可用区
# ① Open the window: size 0 = the host picks the real usable area
ui_win_open_ex("demo_3d (Ruby)", 0, 0, 0, 0)
w = ui_scr_get_w()
h = ui_scr_get_h()

# ② 建视口 + 相机 / 视场 / 背景 / 灯光
# ② Create the viewport plus camera / FOV / background / light
vp = ui_3d_viewport_create(0, 0, w, h, 0)
# 相机:eye (0, 1.5, 4) → at (0, 0, 0)
ui_3d_viewport_set_camera(vp, f_0, f_1p5, f_4, f_0, f_0, f_0)
# 视场角 50°、近 0.1、远 100 / FOV 50 deg, near 0.1, far 100
ui_3d_viewport_set_fov(vp, f_50, f_0p1, f_100)
# 背景 0xFF14202F(写**有符号十进制** -15458257:本前端把超过 int32 的十六进制
# 字面量当 8 字节 double 压栈,同样会与 C 侧的 4 字节形参错位);灯光白 = -1(0xFFFFFFFF)
# Background 0xFF14202F (written as **signed decimal** -15458257: this frontend pushes
# hex literals above int32 as 8-byte doubles, which misaligns the 4-byte C parameters);
# light white = -1 (0xFFFFFFFF)
ui_3d_viewport_set_bg(vp, -15458257)
ui_3d_viewport_set_light(vp, f_0p4, f_0p9, f_0p6, -1, f_0p3)

# ③ 2D 层清成透明,让 3D 透出来 / Clear the 2D layer to transparent so the 3D shows through
ui_2d_clear(0)

# ④ 装载模型(返回场景句柄)/ Load the model (returns a scene handle)
scene = ui_3d_gltf_load(vp, "box.glb")

# ⑤ 有界渲染循环:交帧 + 等一条消息 / Bounded render loop: present + wait for one message
i = 0
while i < frames
  ui_2d_present()
  ui_msg_fetch_wait(40)
  i = i + 1
end

# ⑥ 收尾:释放场景 / 视口,再关窗
# ⑥ Wrap-up: free the scene/viewport, then close the window
ui_3d_gltf_free(scene)
ui_3d_viewport_free(vp)
ui_win_close()
print("demo_3d done (Ruby)\n")