summaryrefslogtreecommitdiff
path: root/chickadee/graphics/phong.scm
blob: 372cdb67d10037047ddf1b26cca70931eddc3d02 (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
;;; 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:
;;
;; Simple forward rendered Phong lighting model.
;;
;;; Code:

(define-module (chickadee graphics phong)
  #:use-module (chickadee config)
  #:use-module (chickadee math vector)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics engine)
  #:use-module (chickadee graphics shader)
  #:use-module (chickadee graphics texture)
  #:use-module (srfi srfi-9)
  #:export (make-phong-material
            phong-material?
            phong-material-name
            phong-material-ambient
            phong-material-ambient-map
            phong-material-use-ambient-map?
            phong-material-diffuse
            phong-material-diffuse-map
            phong-material-use-diffuse-map?
            phong-material-specular
            phong-material-specular-map?
            phong-material-use-specular-map?
            phong-material-specular-exponent
            phong-material-bump-map
            phong-material-use-bump-map?
            default-phong-material
            load-phong-shader
            shader-apply/phong))


;;;
;;; Phong Material
;;;

(define-shader-type <phong-material>
  make-phong-material
  phong-material?
  (local-field name phong-material-name)
  (float-vec3 ambient phong-material-ambient)
  (local-field ambient-map phong-material-ambient-map)
  (bool use-ambient-map phong-material-use-ambient-map?)
  (float-vec3 diffuse phong-material-diffuse)
  (local-field diffuse-map phong-material-diffuse-map)
  (bool use-diffuse-map phong-material-use-diffuse-map?)
  (float-vec3 specular phong-material-specular)
  (local-field specular-map phong-material-specular-map)
  (bool use-specular-map phong-material-use-specular-map?)
  (float shininess phong-material-shininess)
  (local-field bump-map phong-material-bump-map)
  (bool use-bump-map phong-material-use-bump-map?))

(define default-phong-material
  (make-phong-material #:name "default"
                       #:ambient (vec3 0.5 0.5 0.5)
                       #:ambient-map null-texture
                       #:use-ambient-map #f
                       #:diffuse (vec3 0.8 0.8 0.8)
                       #:diffuse-map null-texture
                       #:use-diffuse-map #f
                       #:specular (vec3 0.3 0.3 0.3)
                       #:specular-map null-texture
                       #:use-specular-map #f
                       #:shininess 32.0
                       #:bump-map null-texture
                       #:use-bump-map #f))


;;;
;;; Lights
;;;

(define-shader-type <directional-light>
  make-directional-light
  directional-light?
  (float-vec3 direction directional-light-direction)
  (float-vec3 ambient directional-light-ambient)
  (float-vec3 diffuse directional-light-diffuse)
  (float-vec3 specular directional-light-specular)
  (float shininess directional-light-shininess))

(define default-directional-light
  (make-directional-light #:direction (vec3 0.0 0.0 -1.0)
                          #:ambient (vec3 0.1 0.1 0.1)
                          #:diffuse (vec3 1.0 1.0 1.0)
                          #:specular (vec3 0.5 0.5 0.5)
                          #:shininess 32.0))


;;;
;;; Phong Shader
;;;

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

(define (shader-apply/phong vertex-array material model-matrix view-matrix)
  (let ((shader (graphics-variable-ref phong-shader)))
    (with-graphics-state ((g:texture-0 (phong-material-ambient-map material))
                          (g:texture-1 (phong-material-diffuse-map material))
                          (g:texture-2 (phong-material-specular-map material))
                          (g:texture-3 (phong-material-bump-map material)))
      (shader-apply shader vertex-array
                    #:model model-matrix
                    #:view view-matrix
                    #:projection (current-projection)
                    #:material material
                    #:directional-light default-directional-light))))