diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-03-11 01:34:04 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-03-11 01:34:04 +0800 |
| commit | 1c94e5e61537a1d48052d14f988e565a8ca8b616 (patch) | |
| tree | f9bd83025146fac1a170fa222c9af9c7407e4ab4 /apps | |
| parent | 5dedba12be2ae7a8b2ba6fcc690bab963f1b733a (diff) | |
| download | ray-tracing-master.tar.gz ray-tracing-master.tar.xz | |
Diffstat (limited to 'apps')
| -rw-r--r-- | apps/main.cc | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/apps/main.cc b/apps/main.cc index 4884f6e..2a73d94 100644 --- a/apps/main.cc +++ b/apps/main.cc @@ -20,21 +20,29 @@ #include <iostream> -bool hit_sphere(const point3& center, double radius, const ray& r){ +double 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 ); + + if ( discriminant < 0 ) { + return -1.0; + } else { + return ( -b - std::sqrt( discriminant ) ) / ( 2.0 * a ); + } } color ray_color( const ray& r ) { - if ( hit_sphere( point3( 0, 0, -1 ), 0.5, r ) ) - return color( 1, 0, 0 ); + auto t = hit_sphere( point3( 0, 0, -1 ), 0.5, r ); + if ( t > 0.0 ) { + vec3 N = unit_vector( r.at( t ) - vec3( 0, 0, -1 ) ); + return 0.5 * color( N.x() + 1, N.y() + 1, N.z() + 1 ); + } - 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 ); |
