summaryrefslogtreecommitdiff
path: root/shader.frag
blob: 3ead2db7c7a8b08758def7bb283cd0d19eb95661 (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
#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);
}