Difference between revisions of "Advanced C++ Programming Tutorial 5"

From Chilipedia
Jump to: navigation, search
(Part 2)
(Tutorial 5.2)
 
(11 intermediate revisions by 2 users not shown)
Line 10: Line 10:
 
=== Part 2 ===
 
=== Part 2 ===
 
* Augmented 3x3 matrix for encoding translations
 
* Augmented 3x3 matrix for encoding translations
 +
* Matrix multiplication order matters!
 
* Porting pipeline to use matrix transforms
 
* Porting pipeline to use matrix transforms
 
* Bug hunting!
 
* Bug hunting!
  
 
== Video Timestamp Index ==
 
== Video Timestamp Index ==
* [https://youtu.be/iNCx0yIeCNc Tutorial 5.1]
+
 
* [https://youtu.be/CS4HQdQnbaA Tutorial 5.2]
+
===[https://youtu.be/iNCx0yIeCNc Tutorial 5.1]===
 +
<div class="mw-collapsible mw-collapsed"><br />
 +
* Matrices: what are they, what are they good for? [https://youtu.be/iNCx0yIeCNc?t=0m44s 0:44]
 +
* Basic Matrix operations: Matrix by Vector multiplication [https://youtu.be/iNCx0yIeCNc?t=3m05s 3:05]
 +
* Creating a templated <code>_Mat2<T></code> Matrix class [https://youtu.be/iNCx0yIeCNc?t=7m16s 7:16]
 +
<div class="mw-collapsible-content">
 +
:* Matrix data stored as 2D array <code>T arr[2][2]</code>
 +
</div>
 +
* Implementing the Matrix by Vector multiplication method [https://youtu.be/iNCx0yIeCNc?t=9m21s 9:21]
 +
* Testing in <code>game.cpp</code> [https://youtu.be/iNCx0yIeCNc?t=13m00s 13:00]
 +
* Implementing basic transformations in Matrix form as static functions [https://youtu.be/iNCx0yIeCNc?t=15m16s 15:16]
 +
<div class="mw-collapsible-content">
 +
:* Scaling & Identity Matrices [https://youtu.be/iNCx0yIeCNc?t=17m13s 17:13]
 +
:* Flipping the y-axis [https://youtu.be/iNCx0yIeCNc?t=18m01s 18:01]
 +
:* Rotation [https://youtu.be/iNCx0yIeCNc?t=18m56s 18:56]
 +
</div>
 +
* Testing in <code>game.cpp</code> [https://youtu.be/iNCx0yIeCNc?t=21m25s 21:25]
 +
* Basic matrix operations: Matrix multiplication [https://youtu.be/iNCx0yIeCNc?t=23m06s 23:06]
 +
* Implementing & testing the Matrix multiplication method [https://youtu.be/iNCx0yIeCNc?t=25m52s 25:52]
 +
* Recap: concatenating transformations by multiplying transformation matrices [https://youtu.be/iNCx0yIeCNc?t=30m10s 30:10]
 +
<div class="mw-collapsible-content">
 +
:* This is the beauty of matrix operations: one single concatenated matrix holds all the transformation information
 +
:* Concatenating matrices cuts down the number of vector operations needed at runtime
 +
</div>
 +
* What about translation? [https://youtu.be/iNCx0yIeCNc?t=31m20s 31:20]
 +
</div>
 +
 
 +
===[https://youtu.be/CS4HQdQnbaA Tutorial 5.2]===
 +
<div class="mw-collapsible mw-collapsed"><br />
 +
* Translation: why is it a complicated transformation? [https://youtu.be/CS4HQdQnbaA?t=0m14s= 0:14]
 +
* Trick: augment the matrix (add an extra dimension) [https://youtu.be/CS4HQdQnbaA?t=3m11s 3:11]
 +
* Coding this into the framweork [https://youtu.be/CS4HQdQnbaA?t=6m54s 6:54]
 +
<div class="mw-collapsible-content">
 +
:* Implementing a <code>Mat3</code> class for (3x3) matrices [https://youtu.be/CS4HQdQnbaA?t=6m54s 6:54]
 +
:* Implementing a <code>Vec3</code> class for (3x1) vectors [https://youtu.be/CS4HQdQnbaA?t=8m51s 8:51]
 +
:* Test run [https://youtu.be/CS4HQdQnbaA?t=10m48s 10:48]
 +
</div>
 +
* The order of applying transformations and its relation to Matrix multiplication (concatenation) [https://youtu.be/CS4HQdQnbaA?t=12m09s 12:09]
 +
<div class="mw-collapsible-content">
 +
:* The order in which you concatenate matrices matters: ABC != CBA
 +
:* The sequence of applying transformations reads from right to left in Matrix multiplication
 +
:: T = CBA means transformation A is applied first, then B, then C
 +
:* Demo of this principle in debug mode [https://youtu.be/CS4HQdQnbaA?t=14m19s 14:19]
 +
</div>
 +
* Matrix augmentation is not just a hack, it falls in the domain of Projective Geometry [https://youtu.be/CS4HQdQnbaA?t=15m29s 15:29]
 +
* Fix up the Starfield rendering pipeline [https://youtu.be/CS4HQdQnbaA?t=16m31s 16:31]
 +
<div class="mw-collapsible-content">
 +
:* Design decision: keep working with <code>Vec2</code>, this requires conversion operators to and from <code>Vec3</code>
 +
:* Refactoring the <code>DrawClosedPolygon</code> function in the <code>Graphics</code> class [https://youtu.be/CS4HQdQnbaA?t=18m03s 18:03]
 +
:* Refactoring the <code>Drawable</code> class [https://youtu.be/CS4HQdQnbaA?t=19m15s 19:15]
 +
:* Refactoring the <code>Entity</code> class [https://youtu.be/CS4HQdQnbaA?t=23m22s 23:22]
 +
:* Refactoring the <code>CoordinateTransformer</code> class [https://youtu.be/CS4HQdQnbaA?t=23m22s 24:00]
 +
</div>
 +
* Debugging session: the wrong order of transformations can mess you up [https://youtu.be/CS4HQdQnbaA?t=24m51s 24:51]
 +
<div class="mw-collapsible-content">
 +
:* Be careful with Matrix multiplication operators: using the overloaded <code>operator*=()</code> is risky:
 +
:: <code>M *= A</code> yields M=MA, but we need M=AM
 +
</div>
 +
* Recap & key takeaways: math is doable, mind your order of concatenation [https://youtu.be/CS4HQdQnbaA?t=32m12s 32:12]
 +
* Tutorial Roadmap from here on out [https://youtu.be/CS4HQdQnbaA?t=34m27s 34:27]
 +
</div>
  
 
== Links ==
 
== Links ==

Latest revision as of 16:06, 18 April 2020

Hot take: the Matrix is a mediocre film at best. It's a pretty great way to encode transformations though, so let's matrix it up!

Topics

Part 1

  • Matrix form
  • Matrix by vector multiplication
  • Matrix encoding of scaling, rotation, and flipping
  • Matrix multiplication
  • Concatenating matrix transforms

Part 2

  • Augmented 3x3 matrix for encoding translations
  • Matrix multiplication order matters!
  • Porting pipeline to use matrix transforms
  • Bug hunting!

Video Timestamp Index

Tutorial 5.1


  • Matrices: what are they, what are they good for? 0:44
  • Basic Matrix operations: Matrix by Vector multiplication 3:05
  • Creating a templated _Mat2<T> Matrix class 7:16
  • Matrix data stored as 2D array T arr[2][2]
  • Implementing the Matrix by Vector multiplication method 9:21
  • Testing in game.cpp 13:00
  • Implementing basic transformations in Matrix form as static functions 15:16
  • Scaling & Identity Matrices 17:13
  • Flipping the y-axis 18:01
  • Rotation 18:56
  • Testing in game.cpp 21:25
  • Basic matrix operations: Matrix multiplication 23:06
  • Implementing & testing the Matrix multiplication method 25:52
  • Recap: concatenating transformations by multiplying transformation matrices 30:10
  • This is the beauty of matrix operations: one single concatenated matrix holds all the transformation information
  • Concatenating matrices cuts down the number of vector operations needed at runtime
  • What about translation? 31:20

Tutorial 5.2


  • Translation: why is it a complicated transformation? 0:14
  • Trick: augment the matrix (add an extra dimension) 3:11
  • Coding this into the framweork 6:54
  • Implementing a Mat3 class for (3x3) matrices 6:54
  • Implementing a Vec3 class for (3x1) vectors 8:51
  • Test run 10:48
  • The order of applying transformations and its relation to Matrix multiplication (concatenation) 12:09
  • The order in which you concatenate matrices matters: ABC != CBA
  • The sequence of applying transformations reads from right to left in Matrix multiplication
T = CBA means transformation A is applied first, then B, then C
  • Demo of this principle in debug mode 14:19
  • Matrix augmentation is not just a hack, it falls in the domain of Projective Geometry 15:29
  • Fix up the Starfield rendering pipeline 16:31
  • Design decision: keep working with Vec2, this requires conversion operators to and from Vec3
  • Refactoring the DrawClosedPolygon function in the Graphics class 18:03
  • Refactoring the Drawable class 19:15
  • Refactoring the Entity class 23:22
  • Refactoring the CoordinateTransformer class 24:00
  • Debugging session: the wrong order of transformations can mess you up 24:51
  • Be careful with Matrix multiplication operators: using the overloaded operator*=() is risky:
M *= A yields M=MA, but we need M=AM
  • Recap & key takeaways: math is doable, mind your order of concatenation 32:12
  • Tutorial Roadmap from here on out 34:27

Links

  • GitHub Repo (advmath)
  • Note that the code for this tutorial can be found on the <starfield> branch of the repo not in <master>

Homework

None! (or check out 3D Fundamentals you lazy fucks!)

See also