Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Change Log / Ray Tracing in One Weekend
- Fix -- Big improvement to print version listing font size (#1595) and more compact line
height for code listings in both print and browser.
- Change -- Include hittable.h from material.h; drop `hit_record` forward declaration (#1609)
- Fix -- Slight improvement to `rotate_y::hit()` function (#1484)
- Fix -- Fixed possible bogus values from `random_unit_vector()` due to underflow (#1606)

### In One Weekend
Expand Down
43 changes: 25 additions & 18 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -3816,34 +3816,41 @@
public:

bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
// Change the ray from world space to object space
auto origin = r.origin();
auto direction = r.direction();

origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
// Transform the ray from world space to object space.

direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
auto origin = point3(
(cos_theta * r.origin().x()) - (sin_theta * r.origin().z()),
r.origin().y(),
(sin_theta * r.origin().x()) + (cos_theta * r.origin().z())
);

auto direction = vec3(
(cos_theta * r.direction().x()) - (sin_theta * r.direction().z()),
r.direction().y(),
(sin_theta * r.direction().x()) + (cos_theta * r.direction().z())
);

ray rotated_r(origin, direction, r.time());

// Determine whether an intersection exists in object space (and if so, where)
// Determine whether an intersection exists in object space (and if so, where).

if (!object->hit(rotated_r, ray_t, rec))
return false;

// Change the intersection point from object space to world space
auto p = rec.p;
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
// Transform the intersection from object space back to world space.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, there was a comment about the intersection and one about the normal. Now the one about the normal is gone. Should that just be integrated into this comment, or did you drop mention of the normal on purpose?

Copy link
Collaborator Author

@hollasch hollasch Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered the normal to be part of the intersection data (which I why I didn't say "intersection point").

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking.


// Change the normal from object space to world space
auto normal = rec.normal;
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
rec.p = point3(
(cos_theta * rec.p.x()) + (sin_theta * rec.p.z()),
rec.p.y(),
(-sin_theta * rec.p.x()) + (cos_theta * rec.p.z())
);

rec.p = p;
rec.normal = normal;
rec.normal = vec3(
(cos_theta * rec.normal.x()) + (sin_theta * rec.normal.z()),
rec.normal.y(),
(-sin_theta * rec.normal.x()) + (cos_theta * rec.normal.z())
);

return true;
}
Expand Down
43 changes: 25 additions & 18 deletions src/TheNextWeek/hittable.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,41 @@ class rotate_y : public hittable {
}

bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
// Change the ray from world space to object space
auto origin = r.origin();
auto direction = r.direction();

origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
// Transform the ray from world space to object space.

direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
auto origin = point3(
(cos_theta * r.origin().x()) - (sin_theta * r.origin().z()),
r.origin().y(),
(sin_theta * r.origin().x()) + (cos_theta * r.origin().z())
);

auto direction = vec3(
(cos_theta * r.direction().x()) - (sin_theta * r.direction().z()),
r.direction().y(),
(sin_theta * r.direction().x()) + (cos_theta * r.direction().z())
);

ray rotated_r(origin, direction, r.time());

// Determine whether an intersection exists in object space (and if so, where)
// Determine whether an intersection exists in object space (and if so, where).

if (!object->hit(rotated_r, ray_t, rec))
return false;

// Change the intersection point from object space to world space
auto p = rec.p;
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
// Transform the intersection from object space back to world space.

// Change the normal from object space to world space
auto normal = rec.normal;
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
rec.p = point3(
(cos_theta * rec.p.x()) + (sin_theta * rec.p.z()),
rec.p.y(),
(-sin_theta * rec.p.x()) + (cos_theta * rec.p.z())
);

rec.p = p;
rec.normal = normal;
rec.normal = vec3(
(cos_theta * rec.normal.x()) + (sin_theta * rec.normal.z()),
rec.normal.y(),
(-sin_theta * rec.normal.x()) + (cos_theta * rec.normal.z())
);

return true;
}
Expand Down
43 changes: 25 additions & 18 deletions src/TheRestOfYourLife/hittable.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,34 +123,41 @@ class rotate_y : public hittable {
}

bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
// Change the ray from world space to object space
auto origin = r.origin();
auto direction = r.direction();

origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
// Transform the ray from world space to object space.

direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
auto origin = point3(
(cos_theta * r.origin().x()) - (sin_theta * r.origin().z()),
r.origin().y(),
(sin_theta * r.origin().x()) + (cos_theta * r.origin().z())
);

auto direction = vec3(
(cos_theta * r.direction().x()) - (sin_theta * r.direction().z()),
r.direction().y(),
(sin_theta * r.direction().x()) + (cos_theta * r.direction().z())
);

ray rotated_r(origin, direction, r.time());

// Determine whether an intersection exists in object space (and if so, where)
// Determine whether an intersection exists in object space (and if so, where).

if (!object->hit(rotated_r, ray_t, rec))
return false;

// Change the intersection point from object space to world space
auto p = rec.p;
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
// Transform the intersection from object space back to world space.

// Change the normal from object space to world space
auto normal = rec.normal;
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
rec.p = point3(
(cos_theta * rec.p.x()) + (sin_theta * rec.p.z()),
rec.p.y(),
(-sin_theta * rec.p.x()) + (cos_theta * rec.p.z())
);

rec.p = p;
rec.normal = normal;
rec.normal = vec3(
(cos_theta * rec.normal.x()) + (sin_theta * rec.normal.z()),
rec.normal.y(),
(-sin_theta * rec.normal.x()) + (cos_theta * rec.normal.z())
);

return true;
}
Expand Down