2 ms·
The blog's explanation with sun and light ray is confusing. Ray marching is for finding the interception of the object surface and the camera’s viewing directi
by ww520 4y ago
The blog's explanation with sun and light ray is confusing. Ray marching is for finding the interception of the object surface and the camera’s viewing direction. Whether there’s a sun or light is irrelevant.
The ray and the light source are two different things. The ray is from the camera to a point in the space. The light source is at a different location; it’s for computing light reflection on the object.
It's kind of weird and confusing to increment z only. It should increment the ray. E.g.
camera_pos = vec3(0.0, 0.0, -5.0) // eye is -5.0 in front of the screen
screen_pos = vec3(x, y, 0.0) // screen plane lays at 0.0 on z-axis.
ray_direction = normalize(screen_pos - camera_pos)
For ray marching along a ray, each step extends the ray by a certain amount.
marched_distance = 0.0
for 0 to N
ray_end = camera_pos + marched_distance * ray_direction
ray_end_to_object_distance = sdf_sphere(ray_end)
if (ray_end_to_object_distance < 0.0001)
return ray_end // ray_end hit the object surface
marched_distance += ray_end_to_object_distance
return null // ray never met the object after N steps.
The object's reflection at the returned ray_end point can be computed using the surface normal at the point, the light source position, and the camera_pos.