summaryrefslogtreecommitdiff
path: root/data
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 /data
parente142166c3e2d6f567ed0d4e4c414fa510271260f (diff)
graphics: phong: Extract shader code to separate files.
Diffstat (limited to 'data')
-rw-r--r--data/shaders/phong-frag.glsl89
-rw-r--r--data/shaders/phong-vert.glsl34
2 files changed, 123 insertions, 0 deletions
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;
+}