146 lines
5 KiB
C++
146 lines
5 KiB
C++
#include <random>
|
|
#include "toneshader.h"
|
|
|
|
|
|
ToneShader::ToneShader(const Color &highlightColor, const Color &midtoneColor, const Color &shadowColor,
|
|
float edgeNoise)
|
|
: highlightColor(highlightColor),
|
|
midtoneColor(midtoneColor),
|
|
shadowColor(shadowColor),
|
|
edgeNoise(edgeNoise)
|
|
{
|
|
|
|
}
|
|
|
|
Color ToneShader::shade(const Scene &scene, const Ray &ray) const
|
|
{
|
|
Color fragmentColor;
|
|
float brightness;
|
|
|
|
// Look at first interesting light
|
|
for (const auto &light: scene.lights())
|
|
{
|
|
Light::Illumination illum;
|
|
illum = light->illuminate(scene, ray);
|
|
|
|
if (illum.distance == 0.0f)
|
|
continue; // Skip ambient light
|
|
|
|
if (illum.color.r < 0.01f)
|
|
// Light does not reach the surface
|
|
brightness = 0;
|
|
else
|
|
// Look at angle of light
|
|
brightness = dotProduct(illum.direction,
|
|
-ray.normal); // 0 if light is behind surface, 1 if light is in front of surface
|
|
|
|
break; // No other light needed
|
|
}
|
|
|
|
// Accumulate the light over all light sources
|
|
// for (const auto &light: scene.lights())
|
|
// {
|
|
// Light::Illumination illum;
|
|
// illum = light->illuminate(scene, ray);
|
|
//
|
|
// float const NdotL = std::max(0.0f, dotProduct(-illum.direction, ray.normal));
|
|
// if (NdotL <= 0.0f)
|
|
// continue;
|
|
//
|
|
// // Diffuse term
|
|
// Color const diffuse = this->highlightColor / float(PI);
|
|
// fragmentColor += diffuse * NdotL * illum.color;
|
|
//
|
|
// // Cook-Torrance term
|
|
// // half angle vector
|
|
// Vector3d const H = normalized(-illum.direction - ray.direction);
|
|
// float const NdotH = std::max(0.0f, dotProduct(ray.normal, H));
|
|
// float const NdotV = std::max(0.0f, dotProduct(ray.normal, -ray.direction));
|
|
// float const VdotH = std::max(0.0f, dotProduct(-ray.direction, H));
|
|
//
|
|
// if (NdotV * NdotL > EPSILON)
|
|
// {
|
|
// fragmentColor += NdotL * illum.color;
|
|
// }
|
|
// }
|
|
|
|
// Accumulate the light over all light sources
|
|
// for (const auto &light : scene.lights()) {
|
|
// Light::Illumination const illum = light->illuminate(scene, ray);
|
|
// // Diffuse term
|
|
// Color const diffuse = this->highlightColor * std::max(dotProduct(-illum.direction, ray.normal), 0.0f);
|
|
// fragmentColor += diffuse * illum.color;
|
|
// }
|
|
|
|
// return fragmentColor;
|
|
// for (const auto &light: scene.lights())
|
|
// brightness += light->illuminate(scene, ray).color.r > 0.01f; // Basically count the light sources that hit
|
|
//
|
|
// brightness /= scene.lights().size();
|
|
|
|
// for (const auto &light: scene.lights())
|
|
// fragmentColor += light->illuminate(scene, ray).color;
|
|
//
|
|
// brightness = (fragmentColor.r
|
|
// + fragmentColor.g
|
|
// + fragmentColor.b) / 3;
|
|
|
|
// Handle peak brightness
|
|
// float const PEAK_AREA = 0.0f;
|
|
// float peak = brightness - 1;
|
|
// brightness *= 1 - PEAK_AREA;
|
|
// if (peak > 0)
|
|
// brightness = 1 - PEAK_AREA + PEAK_AREA * 2 * (1 / (exp(-peak / 10.0f) + 1) - 0.5f);
|
|
|
|
|
|
// float viewFactor = std::acos(dotProduct(ray.direction, -ray.normal)) / float(PI); // 0 if normal is facing the view, 1 if normal is facing away from the view
|
|
// viewFactor = pow(viewFactor, 3) * 20;
|
|
// if (viewFactor > 1)
|
|
// viewFactor = 1;
|
|
//
|
|
// brightness = pow(brightness, 1 + 3 * viewFactor);
|
|
// brightness = pow(brightness, 2);
|
|
|
|
// return brightness * Color(1, 1, 1);
|
|
|
|
// Get the tone color base on the brightness
|
|
float const HIGHLIGHT_THRESHOLD = 0.95f;
|
|
float const MIDTONE_HIGHLIGHT_THRESHOLD = HIGHLIGHT_THRESHOLD - 0.2f * edgeNoise;
|
|
float const SHADOW_MIDTONE_THRESHOLD = 0.01f;
|
|
float const MIDTONE_THRESHOLD = SHADOW_MIDTONE_THRESHOLD + 0.4f * edgeNoise;
|
|
|
|
// Used for transitions
|
|
std::random_device dev;
|
|
std::uniform_real_distribution<float> randomDistribution(0, 1);
|
|
|
|
Color tone;
|
|
if (brightness > HIGHLIGHT_THRESHOLD)
|
|
{
|
|
tone = highlightColor;
|
|
} else if (brightness > MIDTONE_HIGHLIGHT_THRESHOLD)
|
|
{
|
|
float transition =
|
|
(brightness - MIDTONE_HIGHLIGHT_THRESHOLD) / (HIGHLIGHT_THRESHOLD - MIDTONE_HIGHLIGHT_THRESHOLD);
|
|
tone = highlightColor * transition + midtoneColor * (1 - transition);
|
|
// if (randomDistribution(dev) < transition)
|
|
// tone = highlightColor;
|
|
// else
|
|
// tone = midtoneColor;
|
|
} else if (brightness > MIDTONE_THRESHOLD)
|
|
{
|
|
tone = midtoneColor;
|
|
} else if (brightness > SHADOW_MIDTONE_THRESHOLD)
|
|
{
|
|
float transition = (brightness - SHADOW_MIDTONE_THRESHOLD) / (MIDTONE_THRESHOLD - SHADOW_MIDTONE_THRESHOLD);
|
|
tone = midtoneColor * transition + shadowColor * (1 - transition);
|
|
// if (randomDistribution(dev) < transition)
|
|
// tone = midtoneColor;
|
|
// else
|
|
// tone = shadowColor;
|
|
} else
|
|
{
|
|
tone = shadowColor;
|
|
}
|
|
|
|
return tone;
|
|
}
|