@@ -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,35 @@ 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);
302297}
303298
304299void 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
300+ const Vector3d& outgoing,
301+ const float turbidity,
302+ const float radiance_multiplier,
303+ RegularSpectrum31f& radiance,
304+ const float squared_distance_to_center ) const
310305{
311306 // Compute the relative optical mass.
312307 const float cos_theta = -static_cast <float >(outgoing.y );
@@ -411,11 +406,11 @@ void SunLight::compute_sun_radiance(
411406
412407 constexpr float LimbDarkeningCoeficent = 0 .6f ; // Limb darkening coefficient for the sun for visible sunlight.
413408 float limb_darkening = 1 .0f ;
414- if (distance_to_center > 0 .0f )
409+ if (squared_distance_to_center > 0 .0f )
415410 {
416411 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 ))));
412+ (1 .0f - std::sqrt (1 .0f - squared_distance_to_center
413+ / square (SunRadius * m_values.m_size_multiplier ))));
419414 }
420415
421416 // Compute the attenuated radiance of the Sun.
@@ -570,7 +565,8 @@ void SunLight::sample_sun_surface(
570565 + sun_radius * p[1 ] * basis.get_tangent_v ();
571566
572567 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 ]));
568+ Vector2d test = static_cast <double >(SunRadius * m_values.m_size_multiplier ) * p;
569+ float squared_distance_to_center = test[0 ] * test[0 ] + test[1 ] * test[1 ];
574570
575571
576572 RegularSpectrum31f radiance;
@@ -579,7 +575,7 @@ void SunLight::sample_sun_surface(
579575 m_values.m_turbidity ,
580576 m_values.m_radiance_multiplier ,
581577 radiance,
582- distance_to_center );
578+ squared_distance_to_center );
583579
584580 value.set (radiance, g_std_lighting_conditions, Spectrum::Illuminance);
585581
0 commit comments