From fc408e32fb673fb87bfcefcb7bf192e5cf1b0a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Mororo=20Costa?= <43460229+mororo250@users.noreply.github.com> Date: Wed, 8 Jul 2020 22:50:35 -0300 Subject: [PATCH] Add sun disc to preethan --- .../environmentedf/hosekenvironmentedf.cpp | 2 +- .../environmentedf/preethamenvironmentedf.cpp | 17 +++++++++ .../renderer/modeling/light/sunlight.cpp | 37 +++++++++---------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/appleseed/renderer/modeling/environmentedf/hosekenvironmentedf.cpp b/src/appleseed/renderer/modeling/environmentedf/hosekenvironmentedf.cpp index cf8b2e82b6..22b26b4964 100644 --- a/src/appleseed/renderer/modeling/environmentedf/hosekenvironmentedf.cpp +++ b/src/appleseed/renderer/modeling/environmentedf/hosekenvironmentedf.cpp @@ -135,7 +135,7 @@ namespace // Evaluate uniform values. m_inputs.evaluate_uniforms(&m_uniform_values); - // If there is a binded sun get it + // If there is a bound sun get it. m_sun = dynamic_cast(m_inputs.get_entity("sun_light")); // Compute the sun direction. diff --git a/src/appleseed/renderer/modeling/environmentedf/preethamenvironmentedf.cpp b/src/appleseed/renderer/modeling/environmentedf/preethamenvironmentedf.cpp index 41b5c04128..acb91e0270 100644 --- a/src/appleseed/renderer/modeling/environmentedf/preethamenvironmentedf.cpp +++ b/src/appleseed/renderer/modeling/environmentedf/preethamenvironmentedf.cpp @@ -39,6 +39,7 @@ #include "renderer/modeling/input/inputarray.h" #include "renderer/modeling/input/source.h" #include "renderer/modeling/input/sourceinputs.h" +#include "renderer/modeling/light/sunlight.h" #include "renderer/utility/transformsequence.h" // appleseed.foundation headers. @@ -102,6 +103,7 @@ namespace m_inputs.declare("luminance_gamma", InputFormat::Float, "1.0"); m_inputs.declare("saturation_multiplier", InputFormat::Float, "1.0"); m_inputs.declare("horizon_shift", InputFormat::Float, "0.0"); + m_inputs.declare("sun_light", InputFormat::Entity, ""); } void release() override @@ -126,6 +128,9 @@ namespace // Evaluate uniform values. m_inputs.evaluate_uniforms(&m_uniform_values); + // If there is a bound sun get it. + m_sun = dynamic_cast(m_inputs.get_entity("sun_light")); + // Compute the sun direction. m_sun_theta = deg_to_rad(m_uniform_values.m_sun_theta); m_sun_phi = deg_to_rad(m_uniform_values.m_sun_phi); @@ -185,6 +190,10 @@ namespace { assert(is_normalized(outgoing)); + Spectrum sun_value(0.0f); + if (m_sun) + m_sun->evaluate(Vector3d(outgoing.x, outgoing.y, outgoing.z), sun_value); + Transformd scratch; const Transformd& transform = m_transform_sequence.evaluate(0.0f, scratch); const Vector3f local_outgoing = transform.vector_to_local(outgoing); @@ -196,6 +205,7 @@ namespace else radiance.set(0.0f); value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance); + value += sun_value; } void evaluate( @@ -206,6 +216,10 @@ namespace { assert(is_normalized(outgoing)); + Spectrum sun_value(0.0f); + if (m_sun) + m_sun->evaluate(Vector3d(outgoing.x, outgoing.y, outgoing.z), sun_value); + Transformd scratch; const Transformd& transform = m_transform_sequence.evaluate(0.0f, scratch); const Vector3f local_outgoing = transform.vector_to_local(outgoing); @@ -217,6 +231,7 @@ namespace else radiance.set(0.0f); value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance); + value += sun_value; probability = shifted_outgoing.y > 0.0f ? shifted_outgoing.y * RcpPi() : 0.0f; assert(probability >= 0.0f); } @@ -265,6 +280,8 @@ namespace float m_uniform_y_zenith; float m_uniform_Y_zenith; + SunLight* m_sun; + // Compute the coefficients of the luminance distribution function. static void compute_Y_coefficients( const float turbidity, diff --git a/src/appleseed/renderer/modeling/light/sunlight.cpp b/src/appleseed/renderer/modeling/light/sunlight.cpp index 9b9e83d503..9b14f15cb1 100644 --- a/src/appleseed/renderer/modeling/light/sunlight.cpp +++ b/src/appleseed/renderer/modeling/light/sunlight.cpp @@ -150,7 +150,6 @@ bool SunLight::on_frame_begin( // Reference: https://en.wikipedia.org/wiki/Solid_angle#Sun_and_Moon m_sun_solid_angle = TwoPi() * (1.0f - std::cos(std::atan(SunRadius * m_values.m_size_multiplier / m_values.m_distance))); - // If the Sun light is bound to an environment EDF, let it override the Sun's direction and turbidity. EnvironmentEDF* env_edf = dynamic_cast(m_inputs.get_entity("environment_edf")); if (env_edf != nullptr) @@ -274,21 +273,17 @@ void SunLight::evaluate( } const Vector3d local_outgoing = normalize(get_transform().point_to_local(outgoing)); - const double cos_theta = dot(local_outgoing, Vector3d(0.0, 0.0, 1.0)); - const double sin_theta = std::sqrt(1.0 - cos_theta * cos_theta); - - const double sin_theta_max = SunRadius * m_values.m_size_multiplier / m_values.m_distance; - const double cos_theta_max = std::sqrt(1.0 - sin_theta_max * sin_theta_max); + const double angle = std::acos(dot(local_outgoing, Vector3d(0.0, 0.0, 1.0))); + const double max_angle = SunRadius * m_values.m_size_multiplier / m_values.m_distance; - if (cos_theta < cos_theta_max) + if (angle > std::atan(max_angle)) { value.set(0.0f); return; } - const double distance_to_center = SunRadius * m_values.m_size_multiplier * - ((sin_theta / cos_theta) / (sin_theta_max / cos_theta_max)); + const double distance_to_center = std::tan(angle) * m_values.m_distance; RegularSpectrum31f radiance; compute_sun_radiance( @@ -296,17 +291,18 @@ void SunLight::evaluate( m_values.m_turbidity, m_values.m_radiance_multiplier, radiance, - static_cast(distance_to_center)); + square(static_cast(distance_to_center))); value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance); + value *= m_sun_solid_angle; } void SunLight::compute_sun_radiance( - const Vector3d& outgoing, - const float turbidity, - const float radiance_multiplier, - RegularSpectrum31f& radiance, - const float distance_to_center) const + const Vector3d& outgoing, + const float turbidity, + const float radiance_multiplier, + RegularSpectrum31f& radiance, + const float squared_distance_to_center) const { // Compute the relative optical mass. const float cos_theta = -static_cast(outgoing.y); @@ -411,11 +407,11 @@ void SunLight::compute_sun_radiance( constexpr float LimbDarkeningCoeficent = 0.6f; // Limb darkening coefficient for the sun for visible sunlight. float limb_darkening = 1.0f; - if (distance_to_center > 0.0f) + if (squared_distance_to_center > 0.0f) { limb_darkening = (1.0f - LimbDarkeningCoeficent * - (1.0f - std::sqrt(1.0f - std::pow(distance_to_center, 2.0f) / - std::pow(SunRadius * m_values.m_size_multiplier, 2.0f)))); + (1.0f - std::sqrt(1.0f - squared_distance_to_center + / square(SunRadius * m_values.m_size_multiplier)))); } // Compute the attenuated radiance of the Sun. @@ -570,7 +566,8 @@ void SunLight::sample_sun_surface( + sun_radius * p[1] * basis.get_tangent_v(); outgoing = normalize(target_point - position); - float distance_to_center = SunRadius * m_values.m_size_multiplier * float(std::sqrt(p[0] * p[0] + p[1] * p[1])); + Vector2d disk_cord = static_cast(SunRadius * m_values.m_size_multiplier) * p; + double squared_distance_to_center = disk_cord[0] * disk_cord[0] + disk_cord[1] * disk_cord[1]; RegularSpectrum31f radiance; @@ -579,7 +576,7 @@ void SunLight::sample_sun_surface( m_values.m_turbidity, m_values.m_radiance_multiplier, radiance, - distance_to_center); + static_cast(squared_distance_to_center)); value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance);