refactor some things...
This commit is contained in:
parent
3550fad638
commit
1744e689b5
25 changed files with 2396 additions and 1307 deletions
98
demo/cube3d.fnl
Normal file
98
demo/cube3d.fnl
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
(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)
|
||||
|
||||
(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 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 "r")
|
||||
(set auto-rotate (not auto-rotate)))
|
||||
(when (pxl8.key_pressed "p")
|
||||
(set orthographic (not orthographic)))
|
||||
|
||||
(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_wireframe wireframe)
|
||||
(pxl8.set_backface_culling true)
|
||||
|
||||
(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)
|
||||
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)))))
|
||||
|
||||
{:update cube-update
|
||||
:frame cube-frame}
|
||||
Loading…
Add table
Add a link
Reference in a new issue