Skip to content

Commit

Permalink
Add sun disc to preethan
Browse files Browse the repository at this point in the history
  • Loading branch information
mororo250 committed Jul 10, 2020
1 parent 39b94c6 commit fc408e3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<SunLight*>(m_inputs.get_entity("sun_light"));

// Compute the sun direction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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<SunLight*>(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);
Expand Down Expand Up @@ -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);
Expand All @@ -196,6 +205,7 @@ namespace
else radiance.set(0.0f);

value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance);
value += sun_value;
}

void evaluate(
Expand All @@ -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);
Expand All @@ -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<float>() : 0.0f;
assert(probability >= 0.0f);
}
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 17 additions & 20 deletions src/appleseed/renderer/modeling/light/sunlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>() * (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<EnvironmentEDF*>(m_inputs.get_entity("environment_edf"));
if (env_edf != nullptr)
Expand Down Expand Up @@ -274,39 +273,36 @@ 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(
-outgoing,
m_values.m_turbidity,
m_values.m_radiance_multiplier,
radiance,
static_cast<float>(distance_to_center));
square(static_cast<float>(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<float>(outgoing.y);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<double>(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;
Expand All @@ -579,7 +576,7 @@ void SunLight::sample_sun_surface(
m_values.m_turbidity,
m_values.m_radiance_multiplier,
radiance,
distance_to_center);
static_cast<float>(squared_distance_to_center));

value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance);

Expand Down

0 comments on commit fc408e3

Please sign in to comment.