summaryrefslogtreecommitdiff
path: root/shader.frag
diff options
context:
space:
mode:
Diffstat (limited to 'shader.frag')
-rw-r--r--shader.frag48
1 files changed, 48 insertions, 0 deletions
diff --git a/shader.frag b/shader.frag
new file mode 100644
index 0000000..3ead2db
--- /dev/null
+++ b/shader.frag
@@ -0,0 +1,48 @@
+#version 450
+#extension GL_ARB_separate_shader_objects : enable
+
+layout(location = 0) in vec3 normal;
+layout(location = 1) in vec3 fragPos;
+
+layout(location = 0) out vec4 outColor;
+
+float tubeIntegral(const float a, const float b, const float c, const float d, const float t)
+{
+ return (b*(c*t + d) - a*(c + t))/((c*c - d)*sqrt(t*(2*c + t) + d));
+}
+
+/* l0: vector to the center of the light from the fragment
+ * n: normalized surface normal of the fragment
+ * w: normalized basis vector for the width axis of the light
+ * halfWidth: half the width of the light
+ */
+float tubeLight(const vec3 l0, const vec3 n, const vec3 w, const float halfWidth)
+{
+ const float a = dot(n, l0);
+ const float b = dot(n, w);
+ const float c = dot(l0, w);
+ const float d = dot(l0, l0);
+ const float m = sign(b + max(0.0, a)*max(0.0, b));
+ const float zeroBound = -a/b;
+ const float lowerBound = clamp(zeroBound, -halfWidth, halfWidth);
+ const float upperBound = m*halfWidth;
+ return m*(tubeIntegral(a, b, c, d, upperBound) - tubeIntegral(a, b, c, d, lowerBound));
+}
+
+void main() {
+ const vec3 lightPos = vec3(1.0, 2.0, -0.0);
+ const vec3 lightDir = lightPos - fragPos;
+
+ //const vec3 lightW = normalize(vec3(1.0, 0.0, 0.0));
+ const float strength = 4.0;
+ const float invFalloff = strength/dot(lightDir, lightDir);
+ outColor = vec4(max(0.0, dot(normalize(normal), normalize(lightDir)))*invFalloff*vec3(1.0), 1.0);
+ //outColor = vec4(tubeLight(lightDir, normalize(normal), lightW, 5)*fragColor, 1.0);
+ //outColor.xyz += vec3(0.0625)*fragColor;
+ //const vec3 viewPos = -fragPos;
+ //const vec3 halfway = normalize(normalize(lightDir) - normalize(fragPos));
+ //const float specular = pow(max(0.0, dot(normalize(normal), halfway))*invFalloff, 20.0);
+ //const float specular = pow(tubeLight(lightDir, halfway, lightW, 5), 100.0);
+ //outColor = vec4(specular*vec3(1.0), 1.0);
+ //outColor = vec4(1.0, 1.0, 1.0, 1.0);
+}