How transform works in OptiX 7 hit functions?


I have been following OptiX7Courses:

I do not understand how transform works in the code below:

extern "C" __global__ void __closesthit__radiance()
    const TriangleMeshSBTData &sbtData
      = *(const TriangleMeshSBTData*)optixGetSbtDataPointer();

    // compute normal:
    const int   primID = optixGetPrimitiveIndex();
    const vec3i index  = sbtData.index[primID];
    const vec3f &A     = sbtData.vertex[index.x];
    const vec3f &B     = sbtData.vertex[index.y];
    const vec3f &C     = sbtData.vertex[index.z];
    const vec3f Ng     = normalize(cross(B-A,C-A));

    const vec3f rayDir = optixGetWorldRayDirection();
    const float cosDN  = 0.2f + .8f*fabsf(dot(rayDir,Ng));
    vec3f &prd = *(vec3f*)getPRD<vec3f>();
    prd = cosDN * sbtData.color;

rayDir is in world space, as it is got from optixGetWorldRayDirection().
However, the normal Ng is from local space, as it is computed by using directly sbtData.vertex.

If the local space is same as world space, I understand this code works.
However, if I have instances with transformations, does this code still work? If not, any suggested modifications?

I think you have 2 ways:

  1. Manually supply instance transform matrix in each hitgroup record.
  2. Directly use device-side functions:(see more detials in OptiX programming guide)
    • optixTransformVectorFromObjectToWorldSpace
    • optixTransformNormalFromObjectToWorldSpace