diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-03-11 01:17:41 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-03-11 01:17:41 +0800 |
| commit | 5dedba12be2ae7a8b2ba6fcc690bab963f1b733a (patch) | |
| tree | 88a5ea47e825eb8a5efa3b3ca67354703839f1dc /apps/main.cc | |
| parent | 4014436905aace9acd47dde245b88bd5f69335ec (diff) | |
| download | ray-tracing-5dedba12be2ae7a8b2ba6fcc690bab963f1b733a.tar.gz ray-tracing-5dedba12be2ae7a8b2ba6fcc690bab963f1b733a.tar.xz | |
Adding a Sphere
Diffstat (limited to 'apps/main.cc')
| -rw-r--r-- | apps/main.cc | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/apps/main.cc b/apps/main.cc index d5702cd..4884f6e 100644 --- a/apps/main.cc +++ b/apps/main.cc @@ -20,8 +20,21 @@ #include <iostream> +bool hit_sphere(const point3& center, double radius, const ray& r){ + vec3 oc = center - r.origin(); + auto a = dot( r.direction(), r.direction() ); + auto b = -2.0 * dot( r.direction(), oc ); + auto c = dot( oc, oc ) - radius * radius; + auto discriminant = b * b - 4 * a * c; + return ( discriminant >= 0 ); +} + color ray_color( const ray& r ) { + if ( hit_sphere( point3( 0, 0, -1 ), 0.5, r ) ) + return color( 1, 0, 0 ); + + vec3 unit_direction = unit_vector( r.direction() ); auto a = 0.5 * ( unit_direction.y() + 1.0 ); return ( 1.0 - a ) * color( 1.0, 1.0, 1.0 ) + a * color( 0.5, 0.7, 1.0 ); |
