Skip to content

Book 1: Chapter 6.1: Is this algorithm correct?  #1558

@artogahr

Description

@artogahr

I'm trying to rewrite this book in Rust, and as far as I can tell, I've done everything 1:1 with the current published book (4.0.0-alpha.2, 2024-04-07)

As I can tell, within the older versions these lines:

// Calculate the location of the upper left pixel.
auto viewport_upper_left = camera_center
- vec3(0, 0, focal_length) - viewport_u/2 - viewport_v/2;
auto pixel00_loc = viewport_upper_left + 0.5 * (pixel_delta_u + pixel_delta_v);

Were represented as just a calculation for lower_left_corner.
Implementing the old version from reference implementations works. But the newer version, doesn't produce the colorful sphere.

Within my code, if I add an offset to the return value of the hit_sphere function, the sphere with the Vectors is reproduced with correct colors, i.e. instead of:

let t = hit_sphere(Point3::new(0.0, 0.0, -1.0), 0.5, r);
    if t > 0.0 {
        let n: Vec3 = (r.at(t) - Vec3::new(0.0, 0.0, -1.0)).normalized();
        return 0.5 * Color::new(n.x() + 1.0, n.y() + 1.0, n.z() + 1.0);
    }

doing

let t = hit_sphere(Point3::new(0.0, 0.0, -1.0), 0.5, r);
    if t > -0.5 {
        let n: Vec3 = (r.at(t) - Vec3::new(0.0, 0.0, -1.0)).normalized();
        return 0.5 * Color::new(n.x() + 1.0, n.y() + 1.0, n.z() + 1.0);
    }

fixes the issue and the exact image from the book is produced. Values between -0.5 and -0.27 produces different width circles. I'm not mathematically versed enough to understand why, but I thought this could help us diagnose this issue.

Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions