|
386 | 386 | -------------- |
387 | 387 | <div class='together'> |
388 | 388 | The one thing that all ray tracers have is a ray class, and a computation of what color is seen |
389 | | -along a ray. Let’s think of a ray as a function $\mathbf{P}(t) = \mathbf{A} + t \mathbf{b}$. (Note: |
390 | | -throughout these books, we'll use uppercase bold letters for points, and lowercase bold letters for |
391 | | -vectors.) Here $\mathbf{P}$ is a 3D position along a line in 3D. $\mathbf{A}$ is the ray origin and |
392 | | -$\mathbf{b}$ is the ray direction. The ray parameter $t$ is a real number (`double` in the |
393 | | -code). Plug in a different $t$ and $\mathbf{P}(t)$ moves the point along the ray. Add in negative |
394 | | -$t$ and you can go anywhere on the 3D line. For positive $t$, you get only the parts in front of |
395 | | -$\mathbf{A}$, and this is what is often called a half-line or ray. |
| 389 | +along a ray. Let’s think of a ray as a function $\mathbf{P}(t) = \mathbf{A} + t \mathbf{b}$. Here |
| 390 | +$\mathbf{P}$ is a 3D position along a line in 3D. $\mathbf{A}$ is the ray origin and $\mathbf{b}$ is |
| 391 | +the ray direction. The ray parameter $t$ is a real number (`double` in the code). Plug in a |
| 392 | +different $t$ and $\mathbf{P}(t)$ moves the point along the ray. Add in negative $t$ and you can go |
| 393 | +anywhere on the 3D line. For positive $t$, you get only the parts in front of $\mathbf{A}$, and this |
| 394 | +is what is often called a half-line or ray. |
396 | 395 |
|
397 | 396 | ![Figure [lerp]: Linear interpolation](../images/fig.lerp.jpg) |
398 | 397 |
|
|
537 | 536 | the given point $(x,y,z)$ is _inside_ the sphere, then $x^2 + y^2 + z^2 < R^2$, and if a given point |
538 | 537 | $(x,y,z)$ is _outside_ the sphere, then $x^2 + y^2 + z^2 > R^2$. |
539 | 538 |
|
540 | | -It gets uglier if the sphere center is at $(\mathbf{C}_x, \mathbf{C}_y, \mathbf{C}_z)$: |
| 539 | +It gets uglier if the sphere center is at $(C_x, C_y, C_z)$: |
541 | 540 |
|
542 | | - $$ (x-\mathbf{C}_x)^2 + (y-\mathbf{C}_y)^2 + (z-\mathbf{C}_z)^2 = r^2 $$ |
| 541 | + $$ (x - C_x)^2 + (y - C_y)^2 + (z - C_z)^2 = r^2 $$ |
543 | 542 | </div> |
544 | 543 |
|
545 | 544 | <div class='together'> |
546 | 545 | In graphics, you almost always want your formulas to be in terms of vectors so all the x/y/z stuff |
547 | 546 | is under the hood in the `vec3` class. You might note that the vector from center |
548 | | -$\mathbf{C} = (\mathbf{C}_x,\mathbf{C}_y,\mathbf{C}_z)$ to point $\mathbf{P} = (x,y,z)$ is |
549 | | -$(\mathbf{P} - \mathbf{C})$, and therefore |
| 547 | +$\mathbf{C} = (C_x,C_y,C_z)$ to point $\mathbf{P} = (x,y,z)$ is $(\mathbf{P} - \mathbf{C})$, and |
| 548 | +therefore |
550 | 549 |
|
551 | 550 | $$ (\mathbf{P} - \mathbf{C}) \cdot (\mathbf{P} - \mathbf{C}) |
552 | | - = (x-\mathbf{C}_x)^2 + (y-\mathbf{C}_y)^2 + (z-\mathbf{C}_z)^2 |
| 551 | + = (x - C_x)^2 + (y - C_y)^2 + (z - C_z)^2 |
553 | 552 | $$ |
554 | 553 | </div> |
555 | 554 |
|
|
565 | 564 | does hit the sphere, there is some $t$ for which $\mathbf{P}(t)$ satisfies the sphere equation. So |
566 | 565 | we are looking for any $t$ where this is true: |
567 | 566 |
|
568 | | - $$ (\mathbf{P}(t) - \mathbf{C})\cdot(\mathbf{P}(t) - \mathbf{C}) = r^2 $$ |
| 567 | + $$ (\mathbf{P}(t) - \mathbf{C}) \cdot (\mathbf{P}(t) - \mathbf{C}) = r^2 $$ |
569 | 568 |
|
570 | 569 | or expanding the full form of the ray $\mathbf{P}(t)$: |
571 | 570 |
|
|
0 commit comments