summaryrefslogtreecommitdiff
path: root/chickadee/render/phong.scm
blob: 2341e7a125083e5ed31f2315e46a8e1b7452fedf (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;;; Chickadee Game Toolkit
;;; Copyright © 2019 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 render phong)
  #:use-module (chickadee math vector)
  #:use-module (chickadee render)
  #:use-module (chickadee render color)
  #:use-module (chickadee render shader)
  #:use-module (chickadee render texture)
  #:use-module (srfi srfi-9)
  #:export (make-phong-material
            phong-material?
            phong-material-name
            phong-material-ambient
            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
            gpu-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)
  (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)
                       #: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 -1.0 0.0 0.0)
                          #:diffuse (vec3 0.0 0.0 0.0)
                          #:specular (vec3 0.0 0.0 0.0)
                          #:shininess 0.0))


;;;
;;; Phong Shader
;;;

(define phong-shader
  (delay
    (strings->shader
     "
#version 130

in vec3 position;
in vec2 texcoord;
in vec3 normal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 fragNorm;
out vec2 fragTex;

void main() {
    gl_Position = projection * view * model * vec4(position, 1.0);
    // TODO: Calculate normal matrix on CPU
    fragNorm = normalize(model * vec4(normal, 1.0)).xyz;
    fragTex = texcoord;
}
"
     "
#version 130

struct Material {
  vec3 ambient;
  vec3 diffuse;
  sampler2D diffuseMap;
  bool useDiffuseMap;
  vec3 specular;
  sampler2D specularMap;
  bool useSpecularMap;
  float shininess;
  sampler2D bumpMap;
  bool useBumpMap;
};

struct DirectionalLight {
    vec3 direction;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

in vec3 fragNorm;
in vec2 fragTex;

uniform Material material;
uniform DirectionalLight directionalLight;

void main() {
    vec3 baseDiffuseColor;
    vec3 baseSpecularColor;
    if(material.useDiffuseMap) {
         baseDiffuseColor = texture2D(material.diffuseMap, fragTex).xyz;
    } else {
         baseDiffuseColor = vec3(1.0, 1.0, 1.0);
    }
    if(material.useDiffuseMap) {
         baseSpecularColor = texture2D(material.specularMap, fragTex).xyz;
    } else {
         baseSpecularColor = vec3(1.0, 1.0, 1.0);
    }
    vec3 ambientColor = material.ambient * baseDiffuseColor;
    vec3 lightDir = normalize(-directionalLight.direction);
    float diffuseFactor = max(dot(lightDir, fragNorm), 0.0);
    vec3 diffuseColor = diffuseFactor * baseDiffuseColor * material.diffuse;
    vec3 reflectDir = reflect(-lightDir, fragNorm);
    float specularFactor = pow(max(dot(lightDir, reflectDir), 0.0), material.shininess);
    vec3 specularColor = specularFactor * baseSpecularColor * material.specular;
    gl_FragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0);
}
")))

(define (load-phong-shader)
  (force phong-shader))

(define (gpu-apply/phong shader vertex-array material model-matrix view-matrix)
  (with-texture 0 (phong-material-diffuse-map material)
    (with-texture 1 (phong-material-specular-map material)
      (with-texture 2 (phong-material-bump-map material)
        (gpu-apply shader vertex-array
                   #:model model-matrix
                   #:view view-matrix
                   #:projection (current-projection)
                   #:material material
                   #:directional-light default-directional-light)))))