### Post by tracer on Jan 18, 2023 2:15:12 GMT

Hi everyone,

I've been following along with the book very easily until I reached refraction.

My Refraction is not working properly, and I've been trying to find the issue for 6 hours, and I still don't see what is the error.

Problems:

- All my tests from Chapter 11 are passing except the Test #7: Finding the Refracted Color
- Even though my schlick function pass the tests, it is returning weird values, like 25, 30, etc when rendering a real scene.

I believe the 2 problems are independent from each other, but I'm not a 100% sure. In this post I'm focusing on the second issue.

When I try to render my scene, this is what I see:

I was investigating, and it looks like that even though my schlick function is passing my tests, when I'm rendering a scene it returns weird values like 30, 25, and such.

Is that normal? I think no, because that will cause my color addition

**((refraction * (1.0 - reflectance)))**to become a "negative color", and that is causing the black/white circles.(when I'm transforming the colors from the 0.0-1.0 range to 0-255 for display, I'm clamping the values. Anything below 0 becomes 0, anything above 255 becomes 255. I believe this is the artifact we see, and it's caused by the schlick return value, but again, not sure)

However, I reviewed my function 100s of times, and I don't see the error:

`Color RayTracerRenderer::colorAt(const Ray &ray, const int remaining) const {`

auto intersections = m_world->intersect(ray);

auto hit = intersections.hit();

if (hit.has_value()) {

auto computations = prepareComputations(hit.value(), intersections, ray);

Color finalColor { 0, 0, 0, 1 };

for (auto &light: m_world->getLights()) {

finalColor += lighting(light, computations);

}

auto reflection = reflectedColor(computations, remaining);

auto refraction = refractedColor(computations, remaining);

auto material = computations.object->getMaterial();

if (material.reflectivity > 0 && material.transparency > 0) {

auto reflectance = schlick(computations);

return finalColor + (reflection * reflectance) + (refraction * (1.0 - reflectance));

} else {

return finalColor + reflection + refraction;

}

} else {

return Color::black();

}

}

`double RayTracerRenderer::schlick(const Computations &comps) const {`

auto angle = glm::dot(comps.eye, comps.normal);

if (comps.n1 > comps.n2) {

auto n = comps.n1 / comps.n2;

auto sin2T = pow(n, 2) * (1.0 - pow(angle, 2));

if (sin2T > 1.0) {

return 1.0;

}

auto cosT = sqrt(1.0 - sin2T);

angle = cosT;

}

double r0 = pow((comps.n1 - comps.n2) / (comps.n1 + comps.n2), 2.0);

auto result = r0 + (1.0 - r0) * pow((1.0 - angle), 5.0);

return result;

}

Reflections are seemingly working fine, and the part that is causing the problems is:

**(refraction * (1.0 - reflectance))**;(If I remove that part, I don't see artifacts).

Any ideas what can be the problem?