summaryrefslogtreecommitdiff
path: root/data/shaders/phong-frag.glsl
diff options
context:
space:
mode:
Diffstat (limited to 'data/shaders/phong-frag.glsl')
-rw-r--r--data/shaders/phong-frag.glsl89
1 files changed, 89 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
+}