Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. vec3 ray_march(in vec3 ro, in vec3 rd)
  2. {
  3. float total_distance_traveled = 0.0;
  4. const int NUMBER_OF_STEPS = 32;
  5. const float MINIMUM_HIT_DISTANCE = 0.001;
  6. const float MAXIMUM_TRACE_DISTANCE = 1000.0;
  7.  
  8. for (int i = 0; i < NUMBER_OF_STEPS; ++i)
  9. {
  10. // Calculate our current position along the ray
  11. vec3 current_position = ro + total_distance_traveled * rd;
  12.  
  13. // We wrote this function earlier in the tutorial -
  14. // assume that the sphere is centered at the origin
  15. // and has unit radius
  16. float distance_to_closest = distance_from_sphere(current_position, vec3(0.0), 1.0);
  17.  
  18. if (distance_to_closest < MINIMUM_HIT_DISTANCE) // hit
  19. {
  20. // We hit something! Return red for now
  21. return vec3(1.0, 0.0, 0.0);
  22. }
  23.  
  24. if (total_distance_traveled > MAXIMUM_TRACE_DISTANCE) // miss
  25. {
  26. break;
  27. }
  28.  
  29. // accumulate the distance traveled thus far
  30. total_distance_traveled += distance_to_closest;
  31. }
  32.  
  33. // If we get here, we didn't hit anything so just
  34. // return a background color (black)
  35. return vec3(0.0);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement