|
1 | | -::pp: {plus}{plus} |
| 1 | +:pp: {plus}{plus} |
2 | 2 |
|
3 | 3 | = Camera & Transformations: Mathematical Foundations |
4 | 4 | :doctype: book |
5 | 5 | :sectnums: |
6 | 6 | :sectnumlevels: 4 |
7 | | -:toc: left |
8 | 7 | :icons: font |
9 | 8 | :source-highlighter: highlightjs |
10 | 9 | :source-language: c++ |
@@ -97,34 +96,6 @@ glm::vec3 crossProduct = glm::cross(a, b); // (-3.0, 6.0, -3.0) |
97 | 96 | glm::vec3 normalized = glm::normalize(a); // (0.267, 0.535, 0.802) |
98 | 97 | ---- |
99 | 98 |
|
100 | | -==== The Right-Hand Rule |
101 | | - |
102 | | -The right-hand rule is a convention used in 3D graphics and mathematics to determine the orientation of coordinate systems and the direction of cross products. |
103 | | - |
104 | | -* *For Cross Products*: When calculating A × B: |
105 | | - 1. Point your right hand's index finger in the direction of vector A |
106 | | - 2. Point your middle finger in the direction of vector B (perpendicular to A) |
107 | | - 3. Your thumb now points in the direction of the resulting cross product |
108 | | - |
109 | | -* *For Coordinate Systems*: In a right-handed coordinate system: |
110 | | - 1. Point your right hand's index finger along the positive X-axis |
111 | | - 2. Point your middle finger along the positive Y-axis |
112 | | - 3. Your thumb points along the positive Z-axis |
113 | | - |
114 | | -[source,cpp] |
115 | | ----- |
116 | | -// The cross product direction follows the right-hand rule |
117 | | -glm::vec3 xAxis(1.0f, 0.0f, 0.0f); // Point right (positive X) |
118 | | -glm::vec3 yAxis(0.0f, 1.0f, 0.0f); // Point up (positive Y) |
119 | | -
|
120 | | -// Cross product gives the Z axis in a right-handed system |
121 | | -glm::vec3 zAxis = glm::cross(xAxis, yAxis); // Points forward (positive Z) |
122 | | -// zAxis will be (0.0f, 0.0f, 1.0f) |
123 | | -
|
124 | | -// If we reverse the order, we get the opposite direction |
125 | | -glm::vec3 negativeZ = glm::cross(yAxis, xAxis); // Points backward (negative Z) |
126 | | -// negativeZ will be (0.0f, 0.0f, -1.0f) |
127 | | ----- |
128 | 99 |
|
129 | 100 | === Matrices and Transformations |
130 | 101 |
|
@@ -177,9 +148,31 @@ glm::mat4 modelMatrix = translationMatrix * rotationMatrix * scaleMatrix; |
177 | 148 |
|
178 | 149 | ==== Matrix Order Matters |
179 | 150 |
|
180 | | -The order of matrix multiplication is crucial: |
181 | | -* In `A * B`, the transformation B is applied first, then A |
182 | | -* For our camera: `projectionMatrix * viewMatrix * modelMatrix * vertex` |
| 151 | +The order of matrix multiplication is crucial because transformations are applied from right to left. Getting the order wrong can completely change your object's final position and orientation. |
| 152 | + |
| 153 | +Consider this practical example: if you want to rotate a cube around its own center and then move it to a new position, you must apply the transformations in the correct order: |
| 154 | + |
| 155 | +[source,cpp] |
| 156 | +---- |
| 157 | +// CORRECT: Scale first, then rotate, then translate |
| 158 | +// This rotates the cube around its own center, then moves it |
| 159 | +glm::mat4 modelMatrix = translationMatrix * rotationMatrix * scaleMatrix; |
| 160 | +
|
| 161 | +// WRONG: Translate first, then rotate |
| 162 | +// This would move the cube away from origin, then rotate it around the world origin |
| 163 | +// The cube would orbit around the world center instead of rotating in place! |
| 164 | +glm::mat4 wrongMatrix = rotationMatrix * translationMatrix * scaleMatrix; |
| 165 | +---- |
| 166 | + |
| 167 | +For our camera pipeline: `projectionMatrix * viewMatrix * modelMatrix * vertex` |
| 168 | +Each transformation prepares the data for the next stage, and changing this order would break the rendering pipeline. |
| 169 | + |
| 170 | +==== Visual Example: Why Matrix Order Matters |
| 171 | + |
| 172 | +The following diagram illustrates the difference between correct and incorrect matrix multiplication order when transforming a cube: |
| 173 | + |
| 174 | +.Matrix Transformation Order Comparison |
| 175 | +image::../../../images/matrix-order-comparison.svg[Matrix Order Comparison showing correct T×R×S vs incorrect R×T×S transformation sequences] |
183 | 176 |
|
184 | 177 | ==== Row-Major vs Column-Major Representation |
185 | 178 |
|
|
0 commit comments