diff options
| author | Patrick | 2021-03-26 20:12:25 +0100 |
|---|---|---|
| committer | Patrick | 2021-03-26 20:12:25 +0100 |
| commit | 0a5e8172a6ee0e79c81eec21a9966bea9385b249 (patch) | |
| tree | 64b7f94af72ed0d57f3ea769bc14fa01702e20e5 | |
| parent | c99ecda7bed596922125f6b1ef1ef2ae8f27703e (diff) | |
| download | subsurface_scattering-0a5e8172a6ee0e79c81eec21a9966bea9385b249.tar.gz subsurface_scattering-0a5e8172a6ee0e79c81eec21a9966bea9385b249.zip | |
comments TS SSS
| -rw-r--r-- | shaders/ts_frag.glsl | 51 | ||||
| -rw-r--r-- | shaders/ts_frag_irradiance.glsl | 47 | ||||
| -rw-r--r-- | shaders/ts_vert.glsl | 1 | ||||
| -rw-r--r-- | shaders/ts_vert_irradiance.glsl | 4 | ||||
| -rw-r--r-- | src/main.cpp | 15 | ||||
| -rw-r--r-- | src/main2.cpp | 20 |
6 files changed, 28 insertions, 110 deletions
diff --git a/shaders/ts_frag.glsl b/shaders/ts_frag.glsl index a0a8457..9362e2a 100644 --- a/shaders/ts_frag.glsl +++ b/shaders/ts_frag.glsl @@ -20,42 +20,7 @@ uniform float transmittanceScale; void main()
{
- if (renderState == 0) {
- FragColor = texture(irradianceTexture, UV);
- }
- else if (renderState == 1) {
- vec3 norm = normalize(Normal);
- vec3 lightDir = normalize(lightPos - FragPos);
-
- float diff = max(dot(norm, lightDir), 0.0);
- vec3 diffuse = diff * lightColor;
-
- float ambientStrength = 0.1;
- vec3 ambient = ambientStrength * lightColor;
-
- float specularStrength = 0.5;
- vec3 viewDir = normalize(viewPos - FragPos);
- vec3 reflectDir = reflect(-lightDir, norm);
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
- vec3 specular = specularStrength * spec * lightColor;
-
- vec3 result = (ambient + diffuse + specular) * objectColor;
-
- FragColor = vec4(result, 1.0);
- }
- else if (renderState == 2) {
- vec4 result = vec4(0, 0, 0, 1);
- for (int i = 0; i < 13; i++) {
- vec2 sampleCoords = UV + samplePositions[i] * vec2(1.0/screenWidth, 1.0/screenHeight);
- //vec4 sample = texture(irradianceTexture, sampleCoords)
- // * texture(shadowmapTexture, sampleCoords);
- vec4 sample = texture(irradianceTexture, sampleCoords);
- vec4 weight = vec4(sampleWeights[i], 1);
- result += sample * weight;
- }
- FragColor = result;
- }
- else if (renderState == 3) {
+ // light the model
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
@@ -73,28 +38,34 @@ void main() vec3 result = vec3((ambient + diffuse + specular) * objectColor);
+ // sample irradiance as distance to light combined with angle to the light
+ // with a Gaussian kernel
vec3 result2 = vec3(0, 0, 0);
for (int i = 0; i < 13; i++) {
vec2 sampleCoords = UV + samplePositions[i] * vec2(1.0/screenWidth, 1.0/screenHeight);
//vec4 sample = texture(irradianceTexture, sampleCoords)
// * texture(shadowmapTexture, sampleCoords);
- vec3 sample = vec3(texture(irradianceTexture, sampleCoords));
+ vec3 sample = vec3(texture(irradianceTexture, sampleCoords)) * diff;
vec3 weight = sampleWeights[i];
result2 += sample * weight;
}
+ // multiply to apply irradiance, sqrt to get values between 0 and 1
result = sqrt(result * result2);
+ // sample texture to get distance from light
vec4 t = texture(irradianceTexture, UV);
-
float BacksideIrradiance = t.r; //*100 + t.g + t.b/100;
-
+ // and calculate world pos
vec3 Backside = (lightPos + (normalize(FragPos - lightPos) * BacksideIrradiance));
+ // 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
float distanceToBackside = length(FragPos - Backside);
if (distanceToBackside != 0)
result += objectColor * exp(2 / pow(distanceToBackside, 0.6)) * transmittanceScale * (1 - diff);
FragColor = vec4(result, 1);
- }
}
diff --git a/shaders/ts_frag_irradiance.glsl b/shaders/ts_frag_irradiance.glsl index b9e70ae..37d28f1 100644 --- a/shaders/ts_frag_irradiance.glsl +++ b/shaders/ts_frag_irradiance.glsl @@ -10,52 +10,11 @@ uniform vec3 lightColor; uniform vec3 objectColor;
uniform vec3 viewPos;
-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()
{
- vec3 norm = normalize(Normal);
- vec3 lightDir = normalize(lightPos - FragPos);
-
- float diff = max(dot(norm, lightDir), 0.0);
- vec3 diffuse = diff * lightColor;
-
- float ambientStrength = 0.1;
- vec3 ambient = ambientStrength * lightColor;
-
- float specularStrength = 0.5;
- vec3 viewDir = normalize(viewPos - FragPos);
- vec3 reflectDir = reflect(-lightDir, norm);
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
- vec3 specular = specularStrength * spec * lightColor;
-
- vec3 result = (ambient + diffuse) * objectColor;
-
- result = vec3(length(FragPos - lightPos));
+ // color the XY-plane according to the distance
+ // of the fragment in world space
+ vec3 result = vec3(length(FragPos - lightPos));
FragColor = vec4(result, 1.0f);
}
diff --git a/shaders/ts_vert.glsl b/shaders/ts_vert.glsl index f8e790a..3ee202f 100644 --- a/shaders/ts_vert.glsl +++ b/shaders/ts_vert.glsl @@ -14,6 +14,7 @@ uniform mat4 projection; void main()
{
+ // regular mvp
gl_Position = projection * view * model * vec4(pos, 1.0);
FragPos = vec3(model * vec4(pos, 1.0));
Normal = normal;
diff --git a/shaders/ts_vert_irradiance.glsl b/shaders/ts_vert_irradiance.glsl index afe5645..f0d77b9 100644 --- a/shaders/ts_vert_irradiance.glsl +++ b/shaders/ts_vert_irradiance.glsl @@ -13,7 +13,9 @@ uniform mat4 projection; void main()
{
- FragPos = vec3(model * vec4(pos, 1.0));
+ // lay out the model in the XY-plane according to it's UV coordinates
gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0);
+ // pass fragment position in world coordinates
+ FragPos = vec3(model * vec4(pos, 1.0));
Normal = normal;
}
diff --git a/src/main.cpp b/src/main.cpp index 8b48e70..168d01d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,18 +20,9 @@ #include <assimp/postprocess.h>
#include <assimp/scene.h>
-/*
-
-TODO:
-- Save Depth to fbo
-- Stencil Buffer
-- LightDist > 1
- - 1 - distanceToBackside in frag_irradiance
-- ShadowMap Perspective (no projection?)
-- (Implement Gaussian Blur)
-- LightDir nicht immer zu 0 0 0
-
-*/
+
+// sample positions and weights for a Gaussian kernel from
+// Hable, John ; Borshukov, George ; Hejl, Jim: Fast Skin Shading. In: ShaderX7, ShaderX : Charles River Media, 2009, S. 161–173
float samplePositions[] = {
0.000000f, 0.000000f,
diff --git a/src/main2.cpp b/src/main2.cpp index 206466d..5d717e6 100644 --- a/src/main2.cpp +++ b/src/main2.cpp @@ -20,6 +20,10 @@ #include <assimp/postprocess.h>
#include <assimp/scene.h>
+
+// sample positions and weights for a Gaussian kernel from
+// Hable, John ; Borshukov, George ; Hejl, Jim: Fast Skin Shading. In: ShaderX7, ShaderX : Charles River Media, 2009, S. 161–173
+
float samplePositions[] = {
0.000000f, 0.000000f,
1.633992f, 0.036795f,
@@ -383,7 +387,6 @@ int main() { GLuint shaderProgramIrradiance = compileShaders("shaders/ts_vert_irradiance.glsl", "shaders/ts_frag_irradiance.glsl");
GLuint shaderProgramCombine = compileShaders("shaders/ts_vert.glsl", "shaders/ts_frag.glsl");
- //model m = loadModel("models/Isotrop-upperjaw.ply");
model m = loadModel("models/african_head/african_head.obj");
arccam arcCam;
@@ -470,7 +473,7 @@ int main() { prevMouse = sf::Mouse::isButtonPressed(sf::Mouse::Right);
- // Render Shadowmap
+ // Render Shadowmap to fbo
glBindFramebuffer(GL_FRAMEBUFFER, fb_irradiance.fbo);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@@ -514,9 +517,7 @@ int main() { m.draw();
-
-
- // Render fbo to screen
+ // Render model and calculate light spread and translucency in shader
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@@ -569,16 +570,9 @@ int main() { glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, fb_irradiance.renderTexture);
- // glBindVertexArray(fb_irradiance.screenVAO);
- // glUniform1i(glGetUniformLocation(fb_irradiance.screenShaderProgram, "shadowmapTexture"), 0);
- // glActiveTexture(GL_TEXTURE0 + 0);
- // glBindTexture(GL_TEXTURE_2D, fb_irradiance.renderTexture);
- // glDrawArrays(GL_TRIANGLES, 0, 6);
- // glBindVertexArray(0);
-
m.draw();
-
+ // menu
ImGui::SFML::Update(window, deltaClock.restart());
|
