Skip to content

Commit fc408e3

Browse files
committed
Add sun disc to preethan
1 parent 39b94c6 commit fc408e3

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

src/appleseed/renderer/modeling/environmentedf/hosekenvironmentedf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ namespace
135135
// Evaluate uniform values.
136136
m_inputs.evaluate_uniforms(&m_uniform_values);
137137

138-
// If there is a binded sun get it
138+
// If there is a bound sun get it.
139139
m_sun = dynamic_cast<SunLight*>(m_inputs.get_entity("sun_light"));
140140

141141
// Compute the sun direction.

src/appleseed/renderer/modeling/environmentedf/preethamenvironmentedf.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "renderer/modeling/input/inputarray.h"
4040
#include "renderer/modeling/input/source.h"
4141
#include "renderer/modeling/input/sourceinputs.h"
42+
#include "renderer/modeling/light/sunlight.h"
4243
#include "renderer/utility/transformsequence.h"
4344

4445
// appleseed.foundation headers.
@@ -102,6 +103,7 @@ namespace
102103
m_inputs.declare("luminance_gamma", InputFormat::Float, "1.0");
103104
m_inputs.declare("saturation_multiplier", InputFormat::Float, "1.0");
104105
m_inputs.declare("horizon_shift", InputFormat::Float, "0.0");
106+
m_inputs.declare("sun_light", InputFormat::Entity, "");
105107
}
106108

107109
void release() override
@@ -126,6 +128,9 @@ namespace
126128
// Evaluate uniform values.
127129
m_inputs.evaluate_uniforms(&m_uniform_values);
128130

131+
// If there is a bound sun get it.
132+
m_sun = dynamic_cast<SunLight*>(m_inputs.get_entity("sun_light"));
133+
129134
// Compute the sun direction.
130135
m_sun_theta = deg_to_rad(m_uniform_values.m_sun_theta);
131136
m_sun_phi = deg_to_rad(m_uniform_values.m_sun_phi);
@@ -185,6 +190,10 @@ namespace
185190
{
186191
assert(is_normalized(outgoing));
187192

193+
Spectrum sun_value(0.0f);
194+
if (m_sun)
195+
m_sun->evaluate(Vector3d(outgoing.x, outgoing.y, outgoing.z), sun_value);
196+
188197
Transformd scratch;
189198
const Transformd& transform = m_transform_sequence.evaluate(0.0f, scratch);
190199
const Vector3f local_outgoing = transform.vector_to_local(outgoing);
@@ -196,6 +205,7 @@ namespace
196205
else radiance.set(0.0f);
197206

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

201211
void evaluate(
@@ -206,6 +216,10 @@ namespace
206216
{
207217
assert(is_normalized(outgoing));
208218

219+
Spectrum sun_value(0.0f);
220+
if (m_sun)
221+
m_sun->evaluate(Vector3d(outgoing.x, outgoing.y, outgoing.z), sun_value);
222+
209223
Transformd scratch;
210224
const Transformd& transform = m_transform_sequence.evaluate(0.0f, scratch);
211225
const Vector3f local_outgoing = transform.vector_to_local(outgoing);
@@ -217,6 +231,7 @@ namespace
217231
else radiance.set(0.0f);
218232

219233
value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance);
234+
value += sun_value;
220235
probability = shifted_outgoing.y > 0.0f ? shifted_outgoing.y * RcpPi<float>() : 0.0f;
221236
assert(probability >= 0.0f);
222237
}
@@ -265,6 +280,8 @@ namespace
265280
float m_uniform_y_zenith;
266281
float m_uniform_Y_zenith;
267282

283+
SunLight* m_sun;
284+
268285
// Compute the coefficients of the luminance distribution function.
269286
static void compute_Y_coefficients(
270287
const float turbidity,

src/appleseed/renderer/modeling/light/sunlight.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ bool SunLight::on_frame_begin(
150150
// Reference: https://en.wikipedia.org/wiki/Solid_angle#Sun_and_Moon
151151
m_sun_solid_angle = TwoPi<float>() * (1.0f - std::cos(std::atan(SunRadius * m_values.m_size_multiplier / m_values.m_distance)));
152152

153-
154153
// If the Sun light is bound to an environment EDF, let it override the Sun's direction and turbidity.
155154
EnvironmentEDF* env_edf = dynamic_cast<EnvironmentEDF*>(m_inputs.get_entity("environment_edf"));
156155
if (env_edf != nullptr)
@@ -274,39 +273,36 @@ void SunLight::evaluate(
274273
}
275274

276275
const Vector3d local_outgoing = normalize(get_transform().point_to_local(outgoing));
277-
const double cos_theta = dot(local_outgoing, Vector3d(0.0, 0.0, 1.0));
278-
const double sin_theta = std::sqrt(1.0 - cos_theta * cos_theta);
279-
280-
const double sin_theta_max = SunRadius * m_values.m_size_multiplier / m_values.m_distance;
281-
const double cos_theta_max = std::sqrt(1.0 - sin_theta_max * sin_theta_max);
276+
const double angle = std::acos(dot(local_outgoing, Vector3d(0.0, 0.0, 1.0)));
282277

278+
const double max_angle = SunRadius * m_values.m_size_multiplier / m_values.m_distance;
283279

284-
if (cos_theta < cos_theta_max)
280+
if (angle > std::atan(max_angle))
285281
{
286282
value.set(0.0f);
287283
return;
288284
}
289285

290-
const double distance_to_center = SunRadius * m_values.m_size_multiplier *
291-
((sin_theta / cos_theta) / (sin_theta_max / cos_theta_max));
286+
const double distance_to_center = std::tan(angle) * m_values.m_distance;
292287

293288
RegularSpectrum31f radiance;
294289
compute_sun_radiance(
295290
-outgoing,
296291
m_values.m_turbidity,
297292
m_values.m_radiance_multiplier,
298293
radiance,
299-
static_cast<float>(distance_to_center));
294+
square(static_cast<float>(distance_to_center)));
300295

301296
value.set(radiance, g_std_lighting_conditions, Spectrum::Illuminance);
297+
value *= m_sun_solid_angle;
302298
}
303299

304300
void SunLight::compute_sun_radiance(
305-
const Vector3d& outgoing,
306-
const float turbidity,
307-
const float radiance_multiplier,
308-
RegularSpectrum31f& radiance,
309-
const float distance_to_center) const
301+
const Vector3d& outgoing,
302+
const float turbidity,
303+
const float radiance_multiplier,
304+
RegularSpectrum31f& radiance,
305+
const float squared_distance_to_center) const
310306
{
311307
// Compute the relative optical mass.
312308
const float cos_theta = -static_cast<float>(outgoing.y);
@@ -411,11 +407,11 @@ void SunLight::compute_sun_radiance(
411407

412408
constexpr float LimbDarkeningCoeficent = 0.6f; // Limb darkening coefficient for the sun for visible sunlight.
413409
float limb_darkening = 1.0f;
414-
if (distance_to_center > 0.0f)
410+
if (squared_distance_to_center > 0.0f)
415411
{
416412
limb_darkening = (1.0f - LimbDarkeningCoeficent *
417-
(1.0f - std::sqrt(1.0f - std::pow(distance_to_center, 2.0f) /
418-
std::pow(SunRadius * m_values.m_size_multiplier, 2.0f))));
413+
(1.0f - std::sqrt(1.0f - squared_distance_to_center
414+
/ square(SunRadius * m_values.m_size_multiplier))));
419415
}
420416

421417
// Compute the attenuated radiance of the Sun.
@@ -570,7 +566,8 @@ void SunLight::sample_sun_surface(
570566
+ sun_radius * p[1] * basis.get_tangent_v();
571567

572568
outgoing = normalize(target_point - position);
573-
float distance_to_center = SunRadius * m_values.m_size_multiplier * float(std::sqrt(p[0] * p[0] + p[1] * p[1]));
569+
Vector2d disk_cord = static_cast<double>(SunRadius * m_values.m_size_multiplier) * p;
570+
double squared_distance_to_center = disk_cord[0] * disk_cord[0] + disk_cord[1] * disk_cord[1];
574571

575572

576573
RegularSpectrum31f radiance;
@@ -579,7 +576,7 @@ void SunLight::sample_sun_surface(
579576
m_values.m_turbidity,
580577
m_values.m_radiance_multiplier,
581578
radiance,
582-
distance_to_center);
579+
static_cast<float>(squared_distance_to_center));
583580

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

0 commit comments

Comments
 (0)