summaryrefslogtreecommitdiff
path: root/examples/model.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2019-10-24 08:04:43 -0400
committerDavid Thompson <dthompson2@worcester.edu>2019-10-24 08:04:43 -0400
commitb43c4058c9a968aa4c0625711ce27ad0187ddce2 (patch)
tree77dd25f8ce0b2546ba80511b460a8b75f79e04e0 /examples/model.scm
parentc55914d815d8828c15f11c4be4364614ca83cee8 (diff)
render: Restructure 3D modules and add basic support for OBJ models.
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)