precomputed_atmospheric_scattering icon indicating copy to clipboard operation
precomputed_atmospheric_scattering copied to clipboard

negative value for sqrt()

Open beantowel opened this issue 2 years ago • 0 comments

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

beantowel avatar Jan 24 '23 17:01 beantowel