1

I am working on ray tracing, and decided to use bounding boxes( axis aligned bbox) as objects (cubes), and shade them. I am able to find the correct t value, and intersection point; however, I could not find a way to calculate the surface normal since I only have ray direction, ray origin, intersection point, and t value, and min-max values of the bbox.

Is there a way to calculate the normal at the intersection point (or deciding which face of the cube ray intersected) with the information I have?

I am using "An Efficientand Robust Ray–Box Intersection Algorithm" by Williams et al.

ciyo
  • 725
  • 4
  • 16
  • 36

1 Answers1

-1

If you have the intersection point and the AABB (BoundingBox) center, you can make a quick calcul to obtain an index corresponding to the face you hit.

Then with an array that stores normals you can get your data.

Vector3 ComputeNormal(Vector3 inter, Vector3 aabbCenter)
{
    static const Vector3 normals[] = { // A cube has 3 possible orientations
        Vector3(1,0,0),
        Vector3(0,1,0),
        Vector3(0,0,1)
    };
    const Vector3 interRelative = inter - aabbCenter;
    const float xyCoef = interRelative.y / interRelative.x;
    const float zyCoef = interRelative.y / interRelative.z;

    const int coef = (isBetweenInclusive<1,-1>(xyCoef) ? 1 :
                      (isBetweenExclusive<1,-1>(zyCoef) ? 2 : 0));
    // Here it's exclusive to avoid coef to be 3
    return normals[coef] * SIGN(interRelative); // The sign he is used to know direction of the normal
}

I have not tested it so don't be surprised if it does not work directly ;) but it should do the trick.

Seb Maire
  • 132
  • 8