Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

negative value for sqrt() #45

Open
beantowel opened this issue Jan 24, 2023 · 0 comments
Open

negative value for sqrt() #45

beantowel opened this issue Jan 24, 2023 · 0 comments

Comments

@beantowel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant