add proper fnl modules to demo
This commit is contained in:
parent
9bb9fa5f5b
commit
47c4f2045c
14 changed files with 510 additions and 240 deletions
151
demo/cube3d.fnl
151
demo/cube3d.fnl
|
|
@ -1,151 +0,0 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
|
||||
(var angle-x 0)
|
||||
(var angle-y 0)
|
||||
(var angle-z 0)
|
||||
(var auto-rotate true)
|
||||
(var orthographic true)
|
||||
(var wireframe true)
|
||||
(var time 0)
|
||||
(var zoom 5.0)
|
||||
(var texture-id nil)
|
||||
(var use-texture false)
|
||||
(var affine false)
|
||||
(var texture-initialized false)
|
||||
|
||||
(fn init-texture []
|
||||
(when (not texture-initialized)
|
||||
(pxl8.load_palette "sprites/pxl8_logo.ase")
|
||||
(set texture-id (pxl8.load_sprite "sprites/pxl8_logo.ase"))
|
||||
(pxl8.upload_atlas)
|
||||
(set texture-initialized true)))
|
||||
|
||||
(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]])
|
||||
|
||||
(fn make-cube-faces []
|
||||
[[0 1 2] [0 2 3]
|
||||
[1 5 6] [1 6 2]
|
||||
[5 4 7] [5 7 6]
|
||||
[4 0 3] [4 3 7]
|
||||
[3 2 6] [3 6 7]
|
||||
[4 5 1] [4 1 0]])
|
||||
|
||||
(fn make-cube-faces-with-uvs []
|
||||
[{:tri [0 1 2] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [0 2 3] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [1 5 6] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [1 6 2] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [5 4 7] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [5 7 6] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [4 0 3] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [4 3 7] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [3 2 6] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [3 6 7] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [4 5 1] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [4 1 0] :uvs [[0 0] [1 1] [0 1]]}])
|
||||
|
||||
(fn get-face-color [face-idx]
|
||||
(let [colors [12 22 30 16 28 20]]
|
||||
(. colors (+ 1 (% face-idx 6)))))
|
||||
|
||||
(fn cube-update [dt]
|
||||
(set time (+ time dt))
|
||||
|
||||
(when (pxl8.key_down "w")
|
||||
(set angle-x (- angle-x (* dt 2.0))))
|
||||
(when (pxl8.key_down "s")
|
||||
(set angle-x (+ angle-x (* dt 2.0))))
|
||||
(when (pxl8.key_down "a")
|
||||
(set angle-y (- angle-y (* dt 2.0))))
|
||||
(when (pxl8.key_down "d")
|
||||
(set angle-y (+ angle-y (* dt 2.0))))
|
||||
(when (pxl8.key_down "q")
|
||||
(set angle-z (- angle-z (* dt 2.0))))
|
||||
(when (pxl8.key_down "e")
|
||||
(set angle-z (+ angle-z (* dt 2.0))))
|
||||
|
||||
(when (pxl8.key_pressed " ")
|
||||
(set wireframe (not wireframe)))
|
||||
(when (pxl8.key_pressed "f")
|
||||
(set affine (not affine)))
|
||||
(when (pxl8.key_pressed "p")
|
||||
(set orthographic (not orthographic)))
|
||||
(when (pxl8.key_pressed "r")
|
||||
(set auto-rotate (not auto-rotate)))
|
||||
(when (pxl8.key_pressed "t")
|
||||
(set use-texture (not use-texture))
|
||||
(when use-texture
|
||||
(init-texture)))
|
||||
|
||||
(when (pxl8.key_down "=")
|
||||
(set zoom (- zoom (* dt 2.0))))
|
||||
(when (pxl8.key_down "-")
|
||||
(set zoom (+ zoom (* dt 2.0))))
|
||||
(set zoom (math.max 1.0 (math.min zoom 20.0)))
|
||||
|
||||
(when auto-rotate
|
||||
(set angle-x (+ angle-x (* dt 0.7)))
|
||||
(set angle-y (+ angle-y (* dt 0.5)))
|
||||
(set angle-z (+ angle-z (* dt 0.3)))))
|
||||
|
||||
(fn cube-frame []
|
||||
(pxl8.clr 0)
|
||||
|
||||
(pxl8.clear_zbuffer)
|
||||
(pxl8.set_affine_textures affine)
|
||||
(pxl8.set_backface_culling true)
|
||||
(pxl8.set_wireframe wireframe)
|
||||
|
||||
(if orthographic
|
||||
(let [size (* 2.5 (/ zoom 5.0))
|
||||
aspect (/ (pxl8.get_width) (pxl8.get_height))
|
||||
w (* size aspect)
|
||||
h size]
|
||||
(pxl8.set_projection (pxl8.mat4_ortho (- w) w (- h) h 1.0 50.0)))
|
||||
(let [aspect (/ (pxl8.get_width) (pxl8.get_height))
|
||||
fov (/ 3.14159 (+ 2.0 (/ zoom 5.0)))]
|
||||
(pxl8.set_projection (pxl8.mat4_perspective fov aspect 1.0 50.0))))
|
||||
|
||||
(pxl8.set_view (pxl8.mat4_lookat [0 0 zoom] [0 0 0] [0 1 0]))
|
||||
|
||||
(let [model (-> (pxl8.mat4_identity)
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_x angle-x))
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_y angle-y))
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_z angle-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))))))
|
||||
|
||||
(pxl8.text "WASD/QE: Rotate | +/-: Zoom | Space: Wire | R: Auto | P: Proj" 5 5 15)
|
||||
(pxl8.text (.. "T: Texture | F: Affine | Mode: "
|
||||
(if wireframe "Wire" "Fill")
|
||||
" | Texture: " (if use-texture "On" "Off")
|
||||
" | Mapping: " (if affine "Affine" "Persp")) 5 15 15))
|
||||
|
||||
{:update cube-update
|
||||
:frame cube-frame}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
(local cube3d (fennel.dofile "cube3d.fnl"))
|
||||
(local cube3d (require :mod.cube3d))
|
||||
|
||||
(var time 0)
|
||||
(var current-effect 1)
|
||||
|
|
|
|||
213
demo/mod/cube3d.fnl
Normal file
213
demo/mod/cube3d.fnl
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
(local debug-ui (require :mod.debug_ui))
|
||||
|
||||
(var angle-x 0)
|
||||
(var angle-y 0)
|
||||
(var angle-z 0)
|
||||
(var auto-rotate true)
|
||||
(var orthographic true)
|
||||
(var wireframe true)
|
||||
(var time 0)
|
||||
(var zoom 5.0)
|
||||
(var texture-id nil)
|
||||
(var use-texture false)
|
||||
(var affine false)
|
||||
(var texture-initialized false)
|
||||
(var cam-x 0)
|
||||
(var cam-y 2)
|
||||
(var cam-z 12)
|
||||
(var cam-yaw 0)
|
||||
(var cam-pitch -0.2)
|
||||
(var show-debug-ui false)
|
||||
(var fps 0)
|
||||
(var fps-timer 0)
|
||||
(var fps-accumulator 0)
|
||||
(var fps-frame-count 0)
|
||||
|
||||
(fn init-texture []
|
||||
(when (not texture-initialized)
|
||||
(pxl8.load_palette "sprites/pxl8_logo.ase")
|
||||
(set texture-id (pxl8.load_sprite "sprites/pxl8_logo.ase"))
|
||||
(pxl8.upload_atlas)
|
||||
(set texture-initialized true)))
|
||||
|
||||
(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]])
|
||||
|
||||
(fn make-cube-faces []
|
||||
[[0 1 2] [0 2 3]
|
||||
[1 5 6] [1 6 2]
|
||||
[5 4 7] [5 7 6]
|
||||
[4 0 3] [4 3 7]
|
||||
[3 2 6] [3 6 7]
|
||||
[4 5 1] [4 1 0]])
|
||||
|
||||
(fn make-cube-faces-with-uvs []
|
||||
[{:tri [0 1 2] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [0 2 3] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [1 5 6] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [1 6 2] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [5 4 7] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [5 7 6] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [4 0 3] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [4 3 7] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [3 2 6] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [3 6 7] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [4 5 1] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [4 1 0] :uvs [[0 0] [1 1] [0 1]]}])
|
||||
|
||||
(fn get-face-color [face-idx]
|
||||
(let [colors [12 22 30 16 28 20]]
|
||||
(. colors (+ 1 (% face-idx 6)))))
|
||||
|
||||
(fn update [dt]
|
||||
(set time (+ time dt))
|
||||
(set fps-accumulator (+ fps-accumulator dt))
|
||||
(set fps-frame-count (+ fps-frame-count 1))
|
||||
|
||||
(when (>= fps-accumulator 0.25)
|
||||
(set fps (/ fps-frame-count fps-accumulator))
|
||||
(set fps-accumulator 0)
|
||||
(set fps-frame-count 0))
|
||||
|
||||
(let [wheel-y (pxl8.mouse_wheel_y)
|
||||
zoom-speed 0.5]
|
||||
(when (not= wheel-y 0)
|
||||
(let [forward-x (* (math.sin cam-yaw) wheel-y zoom-speed)
|
||||
forward-z (* (math.cos cam-yaw) wheel-y zoom-speed)]
|
||||
(set cam-x (+ cam-x forward-x))
|
||||
(set cam-z (- cam-z forward-z)))))
|
||||
|
||||
(let [move-speed 5.0
|
||||
rot-speed 2.0
|
||||
forward-x (* (math.sin cam-yaw) move-speed dt)
|
||||
forward-z (* (math.cos cam-yaw) move-speed dt)
|
||||
right-x (* (math.cos cam-yaw) move-speed dt)
|
||||
right-z (* (- (math.sin cam-yaw)) move-speed dt)]
|
||||
|
||||
(when (pxl8.key_down "w")
|
||||
(set cam-x (+ cam-x forward-x))
|
||||
(set cam-z (- cam-z forward-z)))
|
||||
(when (pxl8.key_down "s")
|
||||
(set cam-x (- cam-x forward-x))
|
||||
(set cam-z (+ cam-z forward-z)))
|
||||
(when (pxl8.key_down "a")
|
||||
(set cam-x (- cam-x right-x))
|
||||
(set cam-z (- cam-z right-z)))
|
||||
(when (pxl8.key_down "d")
|
||||
(set cam-x (+ cam-x right-x))
|
||||
(set cam-z (+ cam-z right-z)))
|
||||
(when (pxl8.key_down "q")
|
||||
(set cam-y (- cam-y (* move-speed dt))))
|
||||
(when (pxl8.key_down "e")
|
||||
(set cam-y (+ cam-y (* move-speed dt))))
|
||||
|
||||
(when (pxl8.key_down "left")
|
||||
(set cam-yaw (- cam-yaw (* rot-speed dt))))
|
||||
(when (pxl8.key_down "right")
|
||||
(set cam-yaw (+ cam-yaw (* rot-speed dt))))
|
||||
(when (pxl8.key_down "up")
|
||||
(set cam-pitch (+ cam-pitch (* rot-speed dt))))
|
||||
(when (pxl8.key_down "down")
|
||||
(set cam-pitch (- cam-pitch (* rot-speed dt))))
|
||||
|
||||
(set cam-pitch (math.max -1.5 (math.min cam-pitch 1.5))))
|
||||
|
||||
(when (pxl8.key_pressed " ")
|
||||
(set wireframe (not wireframe)))
|
||||
(when (pxl8.key_pressed "f")
|
||||
(set affine (not affine)))
|
||||
(when (pxl8.key_pressed "p")
|
||||
(set orthographic (not orthographic)))
|
||||
(when (pxl8.key_pressed "r")
|
||||
(set auto-rotate (not auto-rotate)))
|
||||
(when (pxl8.key_pressed "t")
|
||||
(set use-texture (not use-texture))
|
||||
(when use-texture
|
||||
(init-texture)))
|
||||
(when (pxl8.key_pressed "F8")
|
||||
(set show-debug-ui (not show-debug-ui))
|
||||
(pxl8.ui_window_set_open "Debug Menu (F8)" show-debug-ui))
|
||||
|
||||
(when auto-rotate
|
||||
(set angle-x (+ angle-x (* dt 0.7)))
|
||||
(set angle-y (+ angle-y (* dt 0.5)))
|
||||
(set angle-z (+ angle-z (* dt 0.3)))))
|
||||
|
||||
(fn frame []
|
||||
(pxl8.clr 0)
|
||||
|
||||
(pxl8.clear_zbuffer)
|
||||
(pxl8.set_affine_textures affine)
|
||||
(pxl8.set_backface_culling true)
|
||||
(pxl8.set_wireframe wireframe)
|
||||
|
||||
(if orthographic
|
||||
(let [size 2.5
|
||||
aspect (/ (pxl8.get_width) (pxl8.get_height))
|
||||
w (* size aspect)
|
||||
h size]
|
||||
(pxl8.set_projection (pxl8.mat4_ortho (- w) w (- h) h 1.0 100.0)))
|
||||
(let [aspect (/ (pxl8.get_width) (pxl8.get_height))
|
||||
fov (* (/ 60.0 180.0) 3.14159)]
|
||||
(pxl8.set_projection (pxl8.mat4_perspective fov aspect 0.1 100.0))))
|
||||
|
||||
(let [target-x (* (math.sin cam-yaw) (math.cos cam-pitch))
|
||||
target-y (* (math.sin cam-pitch))
|
||||
target-z (* (- (math.cos cam-yaw)) (math.cos cam-pitch))
|
||||
look-x (+ cam-x target-x)
|
||||
look-y (+ cam-y target-y)
|
||||
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])))
|
||||
|
||||
(let [model (-> (pxl8.mat4_identity)
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_x angle-x))
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_y angle-y))
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_z angle-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))))))
|
||||
|
||||
(let [new-state (debug-ui.render {:show-debug-ui show-debug-ui
|
||||
:fps fps
|
||||
:wireframe wireframe
|
||||
:auto-rotate auto-rotate
|
||||
:orthographic orthographic
|
||||
:use-texture use-texture
|
||||
: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.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.orthographic nil) (set orthographic new-state.orthographic))
|
||||
(when (not= new-state.use-texture nil)
|
||||
(set use-texture new-state.use-texture)
|
||||
(when use-texture (init-texture)))
|
||||
(when (not= new-state.affine nil) (set affine new-state.affine))))
|
||||
|
||||
{: update
|
||||
: frame}
|
||||
32
demo/mod/debug_ui.fnl
Normal file
32
demo/mod/debug_ui.fnl
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
|
||||
(fn render [state]
|
||||
(var new-state {})
|
||||
(when state.show-debug-ui
|
||||
(let [window-h (if state.use-texture 210 180)
|
||||
window-open (pxl8.ui_window_begin "Debug Menu (F8)" 10 10 250 window-h)]
|
||||
(when window-open
|
||||
(pxl8.ui_layout_row 1 0 0)
|
||||
(pxl8.ui_label (string.format "FPS: %.0f" (or state.fps 0)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Wireframe" state.wireframe)]
|
||||
(when changed (set new-state.wireframe new-val)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Auto-rotate" state.auto-rotate)]
|
||||
(when changed (set new-state.auto-rotate new-val)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Orthographic" state.orthographic)]
|
||||
(when changed (set new-state.orthographic new-val)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Texture" state.use-texture)]
|
||||
(when changed
|
||||
(set new-state.use-texture new-val)
|
||||
(when (and new-val state.init-texture)
|
||||
(state.init-texture))))
|
||||
(when state.use-texture
|
||||
(pxl8.ui_indent 20)
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Affine mapping" state.affine)]
|
||||
(when changed (set new-state.affine new-val)))
|
||||
(pxl8.ui_indent -20))
|
||||
(pxl8.ui_window_end))
|
||||
(when (not window-open)
|
||||
(set new-state.show-debug-ui false))))
|
||||
new-state)
|
||||
|
||||
{: render}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
|
||||
(var button-clicks 0)
|
||||
|
||||
(global init (fn []
|
||||
(pxl8.load_palette "palettes/gruvbox.ase")))
|
||||
|
||||
(global update (fn [_dt]))
|
||||
|
||||
(global frame (fn []
|
||||
(pxl8.clr 1)
|
||||
|
||||
(when pxl8.ui
|
||||
(when (pxl8.ui_window_begin "UI Demo" 20 20 280 150)
|
||||
(pxl8.ui_layout_row 1 0 0)
|
||||
(pxl8.ui_label "Welcome to some window UI!")
|
||||
(pxl8.ui_label (.. "Clicks: " button-clicks))
|
||||
(when (pxl8.ui_button "Click me!")
|
||||
(set button-clicks (+ button-clicks 1)))
|
||||
(pxl8.ui_window_end)))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue