Skip to content

Commit 85025c2

Browse files
committed
Listing 12 Rendering surface normals on a sphere
1 parent 96c7ed0 commit 85025c2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/InOneWeekend/main.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44

55
#include <iostream>
66

7-
bool hit_sphere(const point3& center, double radius, const ray& r) {
7+
double hit_sphere(const point3& center, double radius, const ray& r) {
88
vec3 oc = center - r.origin();
99
auto a = dot(r.direction(), r.direction());
1010
auto b = -2.0 * dot(r.direction(), oc);
1111
auto c = dot(oc, oc) - radius*radius;
1212
auto discriminant = b*b - 4*a*c;
13-
return (discriminant >= 0);
13+
14+
if (discriminant < 0) {
15+
return -1.0;
16+
} else {
17+
return (-b - sqrt(discriminant) ) / (2.0*a);
18+
}
1419
}
1520

1621
color ray_color(const ray& r) {
17-
if (hit_sphere(point3(0,0,-1), 0.5, r))
18-
return color(1, 0, 0);
22+
auto t = hit_sphere(point3(0,0,-1), 0.5, r);
23+
if (t > 0.0) {
24+
vec3 N = unit_vector(r.at(t) - vec3(0,0,-1));
25+
return 0.5*color(N.x()+1, N.y()+1, N.z()+1);
26+
}
1927

2028
vec3 unit_direction = unit_vector(r.direction());
2129
auto a = 0.5*(unit_direction.y() + 1.0);

0 commit comments

Comments
 (0)