From 0a5e8172a6ee0e79c81eec21a9966bea9385b249 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 26 Mar 2021 20:12:25 +0100 Subject: [PATCH] comments TS SSS --- shaders/ts_frag.glsl | 51 +++++++-------------------------- shaders/ts_frag_irradiance.glsl | 47 ++---------------------------- shaders/ts_vert.glsl | 1 + shaders/ts_vert_irradiance.glsl | 4 ++- src/main.cpp | 15 ++-------- 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 #include -/* - -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 #include + +// 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()); -- 2.50.1