aboutsummaryrefslogtreecommitdiff
path: root/raytracing/apps/main.cc
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-03-11 01:17:41 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-03-11 01:17:41 +0800
commit4154fae8ac68fbb44e8edcc69f989bce13333b5b (patch)
tree522d2dfac41e76aee6785973305abab21ba50932 /raytracing/apps/main.cc
parentd83cdc8de4d9160fd3eecae5af450aa26c2df154 (diff)
downloadrcc-project-4154fae8ac68fbb44e8edcc69f989bce13333b5b.tar.gz
rcc-project-4154fae8ac68fbb44e8edcc69f989bce13333b5b.tar.xz
Adding a Sphere
Diffstat (limited to 'raytracing/apps/main.cc')
-rw-r--r--raytracing/apps/main.cc13
1 files changed, 13 insertions, 0 deletions
diff --git a/raytracing/apps/main.cc b/raytracing/apps/main.cc
index d5702cd..4884f6e 100644
--- a/raytracing/apps/main.cc
+++ b/raytracing/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 );