Chapter 20 - Code that I do not understand


This is maybe due to fatigue but I do not understand the code of the REFRACTION part of the playground.

To be more specific: it is linked with this specific block of code

// 1
else if (closestObject == SphereObj) {
   inside = !inside;
// 2
  float ior = inside ? 1.0 / 1.33 : 1.33;
  cam = refractRay(cam, normal, eps, ior);

I tested and see the impact of replacing

inside = !inside;


inside = true;

Which I would be confortable with. I see it does not work. But I cannot figure out why.
I think it is linked to my inaccurate understanding of ray marching.

Indeed, for me the ray is “stopped” when in contact between the ray and the object is triggered.
So why can we switch and memorize inside for further I would feel loops.

You can see. I am confused with this part.

Do you have any Idea of which (I hope small) part of the book I should read to try to have a better understanding of this part…

Thank you and take care.

inside = !inside acts like a switch “if on, turn off. if off, turn on”.

Thanks but this is not the answer I seek.

I know what is a switch code pattern.
My concern is about WHY is this one relevant here.


because when the ray is outside the sphere it will enter it when refracting. when it’s already inside it will exit the sphere while still refracting. since you are tracing this ray, it’s the easiest way to see where it is at any given time, by switching the inside variable. makes sense?