summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2018-09-06 12:19:22 -0400
committerDavid Thompson <dthompson2@worcester.edu>2018-09-06 12:19:22 -0400
commitbafb3b241cd0c07a41e932caa195a3a1144f7167 (patch)
tree928b8317d70b285f99f5c267f5331714df4f0950
parent7703e6646b2ea3db978bb1f749b2eb5b257867de (diff)
render: scene: Stop allocating matrices when rendering.
* chickadee/render/scene.scm (<primitive>): Add matrix field. (make-primitive): Call %make-primitive with additional matrix arg. (<scene-node>): 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.
-rw-r--r--chickadee/render/scene.scm39
1 files 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 <primitive>
- (%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 "#<primitive material: ~s targets: ~s>"
@@ -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 <mesh>
(%make-mesh name primitives weights)
@@ -181,14 +182,15 @@
(%make-mesh name primitives weights))
(define-record-type <scene-node>
- (%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 <scene>
(%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)))