-
Notifications
You must be signed in to change notification settings - Fork 0
/
3D-Graphics-Math.txt
214 lines (142 loc) · 13 KB
/
3D-Graphics-Math.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
- Scalar
Ordinary single number used to represent a magnitude or a specific quantity.
- Vector (or vectors)
Mathematical tool used to represent different type of multiple-component data: position (space coordinate), direction, force, color, texture coordinate, etc.
Properties: module (magnitude, size or length), direction (defined by the line segment) and sense (pointing).
Different sizes. Most common in 3D graphics: two-component (or element), e.g. (u, v); three-component, e.g. (x, y, z); four-component, e.g. (r, g, b, a).
Important: It is not required to use four-component vectors extensivelly in 3D graphics (e.g. three-component vector is enough to represent surface normal). Four-component vectors are required for most matrix transformations (homogeneous coordinates).
Unit vector: module equal to 1. Represent only a direction (not a magnitude). Non-unit vector can be normalized (normalization) if required (the vector just represents a direction, e.g. normal to a surface). Useful to simplify some equations of 3D graphics.
Operations:
Addition, substraction, vector-scalar product (unary negation, particular case), etc.: per-component calculation.
Module: square root of the sum of the squares of the components (in 2D is simply Pythagoras’s theorem).
Normalization: divide vector by its own module.
Dot product (or inner product):
Returns a scalar: cosine of the angle between two vectors (if unit vectors).
Order is not important (commutative).
Applications: angle between two vectors, check if an object if facing another one (e.g. player facing a specific light source), projection of one vector onto another one, etc.
Cross product (or vector product):
Returns a vector (perpendicular to the plane in which the first two vectors lie).
Order is important (non-commutative): resulting vector sense (opposite).
Applications: Obtain normal to a surface, calculate LookAt matrix, etc.
Reflection and refraction: during lighting calculation.
- Matrix (or matrices)
Simplifies the process of solving one or more equations with variables that have complex relationships with one another. In 3d graphics widely used for coordinate transformation (locating, rotating, scaling, coordinate space switching, etc.).
A set of numbers arranged in uniform rows and columns (in programming terms, a two-dimensional array). Does not have to be square. Can be thought as a table of column (or row) vectors.
On most graphics-API, two-dimensional array (row-major order in memory). In OpenGL, a single array of 16 floating-point values (column-major or column-primary layout).
Transformation matrix interpretation:
First 3 columns: orientation of x, y and z axes in space. Use of directional vectors (fourth-component - homogeneous component w - value equal 0). Can also include scale (uniform or not).
Last column: translation (or position).
Multiplying all of an object’s vertices by this matrix, transforms the entire object to the given location and orientation in space.
Operations:
Addition, substraction, matrix-scalar product (unary negation, particular case), etc.: per-component calculation.
Multiplication:
Multiple transformation matrices can be multiplied each other resulting in a single transformation matrix (associative).
Order is important (non-commutative). First applied transformation to a vector is first multipled matrix (column major: apply transformations from right to left).
- Coordinate spaces
Several coordinate spaces: model, world, view, clip, NDC, and screen.
Model (object or local) space:
Positions relative to a local origin.
Origin location must be place somewhere logical: often the point about which you might rotate it to place it into a new orientation.
Geometry is stored per each game object.
World space:
Positions relative to a fixed, global origin.
All objects exist in a common frame.
Physics (and collisions) and lighting calculations take usually place here.
Model-World matrix:
Position, orientation and scale of an object placed in the world.
Stored per each game object (camera game object also has its own model-world matrix: location and orientation of the camera game object).
View (camera or eye) space:
Positions relative to a viewer origin.
A virtual fixed coordinate system that is used as a common frame of reference.
World-View matrix:
Transform (relocate and rotate) positions so the reference system of the main (or active) camera match the reference system of the world (origin position and axes orientation). Positions seen from camera game object point of view.
Stored per each camera game object. Required to be updated anytime the location or orientation of the camera is modified. Can be calculated as the inverse of the Model-World matrix of the camera (inverse traslation - match origin - and inverse rotation - match axes).
LookAt matrix (alternative): Use when camera want to point to specific target (e.g. focus on an enemy in a game). Required to be update anytime the location of the camera or of the target is modified.
Camera proyection type does not take part in this transformation.
Usually, each game object provides a single Model-View (bypass world space) matrix to the shaders so a) improve pipeline efficiency (reduce number of calculations) and b) improve calculation accuracy for game objects close to camera (the origin of the reference system).
Clip space:
Positions after projection into a non-linear homogeneous coordinate.
Projection (or View-Clip) matrix:
Transform positions (may also modify homogeneous coordinate fourth-component w) based on camera viewing volume. Define stablish clipping planes: plane equations in 3D space that determine whether geometry can be seen by the viewer.
Orthograpic (or parallel) and perspective most common types. Others: Pannini, Mercator, Fisheye, Miller, Paraboloid / Dual-Paraboloid, Equirectangular, Spherical, Sinusoidal.
Camera position and orientation do not take part in this transformation.
May be calculated just once, during camera game object initialization (it is not common proyection parameters to be modified during application execution).
NDC (normalized device coordinates) space:
Positions in canonical space volume.
Perspective division transformation.
Clipping (discard positions out of fixed planes) occurs in this space.
x and y coordinate in range (-1.0, 1.0); z coordinate in range (0.0, 1.0).
Screen space:
Positions in pixels, relative to the origin of the window (or viewpoint). Relative because many factors to take into account: window size and location, number of monitors, monitor scale and resolution, etc.
Viewport transformation.
Transform x and y coordinates; keep z (depth) coordinate.
- Coordinate (or modeling) transformations
Identity
Multiplying a position by the identity matrix is equivalent to multiplying it by 1; it does nothing to it.
Translation
Move positions along one or more of the three axes: translation vector t = (tx, ty, tz).
Inverse matrix is calculated from the opposite (negated) translation vector: -t = (-tx, -ty, -tz).
Rotation
Rotate an object about one of the axes or, indeed, any arbitrary vector.
Different type of rotation techniques: coordinate system cardinal axes rotation matrices, euler angles and quaternions.
Coordinate system cardinal axes rotation matrices
Rotation around x, y or z axes of the reference frame (or system).
These three matrices can be merged (multiplied) together to produce a composite transform matrix.
Multiple rotation matrices can be merged together to apply multiple rotations to an object (susceptible to gimbal lock).
Inverse matrix is calculated rotating in oposite sense, e.i. negated angle.
Use cases (using axis rotation matrices require rotation vector to be parallel to one of the coordinate system cardinal axes - for which the rotation will be applied - and passing through the origin of coordinate system of the world):
Rotation around a vector parallel to one of the coordinate system cardinal axes and passing through the origin of coordinate system.
Just apply corresponding rotation matrix.
Most basic (and rare) use case.
Rotation around a vector parallel to one of the coordinate system cardinal axes and passing through a point A different from the origin of coordinate system (i.e. not passing through the origin of coordinate system).
Make rotation vector pass through the origin of coordinate system. Traslate of negated A point: t = (-A).
Apply corresponding rotation matrix.
Undo traslation: t = (A)
Rotation around a vector non-parallel to any of the coordinate system cardinal axes and passing through the origin of coordinate system.
Make rotation vector be parallel to one of the coordinate system cardinal axes (may take multiple - one or two - rotations).
Apply corresponding rotation matrix.
Undo rotation(s).
Rotation around a vector non-parallel to any of the coordinate system cardinal axes and passing through a point A different from the origin of coordinate system.
Make rotation vector pass through the origin of coordinate system. Traslate of negated A point: t = (-A).
Make rotation vector be parallel to one of the coordinate system cardinal axes (may take multiple - one or two - rotations).
Apply corresponding rotation matrix.
Undo rotation(s).
Undo traslation: t = (A)
Euler angles
Set of three angles that represent orientation in space.
Each angle represents a rotation around one of three orthogonal vectors that define our frame (e.g. the x, y, and z axes in a three-dimensional frame).
Intuitive representation, e.g. hook the angles up to a user interface. Straightforward to interpolate angles (i.e. smooth motion animation).
Gimbal lock: a rotation by one angle reorients one of the axes to be aligned with another of the axes, removing a degree of freedom from the system. may occur. Not suitable for concatenating transforms.
Quaternions
A four-dimensional quantity that is similar in some ways to a complex number. It has a real part and three imaginary parts (i, j and k).
Operations with quaternions:
Addition, substraction, unary negation, module, etc.: same calculation as four-component vector (component-by-component basis).
Multiplication (non-commutative).
Represents a rotation around any axis.
Can be expressed as a real scalar part and a three-component imaginary vector part.
Require to be converted to a matrix in order to be used as a transformation resource: real part: angle; imaginary parts (vector): axis.
A sequence of rotations can be represented by a series of quaternions multiplied together. Gimbal lock cannot occur.
Scaling
Increase or decrease the dimensions of the object by a specified amount.
Uniform scaling: scale by the same amount in all three dimensions.
Stretch and shrink: Scaling can occur non-uniformly, i.e. the various dimensions can be scaled by different amounts.
Interpolation
Find values that lie between a set of known points.
Types: Linear interpolation; Non-linear interpolation (Bezier curves; Splines; Polinomial interpolation; etc.).
Linear interpolation
Given two points in space A, B: B = A + v (being v vector - module, direction and sense - from A to B: line segment AB).
So, any point P falling in that line can be represented as: P = A + t*v (being t scalar). Substituting, P = (1 - t)*A + t*B.
Values of t in range (0, 1) results in interpolated points. t = 0 results A. t = 1 results B. beyond this t values, results in extrapolated points.
Values of A and B (and therefore P) can have any number of dimensions. Scalar; two-dimensional (2-D texture coordinate: (u, v)); three-dimensional (coordinates in 3-D space or colors); higher-dimension (modeling matrices or images).
Bezier curves
A curve made up of tree (or more) control points: two of which form endpoints; others, define shape.
Cuadratic Bezier curve: 3 control points. Cubic Bezier curve: 4 control points. In practive, curves with more than four control points are not commonly used. Rather, use Splines.
The interpolation is calculated by applying the same interpolation factor on successive pairs of control points that make up the curve.
The process is executed recurrently until a single output control point is obtained.
Applying different successive interpolation factors in the range [0, 1] would result in the corresponding bezier curve defined by the original control points.
Splines
A long curve made up of several n smaller curves (e.g. Bezier curves): share endpoints.
To interpolate point P along the spline, simply divide it into n regions.
Can be used to determine the position or orientation of an object. Must be very careful about our choice of control point locations to keep motion smooth and fluid.
Velocity: rate of change in the value of interpolated point P.
Aceleration: rate of change in the value of velocity.