diff options
| author | Patrick | 2021-03-26 19:36:35 +0100 |
|---|---|---|
| committer | Patrick | 2021-03-26 19:36:35 +0100 |
| commit | c99ecda7bed596922125f6b1ef1ef2ae8f27703e (patch) | |
| tree | 16b3a22483457d22fdd34c1e079fd25dabd84940 /shaders | |
| parent | 36fb27a899045de24d71d55b06648abda7547268 (diff) | |
| download | subsurface_scattering-c99ecda7bed596922125f6b1ef1ef2ae8f27703e.tar.gz subsurface_scattering-c99ecda7bed596922125f6b1ef1ef2ae8f27703e.zip | |
SSSSS comments
Diffstat (limited to 'shaders')
| -rw-r--r-- | shaders/fbo_frag.glsl | 36 | ||||
| -rw-r--r-- | shaders/fbo_vert.glsl | 1 | ||||
| -rw-r--r-- | shaders/frag_irradiance.glsl | 30 | ||||
| -rw-r--r-- | shaders/frag_shadowmap.glsl | 2 | ||||
| -rw-r--r-- | shaders/vert_irradiance.glsl | 44 | ||||
| -rw-r--r-- | shaders/vert_shadowmap.glsl | 1 |
6 files changed, 29 insertions, 85 deletions
diff --git a/shaders/fbo_frag.glsl b/shaders/fbo_frag.glsl index 1102992..f92483e 100644 --- a/shaders/fbo_frag.glsl +++ b/shaders/fbo_frag.glsl @@ -11,45 +11,17 @@ uniform int renderState; uniform vec2 samplePositions[13];
uniform vec3 sampleWeights[13];
-vec4 blur(sampler2D tex, vec2 uv, vec2 res) {
- float Pi = 6.28318530718; // Pi*2
-
- // GAUSSIAN BLUR SETTINGS {{{
- float Directions = 16.0; // BLUR DIRECTIONS (Default 16.0 - More is better but slower)
- float Quality = 4.0; // BLUR QUALITY (Default 4.0 - More is better but slower)
- float Size = 8.0; // BLUR SIZE (Radius)
- // GAUSSIAN BLUR SETTINGS }}}
-
- vec2 Radius = Size/res;
-
- // Pixel colour
- vec4 Color = texture(tex, uv);
-
- // Blur calculations
- for( float d=0.0; d<Pi; d+=Pi/Directions) {
- for(float i=1.0/Quality; i<=1.0; i+=1.0/Quality) {
- Color += texture( tex, uv+vec2(cos(d),sin(d))*Radius*i);
- }
- }
-
- // Output to screen
- Color /= Quality * Directions - 15.0;
- return Color;
-}
-
void main()
{
if (renderState == 0) {
- FragColor = blur(shadowmapTexture, TexCoords, vec2(screenWidth, screenHeight));
- }
- else if (renderState == 1) {
FragColor = texture(shadowmapTexture, TexCoords);
}
- // stencil buffer
- else if (renderState == 2) {
+ else if (renderState == 1) {
FragColor = texture(irradianceTexture, TexCoords);
}
- else if (renderState == 3) {
+ else if (renderState == 2) {
+ // sample calculated irradiance
+ // using Gaussian kernel to approximate light spread
vec4 result = vec4(0, 0, 0, 1);
for (int i = 0; i < 13; i++) {
vec2 sampleCoords = TexCoords + samplePositions[i] * vec2(1.0/screenWidth, 1.0/screenHeight);
diff --git a/shaders/fbo_vert.glsl b/shaders/fbo_vert.glsl index fb80df5..0cf6138 100644 --- a/shaders/fbo_vert.glsl +++ b/shaders/fbo_vert.glsl @@ -6,6 +6,7 @@ out vec2 TexCoords; void main()
{
+ // this is just a plane that covers the screen
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
TexCoords = aTexCoords;
}
diff --git a/shaders/frag_irradiance.glsl b/shaders/frag_irradiance.glsl index 1bcbc99..b985acf 100644 --- a/shaders/frag_irradiance.glsl +++ b/shaders/frag_irradiance.glsl @@ -17,11 +17,9 @@ uniform int renderState; uniform float powBase;
uniform float powFactor;
-uniform float translucencySampleVariances[6];
-uniform vec3 translucencySampleWeights[6];
-
void main()
{
+ // phong lighting
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
@@ -37,30 +35,20 @@ void main() float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
- //float distanceToBackside = length(clamp(FragPos - Backside, vec3(0), vec3(1000)));
- float distanceToBackside = length(FragPos - Backside);
- //distanceToBackside = distance(Backside, LocalPos);
vec3 result = (ambient + diffuse + specular) * objectColor;
- if (renderState == 3) {
+ // thickness
+ float distanceToBackside = length(FragPos - Backside);
+
+ if (renderState == 2) {
if (distanceToBackside != 0) {
+ // add translucency by amplifying color inverse to the thickness
+ // (1 - diff) is part of the irradiance term,
+ // if the light hits the object straight at 90°
+ // most light is received
result += objectColor * pow(powBase, powFactor / pow(distanceToBackside, 0.6)) * transmittanceScale * (1 - diff);
- // vec3 translucency = vec3(0);
- // for (int i = 0; i < 6; i++) {
- // translucency += objectColor * translucencySampleWeights[i] * exp(-pow(distanceToBackside, 2.0) / translucencySampleVariances[i]);
- // }
-
- // result += translucency * transmittanceScale;
}
}
- //result += objectColor * pow(powBase, -pow(distanceToBackside, 2)) * transmittanceScale * (1 - diff);
- // if (renderState == 3) {
- // //result = Backside;
- // //result = LocalPos;
- // result = vec3(distanceToBackside);
- // }
-
FragColor = vec4(result, 1.0f);
- //FragColor = vec4(vec3(distanceToBackside), 1);
}
diff --git a/shaders/frag_shadowmap.glsl b/shaders/frag_shadowmap.glsl index 2011f82..38a01b6 100644 --- a/shaders/frag_shadowmap.glsl +++ b/shaders/frag_shadowmap.glsl @@ -10,10 +10,10 @@ uniform vec3 lightPos; void main()
{
+ // calculate distance in world coordinates
float lightDist = length(lightPos - FragPos);
float c1 = lightDist;
float c2 = lightDist;
float c3 = lightDist;
FragColor = vec4(c1, c2, c3, 1);
- //FragColor = vec4(LocalPos/10, 1);
}
diff --git a/shaders/vert_irradiance.glsl b/shaders/vert_irradiance.glsl index 6b54180..89ba12e 100644 --- a/shaders/vert_irradiance.glsl +++ b/shaders/vert_irradiance.glsl @@ -23,55 +23,37 @@ uniform mat4 lightViewInv; uniform mat4 projection;
uniform mat4 lightProjection;
-vec4 blur(sampler2D tex, vec2 uv, vec2 res) {
- float Pi = 6.28318530718; // Pi*2
-
- // GAUSSIAN BLUR SETTINGS {{{
- float Directions = 16.0; // BLUR DIRECTIONS (Default 16.0 - More is better but slower)
- float Quality = 4.0; // BLUR QUALITY (Default 4.0 - More is better but slower)
- float Size = 8.0; // BLUR SIZE (Radius)
- // GAUSSIAN BLUR SETTINGS }}}
-
- vec2 Radius = Size/res;
-
- // Pixel colour
- vec4 Color = texture(tex, uv);
-
- // Blur calculations
- for( float d=0.0; d<Pi; d+=Pi/Directions) {
- for(float i=1.0/Quality; i<=1.0; i+=1.0/Quality) {
- Color += texture( tex, uv+vec2(cos(d),sin(d))*Radius*i);
- }
- }
-
- // Output to screen
- Color /= Quality * Directions - 15.0;
- return Color;
-}
-
void main()
{
gl_Position = projection * view * model * vec4(pos, 1.0);
+ // calculate fragment position in world coordinates
FragPos = vec3(model * vec4(pos, 1));
+ // and local coordinates
LocalPos = pos;
+
Normal = normal;
+ // get fragment position in the light's projection space
vec4 lightSpace = lightProjection * lightView * model * vec4(pos, 1.0);
+ // and transform them to 2D coordinates
+ // (this is usually done by OpenGL after applying the vertex shader,
+ // so to get them here, we have to divide by w manually)
lightSpace = lightSpace / lightSpace.w;
vec2 shadowmapCoords = lightSpace.xy;
+ // map coordinates from [0 1] to [-1 +1]
+ // multiply by 0.99 first to shift coordinates towards the center slightly
+ // to prevent artifacts at the edges
shadowmapCoords = vec2(
(shadowmapCoords.x * 0.99 + 1) / 2,
(shadowmapCoords.y * 0.99 + 1) / 2
);
-
- // blur
+ // sample shadowmap (brightness encodes distance of fragment to light)
vec4 t = texture(shadowmapTexture, shadowmapCoords);
- //vec4 t = blur(shadowmapTexture, shadowmapCoords, vec2(screenWidth, screenHeight));
- BacksideIrradiance = t.r; //*100 + t.g + t.b/100;
+ BacksideIrradiance = t.r;
+ // calculate backside with distance(BacksideIrradiance) and lightDir
vec3 lightDir = normalize(FragPos - lightPos);
Backside = (lightPos + (lightDir * BacksideIrradiance));
- //Backside = texture(shadowmapTexture, shadowmapCoords).xyz*10;
}
diff --git a/shaders/vert_shadowmap.glsl b/shaders/vert_shadowmap.glsl index 50847bb..a6c9398 100644 --- a/shaders/vert_shadowmap.glsl +++ b/shaders/vert_shadowmap.glsl @@ -14,6 +14,7 @@ uniform mat4 projection; void main()
{
gl_Position = projection * lightView * model * vec4(pos, 1.0);
+ // pass fragment position in world coordinates
FragPos = vec3(model * vec4(pos, 1));
LocalPos = pos;
Normal = normal;
|
