|
2346 | 2346 |
|
2347 | 2347 | On the refracted side of the surface there is a refracted ray $\mathbf{R'}$ and a normal
|
2348 | 2348 | $\mathbf{n'}$, and there exists an angle, $\theta'$, between them. We can split $\mathbf{R'}$ into
|
2349 |
| -the parts of the ray that are parallel to $\mathbf{n'}$ and perpendicular to $\mathbf{n'}$: |
| 2349 | +the parts of the ray that are perpendicular to $\mathbf{n'}$ and parallel to $\mathbf{n'}$: |
2350 | 2350 |
|
2351 |
| - $$ \mathbf{R'} = \mathbf{R'}_{\parallel} + \mathbf{R'}_{\bot} $$ |
| 2351 | + $$ \mathbf{R'} = \mathbf{R'}_{\bot} + \mathbf{R'}_{\parallel} $$ |
2352 | 2352 |
|
2353 |
| -If we solve for $\mathbf{R'}_{\parallel}$ and $\mathbf{R'}_{\bot}$ we get: |
| 2353 | +If we solve for $\mathbf{R'}_{\bot}$ and $\mathbf{R'}_{\parallel}$ we get: |
2354 | 2354 |
|
2355 |
| - $$ \mathbf{R'}_{\parallel} = \frac{\eta}{\eta'} (\mathbf{R} + \cos\theta \mathbf{n}) $$ |
2356 |
| - $$ \mathbf{R'}_{\bot} = -\sqrt{1 - |\mathbf{R'}_{\parallel}|^2} \mathbf{n} $$ |
| 2355 | + $$ \mathbf{R'}_{\bot} = \frac{\eta}{\eta'} (\mathbf{R} + \cos\theta \mathbf{n}) $$ |
| 2356 | + $$ \mathbf{R'}_{\parallel} = -\sqrt{1 - |\mathbf{R'}_{\bot}|^2} \mathbf{n} $$ |
2357 | 2357 |
|
2358 | 2358 | You can go ahead and prove this for yourself if you want, but we will treat it as fact and move on.
|
2359 | 2359 | The rest of the book will not require you to understand the proof.
|
|
2367 | 2367 |
|
2368 | 2368 | $$ \mathbf{a} \cdot \mathbf{b} = \cos\theta $$
|
2369 | 2369 |
|
2370 |
| -We can now rewrite $\mathbf{R'}_{\parallel}$ in terms of known quantities: |
| 2370 | +We can now rewrite $\mathbf{R'}_{\bot}$ in terms of known quantities: |
2371 | 2371 |
|
2372 |
| - $$ \mathbf{R'}_{\parallel} = |
| 2372 | + $$ \mathbf{R'}_{\bot} = |
2373 | 2373 | \frac{\eta}{\eta'} (\mathbf{R} + (\mathbf{-R} \cdot \mathbf{n}) \mathbf{n}) $$
|
2374 | 2374 |
|
2375 | 2375 | When we combine them back together, we can write a function to calculate $\mathbf{R'}$:
|
2376 | 2376 |
|
2377 | 2377 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2378 | 2378 | vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) {
|
2379 | 2379 | auto cos_theta = dot(-uv, n);
|
2380 |
| - vec3 r_out_parallel = etai_over_etat * (uv + cos_theta*n); |
2381 |
| - vec3 r_out_perp = -sqrt(fabs(1.0 - r_out_parallel.length_squared())) * n; |
2382 |
| - return r_out_parallel + r_out_perp; |
| 2380 | + vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n); |
| 2381 | + vec3 r_out_parallel = -sqrt(fabs(1.0 - r_out_perp.length_squared())) * n; |
| 2382 | + return r_out_perp + r_out_parallel; |
2383 | 2383 | }
|
2384 | 2384 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2385 | 2385 | [Listing [refract]: <kbd>[vec3.h]</kbd> Refraction function]
|
|
0 commit comments