summaryrefslogtreecommitdiff
path: root/chickadee/graphics/pbr.scm
blob: 15b78b80b216b244825f1daeeec40f6cfa0e6986 (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
129
130
131
132
133
134
135
136
137
138
139
;;; Chickadee Game Toolkit
;;; Copyright © 2019, 2021 David Thompson <davet@gnu.org>
;;;
;;; Chickadee 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.
;;;
;;; Chickadee 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:
;;
;; Physically based lighting model.
;;
;;; Code:

(define-module (chickadee graphics pbr)
  #:use-module (chickadee config)
  #:use-module (chickadee math vector)
  #:use-module (chickadee graphics blend)
  #:use-module (chickadee graphics buffer)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics engine)
  #:use-module (chickadee graphics polygon)
  #:use-module (chickadee graphics shader)
  #:use-module (chickadee graphics texture)
  #:use-module (srfi srfi-9)
  #:export (make-pbr-material
            pbr-material?
            pbr-material-name
            pbr-material-base-color-factor
            pbr-material-base-color-texture
            pbr-material-base-color-texture-enabled?
            pbr-material-base-color-texcoord
            pbr-material-metallic-factor
            pbr-material-roughness-factor
            pbr-material-normal-factor
            pbr-material-normal-texture
            pbr-material-normal-texture-enabled?
            pbr-material-normal-texcoord
            pbr-material-occlusion-facgor
            pbr-material-occlusion-texture
            pbr-material-occlusion-texture-enabled?
            pbr-material-occlusion-texcoord
            pbr-material-emissive-factor
            pbr-material-emissive-texture
            pbr-material-emissive-texture-enabled?
            pbr-material-emissive-texcoord
            pbr-material-alpha-mode
            pbr-material-alpha-cutoff
            pbr-material-double-sided?
            default-pbr-material
            pbr-shader
            shader-apply/pbr))

(define-shader-type <pbr-material>
  make-pbr-material
  pbr-material?
  (local-field name pbr-material-name)
  (float-vec3 base-color-factor pbr-material-base-color-factor)
  (local-field base-color-texture pbr-material-base-color-texture)
  (bool base-color-texture-enabled pbr-material-base-color-texture-enabled?)
  (int base-color-texcoord pbr-material-base-color-texcoord)
  (float metallic-factor pbr-material-metallic-factor)
  (float roughness-factor pbr-material-roughness-factor)
  (float-vec3 normal-factor pbr-material-normal-factor)
  (local-field normal-texture pbr-material-normal-texture)
  (bool normal-texture-enabled pbr-material-normal-texture-enabled)
  (int normal-texcoord pbr-material-normal-texcoord)
  (float-vec3 occlusion-factor pbr-material-occlusion-factor)
  (local-field occlusion-texture pbr-material-occlusion-texture)
  (bool occlusion-texture-enabled pbr-material-occlusion-texture-enabled)
  (int occlusion-texcoord pbr-material-occlusion-texcoord)
  (float-vec3 emissive-factor pbr-material-emissive-factor)
  (local-field emissive-texture pbr-material-emissive-texture)
  (bool emissive-texture-enabled pbr-material-emissive-texture-enabled)
  (int emissive-texcoord pbr-material-emissive-texcoord)
  (int alpha-mode pbr-material-alpha-mode)
  (float alpha-cutoff pbr-material-alpha-cutoff)
  (local-field double-sided? pbr-material-double-sided?))

(define default-pbr-material
  (make-pbr-material #:name "default"
                     #:base-color-factor #v(1.0 1.0 1.0)
                     #:base-color-texture null-texture
                     #:base-color-texture-enabled #f
                     #:base-color-texcoord 0
                     #:metallic-factor 1.0
                     #:roughness-factor 1.0
                     #:normal-factor #v(1.0 1.0 1.0)
                     #:normal-texture null-texture
                     #:normal-texture-enabled #f
                     #:normal-texcoord 0
                     #:occlusion-factor #v(1.0 1.0 1.0)
                     #:occlusion-texture null-texture
                     #:occlusion-texture-enabled #f
                     #:occlusion-texcoord 0
                     #:emissive-factor #v(1.0 1.0 1.0)
                     #:emissive-texture null-texture
                     #:emissive-texture-enabled #f
                     #:emissive-texcoord 0
                     #:alpha-mode 0
                     #:alpha-cutoff 0.5
                     #:double-sided? #f))

(define-graphics-variable pbr-shader
  (load-shader (scope-datadir "shaders/pbr-vert.glsl")
               (scope-datadir "shaders/pbr-frag.glsl")))

(define (shader-apply/pbr vertex-array material model-matrix view-matrix)
  (let* ((shader (graphics-variable-ref pbr-shader))
         (vattrs (vertex-array-attributes vertex-array))
         (sattrs (shader-attributes shader)))
    (with-graphics-state ((g:blend-mode (if (= (pbr-material-alpha-mode material) 2)
                                            blend:alpha
                                            blend:replace))
                          (g:cull-face-mode (if (pbr-material-double-sided? material)
                                                no-cull-face-mode
                                                back-cull-face-mode))
                          (g:texture-0 (pbr-material-base-color-texture material))
                          (g:texture-1 (pbr-material-normal-texture material))
                          (g:texture-2 (pbr-material-occlusion-texture material))
                          (g:texture-3 (pbr-material-emissive-texture material)))
      (shader-apply shader vertex-array
                    #:model model-matrix
                    #:view view-matrix
                    #:projection (current-projection)
                    #:vertex-colored (buffer-view?
                                      (assv-ref vattrs
                                                (attribute-location
                                                 (hash-ref sattrs "color0"))))
                    #:material material))))