Basis Change
Sampling hemisphere directions around any normal vector using basis change.
Quick Revision
Intro
- We have some basis $B$ for a vector space in $\mathbb{R}^2$ with these basis vectors:
$$ B = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} $$
-
Where the left column is the first basis vector $b_1$ and the second is the second basis vector $b_2$.
-
We also have a vector $v$ which is portrayed in that basis as:
$$[v]_B = \begin{bmatrix} 3 \\ 4 \end{bmatrix}$$
- This means:
$$[v]_S = 3b_1 + 4b_2$$
- Where $S$ is the standard/canonical basis.
- So to convert from a basis $B$ to $S$:
$$B \cdot [v]_B= [v]_S$$
- To go from $S$ to $B$ instead:
$$B^{-1} \cdot [v]_S = [v]_B$$
- The matrix $B$ can also be written as $P_{B \rightarrow S}$.
- So, in general:
$$P_{A \rightarrow B} = (P_{B \rightarrow A})^{-1}$$
- And vice-versa.
Change of Basis from $B$ to $C$
- To go from basis $B$ to basis $C$, we can get from $B$ to $S$, then from $S$ to $C$.
- We make use of the fact that we can go to and from $S$ at any basis here.
- So:
$$P_{B \rightarrow C} = P_{S \rightarrow C} \cdot P_{B \rightarrow S}$$
- Read from right to left, since the vector we're transforming is ultimately put on the far right.
Orthonormal Basis
- An orthonormal basis (ONB) is one whose vectors are orthogonal and normalized (perpendicular and have a magnitude of 1)
- Which means their basis change matrix $P_{B \rightarrow S}$ is an orthogonal matrix.
- Orthogonal matrices are matrices whose columns and rows$^1$ are all orthonormal.
- They have a very useful property:
$$B^T = B^{-1}$$
-
This can be derived easily by treating matrix multiplication as dot products.
-
This property makes finding the inverse of a basis change matrix for an ONB really easy.
-
Also, the product of two orthogonal matrices is also orthogonal. This can be easily checked with the property we just mentioned:
$$(BC)^T(BC)=C^TB^TBC=C^T(I)C=C^TC=I$$
- So, if $B$ and $C$ are both ONBs, the change of basis matrix going from one to the other is an orthogonal matrix.
$1$: columns being orthonormal implies rows are orthonormal too, and vice versa.
Sampling From Any Normal
-
Back to raytracing.
-
We discussed how to sample random vectors in a hemisphere based on a joint PDF.
-
But we did this while assuming the normal was the positive $z$-axis.
-
Our ultimate goal is to sample for ANY possible normal, allowing us to reflect from anywhere we want.
-
Think of it like this: the $z$-axis is currently our normal, forming a full orthonormal basis with the $x$ and $y$ axes.
-
We can treat the new normal $n$ we are trying to orient the vectors around as the new $z$ axis, then create two other vectors $s$ and $t$ that form an ONB with it: $(s,t,n)$. Let's call it $B$.
-
If we form $B$, we can think of those directions we get as if they were written in terms of $B$, not the standard basis.
-
Then we would just need to convert them to standard form using the basis change matrix $P_{B \rightarrow S}$:
$$P_{B \rightarrow S} \cdot [\text{sampled}]_B = [\text{sampled}]_S$$
- We can also forget the basis change idea and just think of it as weighting those new axes $(s, t, n)$ by the local directions we sample.
- In game dev terms, we're basically just treating these points as if they were in the local space of that new normal vector $n$ as a trick, then finding their values global space through basis change.