Advertisement
Guest User

Untitled

a guest
Sep 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.85 KB | None | 0 0
  1. //! # Thinking about matrix layout
  2. //!
  3. //! So, you're programming your game's mathematics. You've gotten to the point where you're using
  4. //! matrices to do transformations quickly, and you're trying to figure out where to put the numbers
  5. //! on the screen so that everything works out correctly. This, somehow, has ended up being confusing
  6. //! and you're trying to figure out why you can't just lay out your numbers in the source code the
  7. //! same way they're laid out in your linear algebra textbook or whatever webpage you found that
  8. //! explains matrices. This explanation will attempt to make sense of that.
  9. //!
  10. //! Say you have a 3D vector at point `(0x, 1y, 2z)`. If you were a mathematician, you would write
  11. //! that out as a vertical box, like this:
  12. //!
  13. //! ```text
  14. //! ___
  15. //! |0|
  16. //! |1|
  17. //! |2|
  18. //! ‾‾‾
  19. //! ```
  20. //!
  21. //! However, you're reading the documentation for a Rust crate, so it's not much of a stretch to
  22. //! assume that you're a programmer, not a mathematician. With that in mind, if you wanted to write
  23. //! out that vector in your source code (assuming you're using our handy [`GVector3`] type), you'd
  24. //! probably do so like this:
  25. //!
  26. //! ```rust
  27. //! let vector = GVector3::new(0, 1, 2);
  28. //! ```
  29. //!
  30. //! In memory, that corresponds to an array of three numbers:
  31. //!
  32. //! ```rust
  33. //! let vector_array = [0, 1, 2];
  34. //! ```
  35. //!
  36. //! Let's return to the (admittedly debunked) assumption that you're a mathematician. Mathematician
  37. //! you may be aware of the fact that vectors are just single-column matrices. Indeed, you can form
  38. //! a matrix by just taking a bunch of vectors (I've commonly heard these referred to as "basis
  39. //! vectors" in this context):
  40. //!
  41. //! ```text
  42. //! ___ ___ ___
  43. //! |0| |2| |4|
  44. //! |1| |3| |5|
  45. //! ‾‾‾ ‾‾‾ ‾‾‾
  46. //! ```
  47. //!
  48. //! and gluing them together:
  49. //!
  50. //! ```text
  51. //! _______
  52. //! |0 2 4|
  53. //! |1 3 5|
  54. //! ‾‾‾‾‾‾‾
  55. //! ```
  56. //!
  57. //! Just like that, we've created a `2x3` matrix via judicious application of Elmer's. (I'll note
  58. //! that "gluing vectors together" isn't a mathematically rigorous operation. However, I don't have
  59. //! a formal background in math, so I don't know anyone who will complain at me for doing that.)
  60. //! Conceptually, we can think of that as an array of vectors:
  61. //!
  62. //! ```text
  63. //! ___
  64. //! |0|
  65. //! |1|
  66. //! ‾‾‾
  67. //! ___
  68. //! |2|
  69. //! |3|
  70. //! ‾‾‾
  71. //! ___
  72. //! |4|
  73. //! |5|
  74. //! ‾‾‾
  75. //! ```
  76. //!
  77. //! Re-donning our programmer hats, we would express an array of arrays like this:
  78. //!
  79. //! ```rust
  80. //! let matrix_array = [
  81. //!     [0, 1],
  82. //!     [2, 3],
  83. //!     [4, 5],
  84. //! ];
  85. //! ```
  86. //!
  87. //! Or, with one of Gullery's matrix types, like this:
  88. //!
  89. //! ```rust
  90. //! let matrix = GLMatrix2r3c {
  91. //!     x: GLVector2::new(0, 1),
  92. //!     y: GLVector2::new(2, 3),
  93. //!     z: GLVector2::new(4, 5),
  94. //! }
  95. //! ```
  96. //!
  97. //! We've recreated the mismatch. This style of layout is referred to as "column major", and most
  98. //! Rust mathematics libraries (and this crate) use that convention. Unfortunately, it means that
  99. //! our source code layout doesn't correspond with mathematical layout. Hopefully, understanding
  100. //! that will help you avoid mixing up your numbers in your code.
  101. //!
  102. //! ----
  103. //!
  104. //! As an aside, you may be aware of the fact that the size of a matrix is written as
  105. //! `rows x columns`. Indeed, we used that notation above to denote the size of our 2 row, 3 column
  106. //! matrix above. GLSL doesn't do that. Instead, it writes matrix size as `columns x rows`, such
  107. //! that our previous `2x3` matrix would have a GLSL type of `mat3x2`. The only sensible reason I
  108. //! can think of for this is that God hates OpenGL programmers, and He worked his best to make
  109. //! matricies as confusing as possible. This is consistent with the rest of OpenGL's raw API, so
  110. //! I'd consider that relatively plausible.
  111. //!
  112. //! Good luck.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement