summaryrefslogtreecommitdiff
path: root/chickadee/graphics/framebuffer.scm
blob: 2af7edd7ded40ce2973e239c1fdbdec8e1d29198 (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
;;; Chickadee Game Toolkit
;;; Copyright © 2017, 2021 David Thompson <dthompson2@worcester.edu>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;    http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

;;; Commentary:
;;
;; Render to texture.
;;
;;; Code:

(define-module (chickadee graphics framebuffer)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-9)
  #:use-module (system foreign)
  #:use-module (chickadee math matrix)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics engine)
  #:use-module (chickadee graphics gpu)
  #:use-module (chickadee graphics pixbuf)
  #:use-module ((chickadee graphics texture) #:select (make-texture null-texture))
  #:use-module (chickadee graphics viewport)
  #:export (make-framebuffer
            framebuffer?
            framebuffer-texture
            framebuffer-viewport
            framebuffer-projection
            null-framebuffer
            current-framebuffer
            with-framebuffer))

(define-record-type <framebuffer>
  (%make-framebuffer id renderbuffer-id texture viewport projection)
  framebuffer?
  (id framebuffer-id)
  (renderbuffer-id framebuffer-renderbuffer-id)
  (texture framebuffer-texture)
  (viewport framebuffer-viewport)
  (projection framebuffer-projection))

(define null-framebuffer
  (%make-framebuffer 0 0 null-texture null-viewport (make-identity-matrix4)))

(define %clear-color (transparency 0.0))

(define* (make-framebuffer width height #:key (min-filter 'linear) (mag-filter 'linear)
                           (wrap-s 'repeat) (wrap-t 'repeat))
  "Create a new framebuffer that renders to a texture with
dimensions WIDTH x HEIGHT."
  (assert-current-graphics-engine)
  (let* ((gpu (current-gpu))
         (framebuffer-id (fresh-gpu-framebuffer gpu))
         (renderbuffer-id (fresh-gpu-renderbuffer gpu))
         (texture (make-texture width height
                                #:min-filter min-filter
                                #:mag-filter mag-filter
                                #:wrap-s wrap-s
                                #:wrap-t wrap-t))
         (texture-id ((@@ (chickadee graphics texture) texture-id)
                      texture))
         ;; It is convenient to make a default viewport and
         ;; projection matrix for the framebuffer so that the
         ;; rendering engine can set it whenever it changes to
         ;; this framebuffer, saving users the trouble of having
         ;; to this tedious task themselves.
         (viewport (make-viewport 0 0 width height #:clear-color %clear-color))
         (projection (orthographic-projection 0 width height 0 0 1))
         (framebuffer (%make-framebuffer framebuffer-id
                                         renderbuffer-id
                                         texture
                                         viewport
                                         projection)))
    (gpu-framebuffer-init gpu framebuffer-id renderbuffer-id texture-id
                          width height)
    framebuffer))

(define-syntax-rule (with-framebuffer framebuffer body ...)
  ;; As a convenience, initialize the viewport and projection matrix
  ;; as well so that the user doesn't have to explicitly make a
  ;; viewport and/or projection matrix unless they actually want to do
  ;; fancy viewport manipulations.
  (with-graphics-state ((framebuffer (framebuffer-id framebuffer))
                        (projection (framebuffer-projection framebuffer)))
    (with-viewport (framebuffer-viewport framebuffer)
      (clear-viewport)
      body ...)))