Generated by a local coordinate system normal

In doing ray tracing, when there is a normal step in the known planar light rays hit, and the incident direction of light. We want to convert the local coordinate normal to the direction of incidence, which must first be built based on a local coordinate system is normal. Then use this article ( https://www.cnblogs.com/wickedpriest/p/12074420.html ) to convert.
So now the question is: Given normal as the z-axis of the local coordinate system, how to build x, y-axis to form a new local coordinate system do?
Graphics website scratch_pixel mentioned a method, the code is posted here:

    XMVECTOR Nt, Nb, N;
    N = XMVectorSet(n.x, n.y, n.z, 0.0);
    if (std::fabs(n.x) > std::fabs(n.y))
        Nt = XMVector3Normalize(XMVectorSet(n.z, 0, -n.x, 0.0));
    else
        Nt = XMVector3Normalize(XMVectorSet(0.0, -n.z, n.y, 0.0));
    Nb = XMVector3Cross(N, Nt);

Where N is the normal, Nt and Nb is our other two axes calculated. I started to see the section if the code also froze a moment, I did not quite get to know motives for doing so. A moment realized when a counted Nt, N perpendicular to each other and in order to ensure, to obtain the vector N is the number of two, and wherein a negated number, consisting of a new vector (this ensures that the N and Nt dot product is zero). But there is one possibility is to take the two numbers are all 0, then the resulting value Nt will be all zeros. To avoid this, Nt N in the need to include the largest absolute value (the maximum value of the absolute value may not be 0).

Guess you like

Origin www.cnblogs.com/wickedpriest/p/12098582.html