Skip to content

Latest commit

 

History

History
195 lines (108 loc) · 6.95 KB

File metadata and controls

195 lines (108 loc) · 6.95 KB

Vectors

Introduction to Vectors

A vector is a mathematical object representing both a direction and a magnitude (length). In 3D rendering, vectors are crucial for describing positions, directions, and various operations related to object transformations (translation, rotation, scale), lighting, and camera movement.

Notation and Representation

A vector in 3D is represented as 3 components along an axis:

Img

Types of Vectors

  • Position Vectors: Describe points in space relative to the origin (e.g., $\mathbf{p} = (x, y, z)$).
  • Direction Vectors: Define only the direction and are often normalized (unit vectors, so length 1).

Img Img

Applications in 3D Rendering

Camera Movement

  • View Vector: Determines the direction the camera is facing.
  • Up Vector: Helps define the camera's orientation to avoid upside-down views.

Object Transformations

Vectors are crucial for describing object positions, orientations, and movements within the 3D scene. Basic transformations include:

  • Translation: Moving an object by adding a vector.
  • Scaling: Adjusting an object’s size through scalar multiplication of position vectors.

Lighting Calculations

Lighting models use vectors to calculate illumination:

  • Diffuse Lighting: Uses the dot product between the light direction and surface normal to compute the brightness of a surface.
  • Specular Lighting: Uses reflection vectors to simulate shiny surfaces by calculating how closely the view vector aligns with the reflected light vector.

Calculating the magnitude

Magnitude (Length): The length of a vector is calculated using the Pythagoras' Theorem:

Img

Let's say you have a position XY of (3,2), then the magnitude is calculated as follows:

Img Img

Vector Operations

Addition and Subtraction

Addition: Adding two vectors results in a new vector that represents moving from the start of the first vector to the end of the second vector:

Img Img

And it doesn't matter which order we add them, we get the same direction.

Subtraction: Represents the vector pointing from one vector to another:

Img
Img

Scalar Multiplication

A scalar is just a number (like 7 or −0.32), so definitely not a vector.
Multiplying a vector by a scalar changes its magnitude without altering its direction:

Img

In the following example the vector in blue is multiplier 2.5x times. It still points in the same direction, but is 2.5x times longer:

Img
Img

Normalization

Normalization scales a vector to a unit vector (length of 1). Basically you divide the vector by its own lenth and you get a normalized vector:

Img

In the following example we have the vector (3,4) which we divide by it's own length to get a normalized vector.

Img

Application:

  • Direction Vectors: In lighting and camera movement, normalized vectors help simplify calculations by keeping the focus on direction only.

Polar Vector Coordinate

Let's say we have the following vector a:

Img

To get the X and Y components of the vector we can use trigonometry:

Img Img

Then vector a is noted as follows:

Img

Dot Product

Applications

  • Angle Calculation: The dot product can determine the angle $\theta$ between two vectors.

  • Lighting: The angle between a light direction and a surface normal determines the brightness in diffuse lighting.

  • Projection: The dot product helps project one vector onto another.

  • Determining View and Visibility: In rendering, the dot product can be used to determine if a surface is facing the camera or away from it. This helps with culling and optimizations.

Cartesian Coordinate Dot Product

The dot product between vector A and vector B is equal to the sum of the products of each component which is calculated as follows:

Img

In the following example we have 2 vector where we calculate the dot product:

Img
Img

Interpretation

The dot product provides insight into the relationship between two vectors:

  • If a.b > 0: The angle between a and b is less than 90°. This means the vectors are pointing in roughly the same direction.
  • If a.b < 0: The angle between a and b is greater than 90°. This means the vectors are pointing in roughly opposite directions.
  • If a.b == 0: The vectors are orthogonal (perpendicular) to each other, meaning they form a 90° angle.

Polar Coordinate Dot Product

Let's say we have the following vectors a and b:

Img

Vector a and b are represented as follows:

Img Img

Now we can calculate the dot product:

Img

Note

In the calculation above we used the trigonometric identity for the cosine of the difference of two angles: Img
More info about this here.

Therefore we have:

Img

Calculating the Angle Between Vectors

From the above statement we can now easily calculate the angle:

Img

Cross Product

The cross product between two vectors a and b produces a third vector c that is perpendicular to both:

Img

How to calculate the cross product:

Img

Applications in 3D Rendering

  • Surface Normals: These are vectors perpendicular to a surface. They are essential for lighting calculations, as they determine how light interacts with the surface.

References