summaryrefslogtreecommitdiff
path: root/examples/model.scm
diff options
context:
space:
mode:
Diffstat (limited to 'examples/model.scm')
-rw-r--r--examples/model.scm73
1 files changed, 73 insertions, 0 deletions
diff --git a/examples/model.scm b/examples/model.scm
new file mode 100644
index 0000000..cc73918
--- /dev/null
+++ b/examples/model.scm
@@ -0,0 +1,73 @@
+(use-modules (chickadee)
+ (chickadee math)
+ (chickadee math matrix)
+ (chickadee math vector)
+ (chickadee render)
+ (chickadee render model)
+ (chickadee render font)
+ (ice-9 format))
+
+(define projection (perspective-projection (/ pi 3) (/ 4.0 3.0) 0.1 500.0))
+(define rotation (make-identity-matrix4))
+(define translation (make-identity-matrix4))
+(define view-matrix (make-identity-matrix4))
+(define model-matrix (make-identity-matrix4))
+(define position (vec3 0.0 0.0 0.0))
+(define velocity (vec3 0.0 0.0 0.0))
+(define model #f)
+(define text-position #v(4.0 464.0))
+(define text "")
+(define y-rotation 0.0)
+
+(define (reset-position)
+ (set-vec3! position 0.0 0.0 -4.0))
+
+(define (load)
+ (set! model (load-obj "models/suzanne.obj"))
+ (reset-position))
+
+(define (draw alpha)
+ (with-projection projection
+ (with-depth-test #t
+ (draw-model model model-matrix view-matrix)))
+ (draw-text text text-position))
+
+(define (update dt)
+ ;; Update camera position
+ (set-vec3! velocity
+ (+ (if (key-pressed? 'd) -1.0 0.0)
+ (if (key-pressed? 'a) 1.0 0.0))
+ 0.0
+ (+ (if (key-pressed? 's) -1.0 0.0)
+ (if (key-pressed? 'w) 1.0 0.0)))
+ (vec3-normalize! velocity)
+ (vec3-mult! velocity 0.1)
+ (vec3-add! position velocity)
+ (set! text (format #f "(~6,2f, ~6,2f, ~6,2f)"
+ (vec3-x position)
+ (vec3-y position)
+ (vec3-z position)))
+
+ ;; Compute new view matrix
+ (matrix4-rotate-y! rotation theta)
+ (matrix4-translate! translation position)
+ (matrix4-identity! view-matrix)
+ (matrix4-mult! view-matrix view-matrix rotation)
+ (matrix4-mult! view-matrix view-matrix translation)
+
+ ;; Rotate the model about the y-axis
+ (set! y-rotation (+ y-rotation 0.03))
+ (matrix4-rotate-y! model-matrix y-rotation))
+
+(define (key-press key scancode modifiers repeat?)
+ (cond
+ ((eq? key 'q)
+ (abort-game))
+ ((eq? key 'r)
+ (reset-position))))
+
+(run-game #:window-title "3D!"
+ #:load load
+ #:draw draw
+ #:update update
+ #:key-press key-press)