@@ -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
304300void 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