summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2019-10-26 19:07:48 -0400
committerDavid Thompson <dthompson2@worcester.edu>2019-10-26 19:07:48 -0400
commit86c9c009b09ed690a6b9d58f36a394f9fedfcceb (patch)
tree36763bf7c528a69359daf87a88385812738b55e4
parent06ac67fd332c3be6ea9793809d393675b3156de0 (diff)
render: model: Support faces with 5 or more elements in OBJ loader.
-rw-r--r--chickadee/render/model.scm39
1 files changed, 33 insertions, 6 deletions
diff --git a/chickadee/render/model.scm b/chickadee/render/model.scm
index 42bfc9a..cf8b917 100644
--- a/chickadee/render/model.scm
+++ b/chickadee/render/model.scm
@@ -330,12 +330,12 @@
(deduplicate-face-element e)))
(define (parse-face args material)
(match args
- ;; Simple triangle
+ ;; A single triangle. Ah, life is so simple...
((a b c)
(push-face material a)
(push-face material b)
(push-face material c))
- ;; Quadrilateral, need to split into 2 triangles
+ ;; A quadrilateral. Needs to be split into 2 triangles.
;;
;; d-------c
;; | /|
@@ -343,18 +343,45 @@
;; | / |
;; |/ |
;; a-------b
- ;;
- ;; triangle 1: a b c
- ;; triangle 2: a c d
((a b c d)
+ ;; triangle 1: a b c
(push-face material a)
(push-face material b)
(push-face material c)
+ ;; triangle 2: a c d
(push-face material a)
(push-face material c)
(push-face material d))
+ ;; 3 or more triangles. Interpret as a strip of triangles
+ ;; moving from right to left (because counter-clockwise
+ ;; winding) like this:
+ ;;
+ ;; h-------f-------d-------c
+ ;; | /| /| /|
+ ;; | / | / | / |
+ ;; | / | / | / |
+ ;; |/ |/ |/ |
+ ;; g-------e-------a-------b
+ ;;
+ ;; ... and so on for however many face elements there are.
+ ;; Every other triangle is flipped over, hence the 'flip?'
+ ;; flag in the loop below.
+ ((a b . rest)
+ (let loop ((a a)
+ (b b)
+ (args rest)
+ (flip? #f))
+ (match args
+ (() #t)
+ ((c . rest)
+ (push-face material a)
+ (push-face material b)
+ (push-face material c)
+ (if flip?
+ (loop c a rest #f)
+ (loop a c rest #t))))))
(_
- (parse-error "unsupported number of face elements" args))))
+ (parse-error "invalid face" args))))
;; Register default material
(hash-set! material-map "default" default-phong-material)
;; Parse file.