From 86c9c009b09ed690a6b9d58f36a394f9fedfcceb Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 26 Oct 2019 19:07:48 -0400 Subject: render: model: Support faces with 5 or more elements in OBJ loader. --- chickadee/render/model.scm | 39 +++++++++++++++++++++++++++++++++------ 1 file 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. -- cgit v1.2.3