add init to cube3d

This commit is contained in:
asrael 2025-11-01 15:31:05 -05:00
parent 0ed7fc4496
commit cec9415d75
No known key found for this signature in database
GPG key ID: 2786557804DFAE24
4 changed files with 72 additions and 51 deletions

View file

@ -1,6 +1,6 @@
# pxl8 # pxl8
## Quick Start ### Quick Start
```bash ```bash
./pxl8.sh build # Build framework ./pxl8.sh build # Build framework
@ -8,6 +8,9 @@
./pxl8.sh run --repl # Run demo w/ repl ./pxl8.sh run --repl # Run demo w/ repl
``` ```
**Note** These functions can also take an optional project arg: **Note** These functions can also take an optional project arg:
```bash
./pxl8.sh run <game/|game.cart>
```
> [!WARNING] > [!WARNING]
> <strong>Heavy development. So... <em>here be dragons :3</em></strong> > <strong>Heavy development. So... <em>here be dragons :3</em></strong>

View file

@ -18,6 +18,7 @@
(global init (fn [] (global init (fn []
(bsp_world.init) (bsp_world.init)
(cube3d.init)
(pxl8.load_palette "res/sprites/pxl8_logo.ase") (pxl8.load_palette "res/sprites/pxl8_logo.ase")
(set logo-sprite (pxl8.load_sprite "res/sprites/pxl8_logo.ase")) (set logo-sprite (pxl8.load_sprite "res/sprites/pxl8_logo.ase"))
(set particles (pxl8.particles_new 1000)))) (set particles (pxl8.particles_new 1000))))

View file

@ -12,7 +12,6 @@
(var texture-id nil) (var texture-id nil)
(var use-texture false) (var use-texture false)
(var affine false) (var affine false)
(var texture-initialized false)
(var cam-x 0) (var cam-x 0)
(var cam-y 2) (var cam-y 2)
(var cam-z 12) (var cam-z 12)
@ -23,12 +22,28 @@
(var fps-accumulator 0) (var fps-accumulator 0)
(var fps-frame-count 0) (var fps-frame-count 0)
(fn init-texture [] (fn init []
(when (not texture-initialized) (set angle-x 0)
(pxl8.load_palette "res/sprites/pxl8_logo.ase") (set angle-y 0)
(set angle-z 0)
(set auto-rotate true)
(set orthographic true)
(set wireframe true)
(set time 0)
(set zoom 5.0)
(set use-texture false)
(set affine false)
(set cam-x 0)
(set cam-y 2)
(set cam-z 12)
(set cam-yaw 0)
(set cam-pitch -0.2)
(set show-debug-ui false)
(set fps 0)
(set fps-accumulator 0)
(set fps-frame-count 0)
(set texture-id (pxl8.load_sprite "res/sprites/pxl8_logo.ase")) (set texture-id (pxl8.load_sprite "res/sprites/pxl8_logo.ase"))
(pxl8.upload_atlas) (pxl8.upload_atlas))
(set texture-initialized true)))
(fn make-cube-vertices [] (fn make-cube-vertices []
[[-1 -1 -1] [1 -1 -1] [1 1 -1] [-1 1 -1] [[-1 -1 -1] [1 -1 -1] [1 1 -1] [-1 1 -1]
@ -142,9 +157,7 @@
(when (pxl8.key_pressed "r") (when (pxl8.key_pressed "r")
(set auto-rotate (not auto-rotate))) (set auto-rotate (not auto-rotate)))
(when (pxl8.key_pressed "t") (when (pxl8.key_pressed "t")
(set use-texture (not use-texture)) (set use-texture (not use-texture)))
(when use-texture
(init-texture)))
(when (pxl8.key_pressed "F8") (when (pxl8.key_pressed "F8")
(set show-debug-ui (not show-debug-ui)) (set show-debug-ui (not show-debug-ui))
(pxl8.ui_window_set_open "Debug Menu (F8)" show-debug-ui)) (pxl8.ui_window_set_open "Debug Menu (F8)" show-debug-ui))
@ -154,6 +167,41 @@
(set angle-y (+ angle-y (* dt 0.5))) (set angle-y (+ angle-y (* dt 0.5)))
(set angle-z (+ angle-z (* dt 0.3))))) (set angle-z (+ angle-z (* dt 0.3)))))
(fn draw-cube [pos scale rotation-offset]
(let [[x y z] pos
model (-> (pxl8.mat4_identity)
(pxl8.mat4_multiply (pxl8.mat4_scale scale scale scale))
(pxl8.mat4_multiply (pxl8.mat4_rotate_x (+ angle-x rotation-offset)))
(pxl8.mat4_multiply (pxl8.mat4_rotate_y (+ angle-y (* rotation-offset 1.3))))
(pxl8.mat4_multiply (pxl8.mat4_rotate_z (+ angle-z (* rotation-offset 0.7))))
(pxl8.mat4_multiply (pxl8.mat4_translate x y z)))]
(pxl8.set_model model))
(let [vertices (make-cube-vertices)]
(if (and use-texture texture-id)
(let [faces (make-cube-faces-with-uvs)]
(each [_i face-data (ipairs faces)]
(let [tri-indices face-data.tri
tri-uvs face-data.uvs
v0 (. vertices (+ 1 (. tri-indices 1)))
v1 (. vertices (+ 1 (. tri-indices 2)))
v2 (. vertices (+ 1 (. tri-indices 3)))
uv0 (. tri-uvs 1)
uv1 (. tri-uvs 2)
uv2 (. tri-uvs 3)]
(pxl8.draw_triangle_3d_textured
v0 v1 v2
uv0 uv1 uv2
texture-id))))
(let [faces (make-cube-faces)]
(each [i face (ipairs faces)]
(let [[i0 i1 i2] face
v0 (. vertices (+ 1 i0))
v1 (. vertices (+ 1 i1))
v2 (. vertices (+ 1 i2))
color (get-face-color (math.floor (/ (- i 1) 2)))]
(pxl8.draw_triangle_3d v0 v1 v2 color)))))))
(fn frame [] (fn frame []
(pxl8.clr 0) (pxl8.clr 0)
@ -180,36 +228,10 @@
look-z (+ cam-z target-z)] look-z (+ cam-z target-z)]
(pxl8.set_view (pxl8.mat4_lookat [cam-x cam-y cam-z] [look-x look-y look-z] [0 1 0]))) (pxl8.set_view (pxl8.mat4_lookat [cam-x cam-y cam-z] [look-x look-y look-z] [0 1 0])))
(let [model (-> (pxl8.mat4_identity) (draw-cube [0 0 0] 1.0 0)
(pxl8.mat4_multiply (pxl8.mat4_rotate_x angle-x)) (draw-cube [-3 0 -4] 0.8 0.5)
(pxl8.mat4_multiply (pxl8.mat4_rotate_y angle-y)) (draw-cube [3 1 -7] 0.9 1.2)
(pxl8.mat4_multiply (pxl8.mat4_rotate_z angle-z)))] (draw-cube [0 -2 -10] 1.1 -0.7)
(pxl8.set_model model))
(let [vertices (make-cube-vertices)]
(if (and use-texture texture-id)
(let [faces (make-cube-faces-with-uvs)]
(each [_i face-data (ipairs faces)]
(let [tri-indices face-data.tri
tri-uvs face-data.uvs
v0 (. vertices (+ 1 (. tri-indices 1)))
v1 (. vertices (+ 1 (. tri-indices 2)))
v2 (. vertices (+ 1 (. tri-indices 3)))
uv0 (. tri-uvs 1)
uv1 (. tri-uvs 2)
uv2 (. tri-uvs 3)]
(pxl8.draw_triangle_3d_textured
v0 v1 v2
uv0 uv1 uv2
texture-id))))
(let [faces (make-cube-faces)]
(each [i face (ipairs faces)]
(let [[i0 i1 i2] face
v0 (. vertices (+ 1 i0))
v1 (. vertices (+ 1 i1))
v2 (. vertices (+ 1 i2))
color (get-face-color (math.floor (/ (- i 1) 2)))]
(pxl8.draw_triangle_3d v0 v1 v2 color))))))
(let [new-state (debug-ui.render {:show-debug-ui show-debug-ui (let [new-state (debug-ui.render {:show-debug-ui show-debug-ui
:fps fps :fps fps
@ -217,16 +239,14 @@
:auto-rotate auto-rotate :auto-rotate auto-rotate
:orthographic orthographic :orthographic orthographic
:use-texture use-texture :use-texture use-texture
:affine affine :affine affine})]
:init-texture init-texture})]
(when (not= new-state.show-debug-ui nil) (set show-debug-ui new-state.show-debug-ui)) (when (not= new-state.show-debug-ui nil) (set show-debug-ui new-state.show-debug-ui))
(when (not= new-state.wireframe nil) (set wireframe new-state.wireframe)) (when (not= new-state.wireframe nil) (set wireframe new-state.wireframe))
(when (not= new-state.auto-rotate nil) (set auto-rotate new-state.auto-rotate)) (when (not= new-state.auto-rotate nil) (set auto-rotate new-state.auto-rotate))
(when (not= new-state.orthographic nil) (set orthographic new-state.orthographic)) (when (not= new-state.orthographic nil) (set orthographic new-state.orthographic))
(when (not= new-state.use-texture nil) (when (not= new-state.use-texture nil) (set use-texture new-state.use-texture))
(set use-texture new-state.use-texture)
(when use-texture (init-texture)))
(when (not= new-state.affine nil) (set affine new-state.affine)))) (when (not= new-state.affine nil) (set affine new-state.affine))))
{:update update {:init init
:update update
:frame frame} :frame frame}

View file

@ -14,10 +14,7 @@
(let [(changed new-val) (pxl8.ui_checkbox "Orthographic" state.orthographic)] (let [(changed new-val) (pxl8.ui_checkbox "Orthographic" state.orthographic)]
(when changed (set new-state.orthographic new-val))) (when changed (set new-state.orthographic new-val)))
(let [(changed new-val) (pxl8.ui_checkbox "Texture" state.use-texture)] (let [(changed new-val) (pxl8.ui_checkbox "Texture" state.use-texture)]
(when changed (when changed (set new-state.use-texture new-val)))
(set new-state.use-texture new-val)
(when (and new-val state.init-texture)
(state.init-texture))))
(when state.use-texture (when state.use-texture
(pxl8.ui_indent 20) (pxl8.ui_indent 20)
(let [(changed new-val) (pxl8.ui_checkbox "Affine mapping" state.affine)] (let [(changed new-val) (pxl8.ui_checkbox "Affine mapping" state.affine)]