electrosoup.com

git clone git://git.electrosoup.com/electrosoup.com
Log | Files | Refs

index.xml (6445B)


      1 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
      2 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
      3   <channel>
      4     <title>Matrices on Electrosoup</title>
      5     <link>https://electrosoup.com/tags/matrices/</link>
      6     <description>Recent content in Matrices on Electrosoup</description>
      7     <generator>Hugo -- gohugo.io</generator>
      8     <language>en-US</language>
      9     <managingEditor>christianermann@gmail.com (Christian Ermann)</managingEditor>
     10     <webMaster>christianermann@gmail.com (Christian Ermann)</webMaster>
     11     <lastBuildDate>Mon, 10 Oct 2022 00:31:00 -0400</lastBuildDate>
     12     <atom:link href="https://electrosoup.com/tags/matrices/index.xml" rel="self" type="application/rss+xml" />
     13     <item>
     14       <title>Understanding Camera Look-At.</title>
     15       <link>https://electrosoup.com/posts/look-at/</link>
     16       <pubDate>Mon, 10 Oct 2022 00:31:00 -0400</pubDate><author>christianermann@gmail.com (Christian Ermann)</author>
     17       <guid>https://electrosoup.com/posts/look-at/</guid>
     18       <description>&lt;p&gt;Often times, we want to position the camera in our scene at a particular point,&#xA;such as the head of the character in a first-person shooter or the cockpit of a&#xA;plane in a flight simulator. We also want the camera to look in the direction&#xA;that the player is trying to move.&lt;/p&gt;</description>
     19       <content:encoded><![CDATA[<p>Often times, we want to position the camera in our scene at a particular point,
     20 such as the head of the character in a first-person shooter or the cockpit of a
     21 plane in a flight simulator. We also want the camera to look in the direction
     22 that the player is trying to move.</p>
     23 <p>To do this, we need to find the forward, right, and up vectors that correspond
     24 to the desired view.</p>
     25 <p>If we let $\vec{p}$ be the camera&rsquo;s position and $\vec{t}$ be the position of
     26 what we want to look at, the camera&rsquo;s look direction is
     27 $$\vec{l}=\vec{t}-\vec{p}\,.$$
     28 Conventionally, the camera looks down the negative forward axis so
     29 $$\vec{f}=-\vec{l}\,.$$</p>
     30 <p>In order to determine the right vector, we need a reference up direction,
     31 $\vec{up}$ (which isn&rsquo;t necessarily orthogonal to $\vec{f}$). This enables us
     32 to calculate
     33 $$\vec{r} = \vec{f} \times \vec{up}$$
     34 and
     35 $$\vec{u} = \vec{r} \times \vec{f}\,.$$</p>
     36 <p>Now we can use the camera position $\vec{p}$ and normalized forms of $\vec{f}$,
     37 $\vec{r}$, and $\vec{u}$ to fill in our
     38 <a href="/posts/view-matrix/">view matrix</a>:</p>
     39 <p>$$
     40 V
     41 =
     42 \begin{bmatrix}
     43 r_{x} &amp; r_{y} &amp; r_{z} &amp; -(\vec{r} \cdot \vec{p}) \\
     44 u_{x} &amp; u_{y} &amp; u_{z} &amp; -(\vec{u} \cdot \vec{p}) \\
     45 f_{x} &amp; f_{y} &amp; f_{z} &amp; -(\vec{f} \cdot \vec{p}) \\
     46 0     &amp; 0     &amp; 0     &amp; 1
     47 \end{bmatrix}
     48 $$</p>
     49 ]]></content:encoded>
     50     </item>
     51     <item>
     52       <title>Deriving the View Matrix</title>
     53       <link>https://electrosoup.com/posts/view-matrix/</link>
     54       <pubDate>Sun, 21 Nov 2021 13:34:08 -0800</pubDate><author>christianermann@gmail.com (Christian Ermann)</author>
     55       <guid>https://electrosoup.com/posts/view-matrix/</guid>
     56       <description>&lt;p&gt;In computer graphics, the view matrix is used to transform points from&#xA;world space into camera space. Below is an explanation of how to derive this&#xA;matrix.&lt;/p&gt;</description>
     57       <content:encoded><![CDATA[<p>In computer graphics, the view matrix is used to transform points from
     58 world space into camera space. Below is an explanation of how to derive this
     59 matrix.</p>
     60 <p>Let $V$ be the view matrix.
     61 If $\vec{r}$, $\vec{u}$, and $\vec{f}$ denote the right, up, and forward
     62 vectors of the camera and $\vec{p}$ is the camera&rsquo;s position, $V$ should
     63 satisfy the following properties:
     64 $$
     65 \begin{matrix}
     66 V_{1*} \times
     67 \begin{bmatrix}
     68 r_{x} \\
     69 r_{y} \\
     70 r_{z} \\
     71 0
     72 \end{bmatrix}
     73 =
     74 \begin{bmatrix}
     75 1 \\
     76 0 \\
     77 0 \\
     78 0
     79 \end{bmatrix},&amp;
     80 V_{2*} \times
     81 \begin{bmatrix}
     82 u_{x} \\
     83 u_{y} \\
     84 u_{z} \\
     85 0
     86 \end{bmatrix}
     87 =
     88 \begin{bmatrix}
     89 0 \\
     90 1 \\
     91 0 \\
     92 0
     93 \end{bmatrix},&amp;
     94 V_{3*} \times
     95 \begin{bmatrix}
     96 f_{x} \\
     97 f_{y} \\
     98 f_{z} \\
     99 0
    100 \end{bmatrix}
    101 =
    102 \begin{bmatrix}
    103 0 \\
    104 0 \\
    105 1 \\
    106 0
    107 \end{bmatrix},&amp;
    108 V_{4*} \times
    109 \begin{bmatrix}
    110 p_{x} \\
    111 p_{y} \\
    112 p_{z} \\
    113 1
    114 \end{bmatrix}
    115 =
    116 \begin{bmatrix}
    117 0 \\
    118 0 \\
    119 0 \\
    120 1
    121 \end{bmatrix}
    122 \end{matrix}
    123 $$
    124 In words, the view matrix should translate the camera to the origin and rotate
    125 it to align with the $\vec{x}$, $\vec{y}$, and $\vec{z}$ axes.</p>
    126 <p>This can be expressed more concisely as the product of two matrices:
    127 $$
    128 V \times
    129 \begin{bmatrix}
    130 r_{x} &amp; u_{x} &amp; f_{x} &amp; p_{x} \\
    131 r_{y} &amp; u_{y} &amp; f_{y} &amp; p_{y} \\
    132 r_{z} &amp; u_{z} &amp; f_{z} &amp; p_{z} \\
    133 0     &amp; 0     &amp; 0     &amp; 1
    134 \end{bmatrix}
    135 =
    136 \begin{bmatrix}
    137 1 &amp; 0 &amp; 0 &amp; 0 \\
    138 0 &amp; 1 &amp; 0 &amp; 0 \\
    139 0 &amp; 0 &amp; 1 &amp; 0 \\
    140 0 &amp; 0 &amp; 0 &amp; 1
    141 \end{bmatrix}
    142 $$</p>
    143 <p>To make this equation easier to solve, we can split the matrix containing our
    144 camera parameters into the product of a translation matrix $T$ and a rotation
    145 matrix $R$:
    146 $$
    147 V \times (T \times R) = I
    148 $$
    149 where
    150 $$
    151 \begin{matrix}
    152 T =
    153 \begin{bmatrix}
    154 1 &amp; 0 &amp; 0 &amp; p_{x} \\
    155 0 &amp; 1 &amp; 0 &amp; p_{y} \\
    156 0 &amp; 0 &amp; 1 &amp; p_{z} \\
    157 0 &amp; 0 &amp; 0 &amp; 1
    158 \end{bmatrix},&amp;
    159 R =
    160 \begin{bmatrix}
    161 r_{x} &amp; u_{x} &amp; f_{x} &amp; 0 \\
    162 r_{y} &amp; u_{y} &amp; f_{y} &amp; 0 \\
    163 r_{z} &amp; u_{z} &amp; f_{z} &amp; 0 \\
    164 0     &amp; 0     &amp; 0     &amp; 1
    165 \end{bmatrix}
    166 \end{matrix}
    167 $$</p>
    168 <p>Solving for $V$ reveals that $V=R^{-1} \times T^{-1}$:
    169 $$
    170 V =
    171 \begin{bmatrix}
    172 r_{x} &amp; r_{y} &amp; r_{z} &amp; 0 \\
    173 u_{x} &amp; u_{y} &amp; u_{z} &amp; 0 \\
    174 f_{x} &amp; f_{y} &amp; f_{z} &amp; 0 \\
    175 0     &amp; 0     &amp; 0     &amp; 1
    176 \end{bmatrix}
    177 \times
    178 \begin{bmatrix}
    179 1 &amp; 0 &amp; 0 &amp; -p_{x} \\
    180 0 &amp; 1 &amp; 0 &amp; -p_{y} \\
    181 0 &amp; 0 &amp; 1 &amp; -p_{z} \\
    182 0 &amp; 0 &amp; 0 &amp; 1
    183 \end{bmatrix}
    184 $$</p>
    185 <p>And with some minor simplification,
    186 $$
    187 V
    188 =
    189 \begin{bmatrix}
    190 r_{x} &amp; r_{y} &amp; r_{z} &amp; -(\vec{r} \cdot \vec{p}) \\
    191 u_{x} &amp; u_{y} &amp; u_{z} &amp; -(\vec{u} \cdot \vec{p}) \\
    192 f_{x} &amp; f_{y} &amp; f_{z} &amp; -(\vec{f} \cdot \vec{p}) \\
    193 0     &amp; 0     &amp; 0     &amp; 1
    194 \end{bmatrix}
    195 $$</p>
    196 <p>When we&rsquo;re ready to render our scene, we can apply $V$ to each vertex $\vec{q}$
    197 by calculating
    198 $$
    199 V \times \vec{q}
    200 $$
    201 in our vertex shader.</p>
    202 ]]></content:encoded>
    203     </item>
    204   </channel>
    205 </rss>