summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2021-05-07 09:53:23 -0400
committerDavid Thompson <dthompson2@worcester.edu>2021-05-07 09:53:23 -0400
commitfeb3c58215960f2bac66922e8d1a1f8e32b7ff95 (patch)
tree476c9d973cec76aa7caf34545f15140415d0973c
parente142166c3e2d6f567ed0d4e4c414fa510271260f (diff)
graphics: phong: Extract shader code to separate files.
-rw-r--r--Makefile.am4
-rw-r--r--chickadee/graphics/phong.scm127
-rw-r--r--data/shaders/phong-frag.glsl89
-rw-r--r--data/shaders/phong-vert.glsl34
4 files changed, 129 insertions, 125 deletions
diff --git a/Makefile.am b/Makefile.am
index be1a53f..be0f917 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -133,7 +133,9 @@ dist_shaders_DATA = \
data/shaders/path-vert.glsl \
data/shaders/path-frag.glsl \
data/shaders/pbr-vert.glsl \
- data/shaders/pbr-frag.glsl
+ data/shaders/pbr-frag.glsl \
+ data/shaders/phong-vert.glsl \
+ data/shaders/phong-frag.glsl
info_TEXINFOS = doc/chickadee.texi
doc_chickadee_TEXINFOS = \
diff --git a/chickadee/graphics/phong.scm b/chickadee/graphics/phong.scm
index acd238c..372cdb6 100644
--- a/chickadee/graphics/phong.scm
+++ b/chickadee/graphics/phong.scm
@@ -22,6 +22,7 @@
;;; 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)
@@ -111,130 +112,8 @@
;;;
(define-graphics-variable phong-shader
- (strings->shader
- "
-#ifdef GLSL330
-layout (location = 0) in vec3 position;
-layout (location = 1) in vec2 texcoord;
-layout (location = 2) in vec3 normal;
-#elif defined(GLSL130)
-in vec3 position;
-in vec2 texcoord;
-in vec3 normal;
-#elif defined(GLSL120)
-attribute vec3 position;
-attribute vec2 texcoord;
-attribute vec3 normal;
-#endif
-
-uniform mat4 model;
-uniform mat4 view;
-uniform mat4 projection;
-
-#ifdef GLSL120
-varying vec3 fragNorm;
-varying vec2 fragTex;
-#else
-out vec3 fragNorm;
-out vec2 fragTex;
-#endif
-
-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;
-}
-"
- "
-struct Material {
- vec3 ambient;
- sampler2D ambientMap;
- bool useAmbientMap;
- 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;
-};
-
-#ifdef GLSL120
-varying vec3 fragNorm;
-varying vec2 fragTex;
-#else
-in vec3 fragNorm;
-in vec2 fragTex;
-#endif
-
-#ifdef GLSL330
-out vec4 fragColor;
-#endif
-
-uniform Material material;
-uniform DirectionalLight directionalLight;
-
-void main() {
- vec3 baseAmbientColor;
- vec3 baseDiffuseColor;
- vec3 baseSpecularColor;
- if(material.useAmbientMap) {
-#ifdef GLSL330
- baseAmbientColor = texture(material.ambientMap, fragTex).xyz;
-#else
- baseAmbientColor = texture2D(material.ambientMap, fragTex).xyz;
-#endif
- } else {
- baseAmbientColor = vec3(1.0, 1.0, 1.0);
- }
- if(material.useDiffuseMap) {
- // discard transparent fragments.
-#ifdef GLSL330
- vec4 color = texture(material.diffuseMap, fragTex);
-#else
- vec4 color = texture2D(material.diffuseMap, fragTex);
-#endif
- if(color.a == 0.0) { discard; }
- baseDiffuseColor = color.xyz;
- } else {
- baseDiffuseColor = vec3(1.0, 1.0, 1.0);
- }
- if(material.useSpecularMap) {
-#ifdef GLSL330
- baseSpecularColor = texture(material.specularMap, fragTex).xyz;
-#else
- baseSpecularColor = texture2D(material.specularMap, fragTex).xyz;
-#endif
- } else {
- baseSpecularColor = vec3(1.0, 1.0, 1.0);
- }
- vec3 ambientColor = material.ambient * baseAmbientColor * 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 = 0;
- if(material.shininess > 0) {
- specularFactor = pow(max(dot(lightDir, reflectDir), 0.0), material.shininess);
- }
- vec3 specularColor = specularFactor * baseSpecularColor * material.specular;
-#ifdef GLSL330
- fragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0);
-#else
- gl_FragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0);
-#endif
-}
-"))
+ (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)))
diff --git a/data/shaders/phong-frag.glsl b/data/shaders/phong-frag.glsl
new file mode 100644
index 0000000..ad324b3
--- /dev/null
+++ b/data/shaders/phong-frag.glsl
@@ -0,0 +1,89 @@
+// -*- mode: c -*-
+
+struct Material {
+ vec3 ambient;
+ sampler2D ambientMap;
+ bool useAmbientMap;
+ 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;
+};
+
+#ifdef GLSL120
+varying vec3 fragNorm;
+varying vec2 fragTex;
+#else
+in vec3 fragNorm;
+in vec2 fragTex;
+#endif
+
+#ifdef GLSL330
+out vec4 fragColor;
+#endif
+
+uniform Material material;
+uniform DirectionalLight directionalLight;
+
+void main() {
+ vec3 baseAmbientColor;
+ vec3 baseDiffuseColor;
+ vec3 baseSpecularColor;
+ if(material.useAmbientMap) {
+#ifdef GLSL330
+ baseAmbientColor = texture(material.ambientMap, fragTex).xyz;
+#else
+ baseAmbientColor = texture2D(material.ambientMap, fragTex).xyz;
+#endif
+ } else {
+ baseAmbientColor = vec3(1.0, 1.0, 1.0);
+ }
+ if(material.useDiffuseMap) {
+ // discard transparent fragments.
+#ifdef GLSL330
+ vec4 color = texture(material.diffuseMap, fragTex);
+#else
+ vec4 color = texture2D(material.diffuseMap, fragTex);
+#endif
+ if(color.a == 0.0) { discard; }
+ baseDiffuseColor = color.xyz;
+ } else {
+ baseDiffuseColor = vec3(1.0, 1.0, 1.0);
+ }
+ if(material.useSpecularMap) {
+#ifdef GLSL330
+ baseSpecularColor = texture(material.specularMap, fragTex).xyz;
+#else
+ baseSpecularColor = texture2D(material.specularMap, fragTex).xyz;
+#endif
+ } else {
+ baseSpecularColor = vec3(1.0, 1.0, 1.0);
+ }
+ vec3 ambientColor = material.ambient * baseAmbientColor * 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 = 0;
+ if(material.shininess > 0) {
+ specularFactor = pow(max(dot(lightDir, reflectDir), 0.0), material.shininess);
+ }
+ vec3 specularColor = specularFactor * baseSpecularColor * material.specular;
+#ifdef GLSL330
+ fragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0);
+#else
+ gl_FragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0);
+#endif
+}
diff --git a/data/shaders/phong-vert.glsl b/data/shaders/phong-vert.glsl
new file mode 100644
index 0000000..51461fa
--- /dev/null
+++ b/data/shaders/phong-vert.glsl
@@ -0,0 +1,34 @@
+// -*- mode: c -*-
+
+#ifdef GLSL330
+layout (location = 0) in vec3 position;
+layout (location = 1) in vec2 texcoord;
+layout (location = 2) in vec3 normal;
+#elif defined(GLSL130)
+in vec3 position;
+in vec2 texcoord;
+in vec3 normal;
+#elif defined(GLSL120)
+attribute vec3 position;
+attribute vec2 texcoord;
+attribute vec3 normal;
+#endif
+
+#ifdef GLSL120
+varying vec3 fragNorm;
+varying vec2 fragTex;
+#else
+out vec3 fragNorm;
+out vec2 fragTex;
+#endif
+
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
+
+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;
+}