                                     ÎÌÌÌÌˆˆˆ¬ªªª¬ˆˆˆ¬ÌÌÌ¬ÌÀÌ¬ÌÀÌ¬ÌÀÌÌÌìîˆˆÌîªªÀîˆ¨ÀîÌ¬ÀÌÀ¬ÀÀÀ¬ÀÀÀ¬ÀÀÎÌÌÌÌˆˆˆ¬ªªª¬ˆˆˆ¬ÌÌÌ¬ÌÌÌ¬ÌÀÌ¬ÌÀÌÌÌìîˆˆÌîªªÀîˆ¨ÀîÌ¬ÀÌÌ¬ÀÀÀ¬ÀÀÀ¬ÀÀ                                                                                                                                                                                                                                                                                                                                                                                                ¬ÌÌÌ¬ªªª¬ª¬ª¬ªÊÌ¬ªªªŒˆˆˆÌ ÀÌÎÌÌÎÌ¬ ÌªªÀì¬ªÀîªªÀîªªÀîˆˆÌî ÀìîÌÌîî¬ÌÌÌ¬ªªª¬ª¬ª¬ªÊÌ¬ªªªŒˆˆˆÌ ÀÌÎÌÌÎÌ¬ ÌªªÀì¬ªÀîªªÀîªªÀîˆˆÌî ÀìîÌÌîî	>                                                                €>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ÌÌÌÌÌ                                                                                                               ÀÌÌÌÌÌÌÌÌÌÌÌÌÌ                                                                                                        ÀÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌˆ                                                                                                    ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌˆˆˆ                                                                                                  pwÌÌÌÌÌÌÌÌÌÌÌÌÌÌŒˆˆˆˆ                                                                                                  pwwwwwÇÌÌÌÌÌÌÌŒˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwÇÌŒˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆˆ                                                                                                  pwwwwwwwwwwwˆˆˆˆˆˆˆˆ                                                                                                   pwwwwwwwwwwwˆˆˆˆˆˆˆ                                                                                                    pwwwwwwwwwwwˆˆˆˆˆˆ                                                                                                     pwwwwwwwwwwwˆˆˆˆˆ                                                                                                       wwwwwwwwwwwˆˆˆˆˆ                                                                                                          wwwwwwwwwˆˆˆˆ                                                                                                             pwwwwwwˆˆˆ                                                                                                                pwwwwˆˆ                                                                                                                   pwwˆ                                                                                                                       u ;; title:   3D cube - Fennel
;; author:  Phoebe Harris
;; desc:    3D graphics from scratch (now in Lua)
;; license: MIT License
;; version: 0.1
;; script:  fennel
;; strict:  true

(var d 90)

(var PALETTE 0x3FC0)

(var count 0)

(var shade 1)

(var (sin cos rad) (values math.sin math.cos math.rad))

(var vAf {"x" -0.5 "y" 1 "z" 5})
(var vBf {"x" -0.5 "y" 2 "z" 5})
(var vCf {"x" 	0.5 "y" 2 "z" 5})
(var vDf {"x" 	0.5 "y" 1 "z" 5})

(var vAb {"x" -0.5 "y" 1 "z" 6})
(var vBb {"x" -0.5 "y" 2 "z" 6})
(var vCb {"x" 	0.5 "y" 2 "z" 6})
(var vDb {"x" 	0.5 "y" 1 "z" 6})

(fn viewport-to-canvas [x y]
	(values
		(+ 120 (* x (/ 240 136)))
		(+ 68  (* y (/ 240 136)))))

(fn project-vertex [v]
	(viewport-to-canvas (* v.x (/ d v.z)) (* v.y (/ d v.z))))

(fn draw-line [v1 v2 color]
	(local (x1 y1) (project-vertex v1))
	(local (x2 y2) (project-vertex v2))
	(line x1 y1 x2 y2 color))
	
(fn dot [v1 v2]
	(+
		(* v1.x v2.x)
		(* v1.y v2.y)
		(* v1.z v2.z)))
	
(fn minus [v1 v2]
	{
		"x" (- v1.x v2.x)
		"y" (- v1.y v2.y)
		"z" (- v1.z v2.z)
	})
	
(fn cross [v1 v2]
	{
		"x" (- (* v1.y v2.z) (* v1.z v2.y))
		"y" (- (* v1.z v2.x) (* v1.x v2.z))
		"z" (- (* v1.x v2.y) (* v1.y v2.x))
	})
	
(fn mag [v]
	(math.sqrt (+ 
		(* v.x v.x) 
		(* v.y v.y) 
		(* v.z v.z))))

(fn degree-towards-camera [v1 v2 v3]
	(local a (cross (minus v2 v1) (minus v3 v1)))
	(local b v1)
	(* (dot a b) (/ (mag a) (mag b))))
	
(fn replace-color [color proportion]
	(var proportion proportion)
	(var color color)
	
	(when (> proportion 1)
		(set proportion 1))
	
	(local r (peek (+ PALETTE (* color 3)  )))
	(local g (peek (+ PALETTE (* color 3) 1)))
	(local b (peek (+ PALETTE (* color 3) 2)))
	
	(set color (+ color 6))
	
	(poke (+ PALETTE (* color 3)  ) (* r proportion))
	(poke (+ PALETTE (* color 3) 1) (* g proportion))
	(poke (+ PALETTE (* color 3) 2) (* b proportion)))
	
(fn draw-tri [v1 v2 v3 color replace]
	(local angle (degree-towards-camera v1 v2 v3))
	(when (>= angle 0)
		(local (x1 y1) (project-vertex v1))
		(local (x2 y2) (project-vertex v2))
		(local (x3 y3) (project-vertex v3))
		
		(when replace
			(replace-color color (+ angle 0.5)))
		
		(tri x1 y1 x2 y2 x3 y3 (+ color (* 6 shade)))))
	
(fn rotate [count]
	(local x1 (- -1.5 (* 0.7 (sin (rad (+ 135 count))))))
	(local z1 (- 5.5 (* 0.7 (cos (rad (+ 135 count))))))

	(local x2 (- -1.5 (* 0.7 (sin (rad (+ 225 count))))))
	(local z2 (- 5.5 (* 0.7 (cos (rad (+ 225 count))))))

	(local x3 (- -1.5 (* 0.7 (sin (rad (+ 45 count))))))
	(local z3 (- 5.5 (* 0.7 (cos (rad (+ 45 count))))))
	
	(local x4 (- -1.5 (* 0.7 (sin (rad (+ 315 count))))))
	(local z4 (- 5.5 (* 0.7 (cos (rad (+ 315 count))))))
	
	(set vAf {"x" x1 "y" vAf.y "z" z1})
	(set vBf {"x" x1 "y" vBf.y "z" z1})
	(set vCf {"x" x2 "y" vCf.y "z" z2})
	(set vDf {"x" x2 "y" vDf.y "z" z2})
	
	(set vAb {"x" x3 "y" vAb.y "z" z3})
	(set vBb {"x" x3 "y" vBb.y "z" z3})
	(set vCb {"x" x4 "y" vCb.y "z" z4})
	(set vDb {"x" x4 "y" vDb.y "z" z4}))
	
(fn _G.TIC []
	(cls)
	
	(when (keyp 48)
		(set shade (- 1 shade)))
	
	(rotate count)
	
	(set count (+ count 1))
	
	(draw-tri vCb vBb vAb 1 true)
	(draw-tri vCb vAb vDb 1 false)		
	(draw-tri vCf vCb vDb 2 true)	
	(draw-tri vCf vDb vDf 2 false)	
	(draw-tri vBf vCf vDf 3 true)	
	(draw-tri vBf vDf vAf 3 false)	
	(draw-tri vBb vBf vAf 4 true)	
	(draw-tri vBb vAf vAb 4 false)	
	(draw-tri vCf vBf vBb 5 true)	
	(draw-tri vCf vBb vCb 5 false)	
	(draw-tri vAb vAf vDf 6 true)	
	(draw-tri vAb vDf vDb 6 false)	

	)