-
Notifications
You must be signed in to change notification settings - Fork 1
Mathematics
In order to get familiar with the mathematics used in raytracing, I've designed this step by step guide in understanding
the linear algebra.
We'll talk about the mathematics in 3D/4D space.
Refer to the examples at the bottom of the page for more intuitive understanding.
Please refer to the wiki for all of the
symbols.
Symbols used in this document:
| Symbol | Meaning | Description |
|---|---|---|
| Real numbers | Real numbers are all numbers that can be written on a number line. | |
| Real numbers in n-dimensional space | Real numbers in n-dimensional space. | |
| Vector | A vector is a line that starts at the origin and points somewhere. | |
| Unit vector | A unit vector is a vector with a length of 1. | |
| Pi | Pi is a mathematical constant (3.14...) | |
| Angle in degrees | Angle in degrees (0 - 360). | |
| Angle in radians | Angle in radians (0 - |
|
| Degree | Degree symbol. | |
| Angle | Angle symbol. | |
| Parallel | A parallel object is pointing in the same direction as another. | |
| Perpendicular | A perpendicular object is pointing in a 90 degree angle. | |
| Dot product | Dot product is a way to calculate the angle between two vectors. | |
| Cross product | Cross product is a way to calculate a vector perpendicular to two other vectors. | |
| Less than or equal to | Less than or equal to means that the left side is less than or equal to the right side. | |
| Greater than or equal to | Greater than or equal to means that the left side is greater than or equal to the right side. | |
| Equivalent by definition | Equivalent means that the left side is equal to the right side. | |
| Logical consequence | Logical consequence means that the properties of the right side are true for the left side. |
Let's start with definitions, don't be intimidated by the math, it's not as hard as it looks.
Later on in the examples you'll see that it's actually quite simple.
This is a 3D object, it has 3 components, x, y and z. As you can see, we write vectors with a little arrow on top of the name.
Usually with 3D objects we use
I'll be using the notation
We can add two
We can also subtract
An
However, we can multiply an
Just like multiplication, we have to use scalar division.
Vectors start at the origin and points to a position in space.
Vectors are written with a little arrow on top of the name, like this:
Vectors have a few operations that are unique to them.
One of them is calculating the magnitude of a vector, this is also called the length of a vector.
The magnitude of a vector is always a positive number.
Sometimes you'll see the magnitude of a vector written with double bars, this is just a different notation.
I'll be using the single bar notation, but it's important to know that they mean the same thing.
An optimization that can be done is to square the magnitude of a vector to avoid using the square root function.
It's important to understand that the result of the magnitude of a vector is the size of the vector, which is a number.
Now that we know how to calculate the magnitude of a vector, we can normalize a vector.
Normalization is the process of making a vector have a length of 1, yet the same direction.
This is used for calculating the angle between two vectors, and a lot more
which we'll see later.
A normalized vector is also called a unit vector, and is written with a little hat on top of the name, like
this:
Now if we calculate the magnitude of a normalized vector, we get 1.
The dot product is a way to calculate the angle between two vectors.
The dot product is also called the scalar product.
The result of the dot product is a number between -1 and 1.
| Dot product | Definition | Explanation | Angle |
|---|---|---|---|
| 1 | Vectors are pointing in the same direction. | ||
| 0 | Vectors are perpendicular to each other. | ||
| -1 | Vectors are pointing in the opposite direction. |
The notation is a literal dot sign, not a multiplication sign.
The dot product is a scalar, meaning that it's a number.
Dot product is sometimes written as
The cross product is a way to calculate a vector that is perpendicular
Naturally this means that the cross product is a vector.
You can see the notation is a literal cross sign.
Cross product is sometimes written as
Now that we know how to calculate the dot product, we can calculate the angle between two vectors.
The angle between two vectors is the same as the angle between two lines.
Here theta
That being said, these are slow operations, it's better to use a dot product to calculate the angle between two
vectors.
Which we've already seen, just make sure to normalize the vectors first.
Like mentioned in the dot product, only here we normalize both vectors first.
Reflection is a way to calculate the direction of a vector after it bounces off a surface.
This is used for calculating the direction of a vector after it hits a surface.
Here the
This normalizes the normal vector, then calculates the reflection vector.
You could avoid this assuming
Important to note that the resulting reflection vector is not normalized, this obviously means that the incoming vector has the same magnitude as the outgoing vector.
You could extend the definition to where
This is not necessary, but it's important to know that the normal should be pointing towards the incoming vector.
Perhaps turn the normal around if this is the case.
Refraction is a way to calculate the direction of a vector after it passes through a surface.
This can be anything from glass, water, air, etc.
With reference to the wiki we can calculate the refraction
vector.
We start with the definitions:
Here
This is just a simple dot product between the normal and the incoming vector.
The reason why it's inverted is because we want the angle between the normal and the incoming vector, not the outgoing.
This is an important step, now we know the angle of the outgoing vector.
If the outgoing vector is pointing away from the surface, then the angle is invalid.
This means that we've got an internal reflection, which we can already calculate with the reflection vector.
This means that if
This is the final definition, here we calculate the refraction vector.
Points are very similar to vectors, but they don't have a direction.
Points are written with a capital letter, like this:
We can convert a point to a vector, this is done by subtracting the origin from the point.
Important to understand that we're creating a vector that points from
The subtraction is the same as any
We can calculate the center of two points, this is done by adding the two points together and dividing by 2.
This is a pretty simple operation.
We can calculate the distance between two points.
This might be useful for calculating the distance between two objects, or a whole lot of other things.
Here
Another optimization that can be done is to square the distance to avoid using the square root function.
- Home
- Introduction
-
Installation
- Install-Package
- Build (Optional for developers)
- Examples
- Design (For developers)