ebruneton / precomputed_atmospheric_scattering

This project provides a new implementation of our EGSR 2008 paper "Precomputed Atmospheric Scattering".
BSD 3-Clause "New" or "Revised" License
908 stars 120 forks source link

negative value for sqrt() #45

Open beantowel opened 1 year ago

beantowel commented 1 year ago

In the demo shader atmosphere/demo/demo.glsl, when testing whether the view ray intersects with the ground:

  p = camera - earth_center;
  p_dot_v = dot(p, view_direction);
  p_dot_p = dot(p, p);
  float ray_earth_center_squared_distance = p_dot_p - p_dot_v * p_dot_v;
  distance_to_intersection = -p_dot_v - sqrt(
      earth_center.z * earth_center.z - ray_earth_center_squared_distance);
  //...
  if (distance_to_intersection > 0.0) {
  //...
  }

There may be negative values passing into the sqrt function. When testing on PC, it's ok, but when I ported the code to android platform, the horizon had the wrong color. We'd better test if the inner value is positive before testing the distance:

  float delta_intersection_square = earth_center.z * earth_center.z - ray_earth_center_squared_distance;
  distance_to_intersection = -p_dot_v - sqrt(delta_intersection_square);
  //...
  if (delta_intersection_square > 0.0 && distance_to_intersection > 0.0) {
  //...
  }

horizon