summaryrefslogtreecommitdiff
path: root/sly/render/group.scm
blob: b4fad1661a6d6cf9e3374f0b9dc3662218d77d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
;;; Sly
;;; Copyright (C) 2014 David Thompson <davet@gnu.org>
;;;
;;; 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
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Hierarchy of renderable objects using a directed acyclic graph
;; structure.
;;
;;; Code:

(define-module (sly render group)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-9)
  #:use-module (sly math transform)
  #:use-module (sly render shader)
  #:use-module (sly render texture)
  #:use-module (sly render utils)
  #:use-module (sly render camera)
  #:use-module (sly render context)
  #:use-module (sly render model)
  #:export (make-group group
            group?
            group-transform group-visible? group-children
            draw-group
            group-move group-place group-show))

;;;
;;; Group
;;;

;; The composite object of the scene graph.  Groups have zero or more
;; children, which may be groups or models.
(define-record-type <group>
  (%make-group transform visible? children)
  group?
  (transform group-transform)
  (visible? group-visible?)
  (children group-children))

(define* (make-group children #:optional #:key (transform identity-transform)
                     (visible? #t))
  "Create a new group containing CHILDREN in which each child is
rendered relative to TRANSFORM.  The VISIBLE? flag determines whether
or not to render child nodes."
  (%make-group transform visible? children))

(define (%draw-group group parent-transform view context)
  (match group
    (($ <group> transform visible? children)
     (when visible?
       (with-temp-transform context world-transform
         (transform*! world-transform transform parent-transform)
         (for-each (lambda (child)
                     (let ((draw (match child
                                   ((? group? group) %draw-group)
                                   ((? model? model) draw-model))))
                       (draw child world-transform view context)))
                   children))))))

(define (set-transform-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 draw-group
  (let ((context (make-render-context)))
    (lambda* (group camera #:optional (context context))
      "Draw the scene defined by GROUP as viewed by CAMERA, with the given
render CONTEXT."
      (with-temp-transform context view
        (transform*! view
                     (camera-location camera)
                     (camera-projection camera))
        (with-temp-transform context base-transform
          (set-transform-identity! base-transform)
          (apply-viewport (camera-viewport camera))
          (%draw-group group base-transform view context))))))

;;;
;;; Utility Procedures
;;;

(define (group . children)
  "Create a new group containing the list of CHILDREN."
  (make-group children))

(define (group-move position . children)
  "Create a new group in which the list of CHILDREN are translated by
the vector POSITION."
  (make-group children #:transform (translate position)))

(define (group-place transform . children)
  "Create a new group in which the tranformation matrices of the
CHILDREN are multiplied by TRANSFORM."
  (make-group children #:transform transform))

(define (group-show visible? . children)
  "Create a new group in which the visibility of the list of
CHILDREN is determined by the VISIBLE? flag."
  (make-group children #:visible? visible?))