From a4fd060c95cb19969e69a2d63a217cd82af1978d Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 8 Nov 2014 07:43:21 -0500 Subject: render: Move scene module to sly/render directory. * sly/scene.scm: Delete. * sly/render/scene.scm: New file. * Makefile.am (SOURCES): Add new file. Delete old one. * examples/simple.scm: Use (sly render scene). * examples/tilemap.scm: Likewise. --- Makefile.am | 2 +- examples/simple.scm | 2 +- examples/tilemap.scm | 2 +- sly/render/scene.scm | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ sly/scene.scm | 94 --------------------------------------- 5 files changed, 124 insertions(+), 97 deletions(-) create mode 100644 sly/render/scene.scm delete mode 100644 sly/scene.scm diff --git a/Makefile.am b/Makefile.am index e99fb61..d7640e3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,7 +40,6 @@ SOURCES = \ sly/quaternion.scm \ sly/rect.scm \ sly/repl.scm \ - sly/scene.scm \ sly/signal.scm \ sly/transform.scm \ sly/transition.scm \ @@ -51,6 +50,7 @@ SOURCES = \ sly/render/framebuffer.scm \ sly/render/mesh.scm \ sly/render/texture.scm \ + sly/render/scene.scm \ sly/render/shader.scm \ sly/render/shape.scm \ sly/render/sprite.scm \ diff --git a/examples/simple.scm b/examples/simple.scm index 92a2010..c6ba69f 100644 --- a/examples/simple.scm +++ b/examples/simple.scm @@ -18,7 +18,7 @@ (use-modules (sly camera) (sly game) (sly rect) - (sly scene) + (sly render scene) (sly render sprite) (sly transform) (sly vector) diff --git a/examples/tilemap.scm b/examples/tilemap.scm index 079aa13..d05f8d9 100644 --- a/examples/tilemap.scm +++ b/examples/tilemap.scm @@ -25,7 +25,7 @@ (sly render tileset) (sly vector) (sly window) - (sly scene) + (sly render scene) (sly signal) (sly camera) (sly color) diff --git a/sly/render/scene.scm b/sly/render/scene.scm new file mode 100644 index 0000000..19d4629 --- /dev/null +++ b/sly/render/scene.scm @@ -0,0 +1,121 @@ +;;; Sly +;;; Copyright (C) 2014 David Thompson +;;; +;;; Sly is free software: you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; Sly is distributed in the hope that it will be useful, but WITHOUT +;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public +;;; License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see +;;; . + +;;; Commentary: +;; +;; Hierarchy of renderable objects using a directed acyclic graph +;; structure. +;; +;;; Code: + +(define-module (sly render scene) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-9) + #:use-module (srfi srfi-26) + #:use-module (sly signal) + #:use-module (sly transform) + #:use-module (sly math vector) + #:use-module (sly render utils) + #:use-module (sly render camera) + #:use-module (sly render renderer) + #:export (scene-node make-scene-node + scene-node? + scene-node-object scene-node-transform + scene-node-visible? scene-node-children + draw-scene)) + +(define-record-type + (%make-scene-node object transform visible? children) + scene-node? + (object scene-node-object) + (transform scene-node-transform) + (visible? scene-node-visible?) + (children scene-node-children)) + +(define* (make-scene-node #:optional object #:key (transform identity-transform) + (visible? #t) (children '())) + "Create a new scene node containing OBJECT, a renderable object that +responds to the 'draw' method. The node has a local transformation +matrix TRANSFORM, and a list of CHILDREN. The VISIBLE? flag etermines +whether to draw the node and all of its children or not." + (%make-scene-node object transform visible? children)) + +(define scene-node make-scene-node) + +(define (flatten lst) + "Return a list that recursively concatenates all sub-lists of LIST." + (fold-right + (match-lambda* + (((sub-list ...) memo) + (append (flatten sub-list) memo)) + ((elem memo) + (cons elem memo))) + '() lst)) + +(define (set-identity! t) + (let ((matrix (transform-matrix t))) + (array-set! matrix 1 0 0) + (array-set! matrix 0 0 1) + (array-set! matrix 0 0 2) + (array-set! matrix 0 0 3) + (array-set! matrix 0 1 0) + (array-set! matrix 1 1 1) + (array-set! matrix 0 1 2) + (array-set! matrix 0 1 3) + (array-set! matrix 0 2 0) + (array-set! matrix 0 2 1) + (array-set! matrix 1 2 2) + (array-set! matrix 0 2 3) + (array-set! matrix 0 3 0) + (array-set! matrix 0 3 1) + (array-set! matrix 0 3 2) + (array-set! matrix 1 3 3))) + +(define t (make-transform 0 0 0 0 + 0 0 0 0 + 0 0 0 0 + 0 0 0 0)) + +(set-identity! t) + +(define (scene->renderer node camera) + "Traverse the scene graph defined by NODE and its children, as seen +by CAMERA, and return a list of the render operations needed to +display the scene." + (define (iter node parent-transform) + (signal-let ((node node)) + (if (scene-node-visible? node) + (let ((transform (transform* parent-transform + (scene-node-transform node))) + (object (scene-node-object node))) + ;; (transform*! t parent-transform (scene-node-transform node)) + (cons (if object + (transform-render-op (draw (scene-node-object node)) + transform) + '()) + (map (cut iter <> transform) + (scene-node-children node)))) + '()))) + (let ((view (transform* (camera-projection camera) + (camera-location camera)))) + (make-renderer (flatten (iter node view))))) + +(define (draw-scene node camera) + "Draw the scene defined by NODE, as seen by CAMERA." + (apply-viewport (camera-viewport camera)) + (render (scene->renderer node camera))) diff --git a/sly/scene.scm b/sly/scene.scm deleted file mode 100644 index 6cd3a5d..0000000 --- a/sly/scene.scm +++ /dev/null @@ -1,94 +0,0 @@ -;;; Sly -;;; Copyright (C) 2014 David Thompson -;;; -;;; Sly is free software: you can redistribute it and/or modify it -;;; under the terms of the GNU General Public License as published by -;;; the Free Software Foundation, either version 3 of the License, or -;;; (at your option) any later version. -;;; -;;; Sly is distributed in the hope that it will be useful, but WITHOUT -;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public -;;; License for more details. -;;; -;;; You should have received a copy of the GNU General Public License -;;; along with this program. If not, see -;;; . - -;;; Commentary: -;; -;; Hierarchy of renderable objects using a directed acyclic graph -;; structure. -;; -;;; Code: - -(define-module (sly scene) - #:use-module (ice-9 match) - #:use-module (srfi srfi-1) - #:use-module (srfi srfi-9) - #:use-module (srfi srfi-26) - #:use-module (sly signal) - #:use-module (sly transform) - #:use-module (sly math vector) - #:use-module (sly render utils) - #:use-module (sly render camera) - #:use-module (sly render renderer) - #:export (scene-node make-scene-node - scene-node? - scene-node-object scene-node-transform - scene-node-visible? scene-node-children - draw-scene)) - -(define-record-type - (%make-scene-node object transform visible? children) - scene-node? - (object scene-node-object) - (transform scene-node-transform) - (visible? scene-node-visible?) - (children scene-node-children)) - -(define* (make-scene-node #:optional object #:key (transform identity-transform) - (visible? #t) (children '())) - "Create a new scene node containing OBJECT, a renderable object that -responds to the 'draw' method. The node has a local transformation -matrix TRANSFORM, and a list of CHILDREN. The VISIBLE? flag etermines -whether to draw the node and all of its children or not." - (%make-scene-node object transform visible? children)) - -(define scene-node make-scene-node) - -(define (flatten lst) - "Return a list that recursively concatenates all sub-lists of LIST." - (fold-right - (match-lambda* - (((sub-list ...) memo) - (append (flatten sub-list) memo)) - ((elem memo) - (cons elem memo))) - '() lst)) - -(define (scene->renderer node camera) - "Traverse the scene graph defined by NODE and its children, as seen -by CAMERA, and return a list of the render operations needed to -display the scene." - (define (iter node parent-transform) - (signal-let ((node node)) - (if (scene-node-visible? node) - (let ((transform (transform* parent-transform - (scene-node-transform node))) - (object (scene-node-object node))) - (cons (if object - (transform-render-op (draw (scene-node-object node)) - transform) - '()) - (map (cut iter <> transform) - (scene-node-children node)))) - '()))) - (let ((view (transform* (camera-projection camera) - (camera-location camera)))) - (make-renderer (flatten (iter node view))))) - -(define (draw-scene node camera) - "Draw the scene defined by NODE, as seen by CAMERA." - (apply-viewport (camera-viewport camera)) - (render (scene->renderer node camera))) -- cgit v1.2.3