From bafb3b241cd0c07a41e932caa195a3a1144f7167 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 6 Sep 2018 12:19:22 -0400 Subject: render: scene: Stop allocating matrices when rendering. * chickadee/render/scene.scm (): Add matrix field. (make-primitive): Call %make-primitive with additional matrix arg. (): Add world-matrix field. (make-scene-node): Call %make-scene-node with additional matrix arg. (modelview): Delete. (draw-primitive): Perform matrix multiplication without allocating. (draw-scene-node): Likewise. (draw-scene): Pass pre-built matrix rather than allocating. --- chickadee/render/scene.scm | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/chickadee/render/scene.scm b/chickadee/render/scene.scm index 8aa6f1f..434f301 100644 --- a/chickadee/render/scene.scm +++ b/chickadee/render/scene.scm @@ -143,11 +143,12 @@ (force shader)))) (define-record-type - (%make-primitive vertex-array material targets) + (%make-primitive vertex-array material targets matrix) primitive? (vertex-array primitive-vertex-array) (material primitive-material) - (targets primitive-targets)) + (targets primitive-targets) + (matrix primitive-matrix)) (define (display-primitive primitive port) (format port "#" @@ -160,7 +161,7 @@ vertex-array (material default-material) targets) - (%make-primitive vertex-array material targets)) + (%make-primitive vertex-array material targets (make-identity-matrix4))) (define-record-type (%make-mesh name primitives weights) @@ -181,14 +182,15 @@ (%make-mesh name primitives weights)) (define-record-type - (%make-scene-node name children camera skin matrix mesh - rotation scale translation weights) + (%make-scene-node name children camera skin matrix world-matrix + mesh rotation scale translation weights) node? (name scene-node-name) (children scene-node-children) (camera scene-node-camera) (skin scene-node-skin) (matrix scene-node-matrix) + (world-matrix scene-node-world-matrix) (mesh scene-node-mesh) (rotation scene-node-rotation) (scale scene-node-scale) @@ -211,8 +213,8 @@ (scale (vec3 1.0 1.0 1.0)) (translation (vec3 0.0 0.0 0.0)) weights) - (%make-scene-node name children camera skin matrix mesh - rotation scale translation weights)) + (%make-scene-node name children camera skin matrix (make-identity-matrix4) + mesh rotation scale translation weights)) (define-record-type (%make-scene name nodes) @@ -223,21 +225,14 @@ (define* (make-scene #:key (name "anonymous") (nodes '())) (%make-scene name nodes)) -(define modelview - (make-matrix4 1.0 0.0 0.0 0.0 - 0.0 1.0 0.0 0.0 - 0.0 0.0 1.0 0.0 - 0.0 0.0 1.0 1.0) - ;;(make-identity-matrix4) - ) - (define (draw-primitive primitive matrix) ;; TODO: Use material. ;; TODO: Actually use physically based rendering. - (gpu-apply (pbr-shader) - (primitive-vertex-array primitive) - #:mvp (matrix4* modelview (current-projection)) - )) + (let ((mvp (primitive-matrix primitive))) + (matrix4-mult! mvp matrix (current-projection)) + (gpu-apply (pbr-shader) + (primitive-vertex-array primitive) + #:mvp mvp))) (define (draw-mesh mesh matrix) (for-each (lambda (primitive) @@ -245,8 +240,8 @@ (mesh-primitives mesh))) (define (draw-scene-node node matrix) - ;; TODO: Apply transformation matrix. - (let ((world-matrix (matrix4* matrix (scene-node-matrix node)))) + (let ((world-matrix (scene-node-world-matrix node))) + (matrix4-mult! world-matrix matrix (scene-node-matrix node)) (when (scene-node-mesh node) (draw-mesh (scene-node-mesh node) world-matrix)) (for-each (lambda (node) @@ -255,5 +250,5 @@ (define (draw-scene scene) (for-each (lambda (scene) - (draw-scene-node scene (make-identity-matrix4))) + (draw-scene-node scene (scene-node-world-matrix scene))) (scene-nodes scene))) -- cgit v1.2.3