Skip to content

Incorrect linear interpolation in Chapter 4.2 #1652

Open
@KDJDEV

Description

@KDJDEV

Chapter 4.2 describes linear interpolation as some function that outputs a color when given a value a that ranges from 0 to 1. However, the ray_color function does not make a range from 0 to 1.

color ray_color(const ray& r) {
    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);
}

Because the code runs unit_vector on r.direction(), the y value no longer ranges from 1 to -1, and therefore a does not range from 0 to 1. If you just don't make unit_direction into a unit vector then it works as expected.

This is what I think the code should be:

color ray_color(const ray& r) {
    vec3 unit_direction = 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);
}

The current code still does an interpolation between a sort of white and a sort of blue, but it does not perform a linear interpolation between color(1.0, 1.0, 1.0) and color(0.5, 0.7, 1.0).

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions