diff --git a/exam-2/exam2.md b/exam-2/exam2.md index 2449b8f..933dbc1 100644 --- a/exam-2/exam2.md +++ b/exam-2/exam2.md @@ -12,6 +12,7 @@ author: | \renewcommand{\c}[1]{\textcolor{gray}{#1}} \newcommand{\now}[1]{\textcolor{blue}{#1}} +\newcommand{\todo}[0]{\textcolor{red}{\textbf{TODO}}} ## Reflection and Refraction @@ -99,31 +100,156 @@ author: | 4. \c{Consider the viewing transformation matrix $V$ that enables all of the vertices in a scene to be expressed in terms of a coordinate system in which the eye is located at $(0, 0, 0)$, the viewing direction ($-n$) is aligned - with the $-z$ axis $(0, 0, –1)$, and the camera's 'up' direction (which + with the $-z$ axis $(0, 0, -1)$, and the camera's 'up' direction (which controls the roll of the view) is aligned with the $y$ axis (0, 1, 0).} - a. (4 points) When the eye is located at $e = (2, 3, 5)$, the camera is + a. \c{(4 points) When the eye is located at $e = (2, 3, 5)$, the camera is pointing in the direction $(1, -1, -1)$, and the camera's 'up' direction is - $(0, 1, 0)$, what are the entries in $V$? + $(0, 1, 0)$, what are the entries in $V$?} - $$\begin{bmatrix} - 0 & 1 & 0 & d_x \\ - \end{bmatrix}$$ + First we can calculate $n$ and $u$: - b. (2 points) How will this matrix change if the eye moves forward in the + - Viewing direction is $(1, -1, -1)$. + - Normalized $n = (\frac{1}{\sqrt{3}}, -\frac{1}{\sqrt{3}}, -\frac{1}{\sqrt{3}})$. + - $u = up \times n = (-\frac{1}{\sqrt{2}}, 0, -\frac{1}{\sqrt{2}})$. + - $v = n \times u = (\frac{\sqrt{6}}{6}, \frac{\sqrt{6}}{3}, -\frac{\sqrt{6}}{6})$ + + $$ + \begin{bmatrix} + -\frac{1}{\sqrt{2}} & 0 & -\frac{1}{\sqrt{2}} & d_x \\ + 1 & -1 & -1 & d_y \\ + \end{bmatrix} + $$ + + \todo + + b. \c{(2 points) How will this matrix change if the eye moves forward in the direction of view? [which elements in V will stay the same? which elements - will change and in what way?] + will change and in what way?]} - c. (2 points) How will this matrix change if the viewing direction spins in - the clockwise direction around the camera's 'up' direction? [which elements - in V will stay the same? which elements will change and in what way?] + If the eye moves forward, the eye _position_ and everything that depends on it + will change, while everything else doesn't. - d. (2 points) How will this matrix change if the viewing direction rotates + | $n$ | $u$ | $v$ | $d$ | + | ---- | ---- | ---- | --------- | + | same | same | same | different | + + The $n$ is the same because the viewing direction does not change. + + c. \c{(2 points) How will this matrix change if the viewing direction spins + in the clockwise direction around the camera's 'up' direction? [which + elements in V will stay the same? which elements will change and in what + way?]} + + In this case, the eye _position_ stays the same, and everything else changes. + + | $n$ | $u$ | $v$ | $d$ | + | --------- | --------- | --------- | ---- | + | different | different | different | same | + + d. \c{(2 points) How will this matrix change if the viewing direction rotates directly upward, within the plane defined by the viewing and 'up' directions? [which elements in V will stay the same? which elements will change and in - what way?] + what way?]} -5. + In this case, the eye _position_ stays the same, and everything else changes. + + | $n$ | $u$ | $v$ | $d$ | + | --------- | --------- | --------- | ---- | + | different | different | different | same | + +5. \c{Suppose a viewer located at the point $(0, 0, 0)$ is looking in the $-z$ + direction, with no roll ['up' = $(0, 1 ,0)$], towards a cube of width 2, + centered at the point $(0, 0, -5)$, whose sides are colored: red at the plane + $x = 1$, cyan at the plane $x = -1$, green at the plane $y = 1$, magenta at + the plane $y = -1$, blue at the plane $z = -4$, and yellow at the plane $z = + -6$.} + + a. \c{(1 point) What is the color of the cube face that the user sees?} + + \boxed{\textrm{Blue}} + + b. \c{(3 points) Because the eye is at the origin, looking down the $-z$ axis + with 'up' = $(0,1,0)$, the viewing transformation matrix $V$ in this case is + the identity $I$. What is the model matrix $M$ that you could use to rotate + the cube so that when the image is rendered, it shows the red side of the + cube?} + + You would have to do a combination of (1) translate to the origin, (2) rotate + around the origin, and then (3) untranslate back. This way, the eye position + doesn't change. + + $$ + M = + \begin{bmatrix} + 1 & 0 & 0 & 0 \\ + 0 & 1 & 0 & 0 \\ + 0 & 0 & 1 & -5 \\ + 0 & 0 & 0 & 1 \\ + \end{bmatrix} \cdot + \begin{bmatrix} + 0 & 0 & -1 & 0 \\ + 0 & 1 & 0 & 0 \\ + 1 & 0 & 0 & 0 \\ + 0 & 0 & 0 & 1 \\ + \end{bmatrix} \cdot + \begin{bmatrix} + 1 & 0 & 0 & 0 \\ + 0 & 1 & 0 & 0 \\ + 0 & 0 & 1 & 5 \\ + 0 & 0 & 0 & 1 \\ + \end{bmatrix} + = + \boxed{\begin{bmatrix} + 0 & 0 & -1 & -5 \\ + 0 & 1 & 0 & 0 \\ + 1 & 0 & 0 & -5 \\ + 0 & 0 & 0 & 1 \\ + \end{bmatrix}} + $$ + + To verify this, testing with an example point $(1, 1, -4)$ yields: + + $$ + \begin{bmatrix} + 0 & 0 & -1 & -5 \\ + 0 & 1 & 0 & 0 \\ + 1 & 0 & 0 & -5 \\ + 0 & 0 & 0 & 1 \\ + \end{bmatrix} + \cdot + \begin{bmatrix} + 1 \\ 1 \\ -4 \\ 1 + \end{bmatrix} + = + \begin{bmatrix} + -1 \\ 1 \\ -4 \\ 1 + \end{bmatrix} + $$ + + c. \c{(4 points) Suppose now that you want to leave the model matrix $M$ as + the identity. What is the viewing matrix $V$ that you would need to use to + render an image of the scene from a re-defined camera configuration so that + when the scene is rendered, it shows the red side of the cube? Where is the + eye in this case and in what direction is the camera looking?} + + For this, a different eye position will have to be used. Instead of looking + from the origin, you could view it from the red side, and then change the + direction so it's still pointing at the cube. + + - eye is located at $(5, 0, -5)$ + - viewing direction is $(-1, 0, 0)$ + - $n = (1, 0, 0)$ + - $u = up \times n = (0, 0, -1)$ + - $v = n \times u = (0, 1, 0)$ + - $d = (-5, 0, -5)$ + + The final viewing matrix is $\boxed{\begin{bmatrix} + 0 & 0 & -1 & -5 \\ + 0 & 1 & 0 & 0 \\ + 1 & 0 & 0 & -5 \\ + 0 & 0 & 0 & 1 \\ + \end{bmatrix}}$. Turns out it's the same matrix! Wow! ## The Projection Transformation @@ -147,32 +273,36 @@ author: | \tan(60^\circ) \times 0.5 = \boxed{\frac{\sqrt{3}}{2}}$. The same goes for the vertical, which also yields $\frac{\sqrt{3}}{2}$. - $$\begin{bmatrix} + $$ + \begin{bmatrix} \frac{2\times near}{right - left} & 0 & \frac{right + left}{right - left} & 0 \\ 0 & \frac{2\times near}{top - bottom} & \frac{top + bottom}{top - bottom} & 0 \\ 0 & 0 & -\frac{far + near}{far - near} & -\frac{2\times far\times near}{far - near} \\ 0 & 0 & -1 & 0 - \end{bmatrix}$$ + \end{bmatrix} + $$ - $$= \begin{bmatrix} + $$ + = \begin{bmatrix} \frac{2\times 0.5}{\frac{\sqrt{3}}{2} - (-\frac{\sqrt{3}}{2})} & 0 & \frac{\frac{\sqrt{3}}{2} + (-\frac{\sqrt{3}}{2})}{\frac{\sqrt{3}}{2} - (-\frac{\sqrt{3}}{2})} & 0 \\ 0 & \frac{2\times 0.5}{\frac{\sqrt{3}}{2} - (-\frac{\sqrt{3}}{2})} & \frac{\frac{\sqrt{3}}{2} + (-\frac{\sqrt{3}}{2})}{\frac{\sqrt{3}}{2} - (-\frac{\sqrt{3}}{2})} & 0 \\ 0 & 0 & -\frac{20 + 0.5}{20 - 0.5} & -\frac{2\times 20\times 0.5}{20 - 0.5} \\ 0 & 0 & -1 & 0 - \end{bmatrix}$$ + \end{bmatrix} + $$ - $$= \boxed{\begin{bmatrix} + $$ + = \boxed{\begin{bmatrix} \frac{1}{\sqrt{3}} & 0 & 0 & 0 \\ 0 & \frac{1}{\sqrt{3}} & 0 & 0 \\ 0 & 0 & -\frac{41}{39} & -\frac{40}{39} \\ 0 & 0 & -1 & 0 - \end{bmatrix}}$$ + \end{bmatrix}} + $$ b. \c{(3 points) How should be matrix $P$ be re-defined if the viewing window is re-sized to be twice as tall as it is wide?} - - c. \c{(3 points) What are the new horizontal and vertical fields of view after this change has been made?} diff --git a/exam-2/exam2.py b/exam-2/exam2.py index 6f93990..ef695fd 100644 --- a/exam-2/exam2.py +++ b/exam-2/exam2.py @@ -37,6 +37,62 @@ def problem_1(): def problem_4(): print("part 4a.") + up = np.array([0, 1, 0]) + viewing_dir = np.array([1, -1, -1]) + n = unit(viewing_dir) + print(f"{n = }") + + u = unit(np.cross(up, n)) + print(f"{u = }") + + v = np.cross(n, u) + print(f"{v = }") + + print(math.sqrt(1 / 6.0)) + print(math.sqrt(2 / 3.0)) + +def build_translation_matrix(vec): + return np.array([ + [1, 0, 0, vec[0]], + [0, 1, 0, vec[1]], + [0, 0, 1, vec[2]], + [0, 0, 0, 1], + ]) + +def problem_5(): + b1 = build_translation_matrix(np.array([0, 0, 5])) + theta = math.radians(-90) + sin_theta = round( math.sin(theta), 5) + cos_theta = round(math.cos(theta), 5) + b2 = np.array([ + [cos_theta, 0, sin_theta, 0], + [0, 1, 0, 0], + [-sin_theta, 0, cos_theta, 0], + [0, 0, 0, 1], + ]) + b3 = build_translation_matrix(np.array([0, 0, -5])) + + print("b1", b1) + print("b2", b2) + print("b3", b3) + M = b3 @ b2 @ b1 + print("M", M) + + ex1 = np.array([1, 1, -4, 1]) + print("ex1", ex1, M @ ex1) + + up = np.array([0, 1, 0]) + n = np.array([1, 0, 0]) + u = unit(np.cross(up, n)) + v = np.cross(n, u) + + print(f"{up = }, {n = }, {u = }, {v = }") + + eye = np.array([5, 0, -5]) + dx = -(np.dot(eye, u)) + dy = -(np.dot(eye, v)) + dz = -(np.dot(eye, n)) + print(f"{dx = }, {dy = }, {dz = }") def problem_8(): def P(left, right, bottom, top, near, far): @@ -67,3 +123,4 @@ def problem_8(): print("\nPROBLEM 4 -------------------------"); problem_4() print("\nPROBLEM 8 -------------------------"); problem_8() print("\nPROBLEM 1 -------------------------"); problem_1() +print("\nPROBLEM 5 -------------------------"); problem_5()