### Disclaimer:

The explanations here are very math heavy and assume some linear algebra knowledge, but aren’t always required to use their respective concepts and are usually skippable.

## TBN/Tangent-Bitangent-Normal matrix

(Bitangent is also known as “binormal”, although that is a misnomer)

 1 // Creates a TBN matrix from a normal and a tangent
2 mat3 tbnNormalTangent(vec3 normal, vec3 tangent) {
3     // For DirectX normal mapping you want to switch the order of these
4     vec3 bitangent = cross(normal, tangent);
5     return mat3(tangent, bitangent, normal);
6 }
7
8 // Creates a TBN matrix from just a normal
9 // The tangent version is needed for normal mapping because
10 //   of face rotation
11 mat3 tbnNormal(vec3 normal) {
12     // This could be
13     // normalize(vec3(normal.y - normal.z, -normal.x, normal.x))
14     vec3 tangent = normalize(cross(normal, vec3(0, 1, 1)));
15     return tbnNormalTangent(normal, tangent);
16 }


### Explanation

The TBN matrix transforms positions into the space where the normal vector points backwards, the tangent vector points to the right and the bitangent vector points upwards.

For this explanation, it’s important to know that the normal vector points in the +Z direction (in OpenGL this is out of the screen), the tangent vector points in the +X direction and the bitangent vector points in the +Y direction.

Since OpenGL matrices are column-major, the matrix above comes out as

${\displaystyle {\begin{bmatrix}t_{x}&b_{x}&n_{x}\\t_{y}&b_{y}&n_{y}\\t_{z}&b_{z}&n_{z}\end{bmatrix}}}$

Where ${\displaystyle t}$ is the tangent, ${\displaystyle b}$ is the bitangent and ${\displaystyle n}$ is the normal. If we multiply the standard basis vectors by the TBN matrix we get the following results:

${\displaystyle {\begin{bmatrix}t_{x}&b_{x}&n_{x}\\t_{y}&b_{y}&n_{y}\\t_{z}&b_{z}&n_{z}\end{bmatrix}}\cdot {\begin{bmatrix}1\\0\\0\end{bmatrix}}={\begin{bmatrix}t_{x}\\t_{y}\\t_{z}\end{bmatrix}}}$
${\displaystyle {\begin{bmatrix}t_{x}&b_{x}&n_{x}\\t_{y}&b_{y}&n_{y}\\t_{z}&b_{z}&n_{z}\end{bmatrix}}\cdot {\begin{bmatrix}0\\1\\0\end{bmatrix}}={\begin{bmatrix}b_{x}\\b_{y}\\b_{z}\end{bmatrix}}}$
${\displaystyle {\begin{bmatrix}t_{x}&b_{x}&n_{x}\\t_{y}&b_{y}&n_{y}\\t_{z}&b_{z}&n_{z}\end{bmatrix}}\cdot {\begin{bmatrix}0\\0\\1\end{bmatrix}}={\begin{bmatrix}n_{x}\\n_{y}\\n_{z}\end{bmatrix}}}$

Every vector ${\displaystyle (x,y,z)}$ can be written as

${\displaystyle x{\begin{bmatrix}1\\0\\0\end{bmatrix}}+y{\begin{bmatrix}0\\1\\0\end{bmatrix}}+z{\begin{bmatrix}0\\0\\1\end{bmatrix}}}$

(You commonly see this as ${\displaystyle x\cdot {\vec {i}}+y\cdot {\vec {j}}+z\cdot {\vec {k}}}$)

And since matrix multiplication is distributive, multiplying the (x, y, z) vector by the TBN matrix will result in

${\displaystyle x{\begin{bmatrix}t_{x}\\t_{y}\\t_{z}\end{bmatrix}}+y{\begin{bmatrix}b_{x}\\b_{y}\\b_{z}\end{bmatrix}}+z{\begin{bmatrix}n_{x}\\n_{y}\\n_{z}\end{bmatrix}}}$

Since the tangent, bitangent and normal vectors are the orthonormal basis of their respective space, this transforms the (x, y, z) vector into it.

## Inverse of a rotation matrix

The inverse function is very slow, so it’s best to avoid it (there’s a reason Optifine gives you inverse matrices for almost everything). Sadly it’s not possible to avoid it for situations when matrices are created in the shader program. Luckily for rotation matrices inverting is not necessary. Instead you can use the following identity:

${\displaystyle inverse(rotationMatrix)=transpose(rotationMatrix)}$

Furthermore, since in GLSL when a vector is multiplied by a matrix from the left (a.k.a. matrix * vector) the vector is treated as a column matrix and when multiplied from the right (a.k.a. vector * matrix) the vector is treated as a row vector, we can add a further identity:

${\displaystyle transpose(rotationMatrix)\cdot vector=vector\cdot rotationMatrix}$
This is important to keep in mind when working with TBN matrices (see above), since those are also rotation matrices.

### Explanation

The explanation will be based on 2x2 rotation matrices. Extending the explanation to 3x3 is left as an exercise to the reader.

Since rotation matrices transform from one space to another, the columns of a rotation matrix must form an orthonormal basis. The important part from this is that if we take the dot product of any two vectors from the set, we either get 1 if the two vectors are the same or 0 if they aren’t (since they are orthogonal). Let’s mark these orthonormal vectors in our rotation matrix as ${\displaystyle u}$ and ${\displaystyle v}$:

${\displaystyle R={\begin{bmatrix}u_{x}&v_{x}\\u_{y}&v_{y}\end{bmatrix}}}$

Keep in mind: ${\displaystyle u\cdot u=1}$ and ${\displaystyle u\cdot v=0}$

We’ll multiply this matrix by its transpose (marked here, and everywhere else as ${\displaystyle R^{T}}$):

${\displaystyle R^{T}R={\begin{bmatrix}u_{x}&u_{y}\\v_{x}&v_{y}\end{bmatrix}}\cdot {\begin{bmatrix}u_{x}&v_{x}\\u_{y}&v_{y}\end{bmatrix}}}$

By definition matrix multiplication works as if we took the rows of the left matrix and the columns of the right matrix as vectors and took their dot products. In other words the resulting value at position ${\displaystyle (i,j)}$ is the dot product of row ${\displaystyle i}$ from the left matrix and column ${\displaystyle j}$ of the right matrix. Knowing this we can write the result of our matrix multiplication the following way:

${\displaystyle R^{T}R={\begin{bmatrix}1&0\\0&1\end{bmatrix}}}$

And since by definition

${\displaystyle R^{-1}R={\begin{bmatrix}1&0\\0&1\end{bmatrix}}}$

We can say that ${\displaystyle R^{T}=R^{-1}}$

## Calculating normal vectors from various stuff

Heightmap means that for any ${\displaystyle (x,z)}$ coordinates you define a corresponding ${\displaystyle y}$ value. This means that you can’t have overhangs (e.g. POM uses a heightmap). I’ll also assume (for simplicity’s sake) that the area is flat, so this is perfect for water normals.

2D->3D mapping means for any ${\displaystyle (i,j)}$ value you define an ${\displaystyle (x,y,z)}$ position. ${\displaystyle i}$ and ${\displaystyle j}$ can be anything, including horizontal ${\displaystyle (x,z)}$ positions, but also stuff like latitude and longitude on a sphere. This can have overhangs and supports non-flat geometry.

### Numerical solutions

Numerical solutions in this case means these are approximations, but they work quite reliably (and most importantly: always). The functions take in an extra stepSize parameter, when the functions read from a texture, this should be the texel size, otherwise make it something small, like ${\displaystyle 0.001}$.

For heightmaps I’ll assume you have a height function that takes in a vec2 and returns the corresponding height. For 2D->3D mappings I assume you have a map function that takes in a vec2 and returns the corresponding vec3 position (not offset!).

#### From heightmaps

1 vec3 normalFromHeight(vec2 pos, float stepSize) {
2     vec2 e = vec2(stepSize, 0);
3     vec3 px1 = vec3(pos.x - e.x, height(pos - e.xy), pos.y - e.y);
4     vec3 px2 = vec3(pos.x + e.x, height(pos + e.xy), pos.y + e.y);
5     vec3 py1 = vec3(pos.x - e.y, height(pos - e.yx), pos.y - e.x);
6     vec3 py2 = vec3(pos.x + e.y, height(pos + e.yx), pos.y + e.x);
7
8     return normalize(cross(px2 - px1, py2 - py1));
9 }


#### From 2D->3D mappings

1 vec3 normalFromMapping(vec2 pos, float stepSize) {
2     vec2 e = vec2(stepSize, 0);
3     return normalize(cross(
4         map(pos + e.xy) - map(pos - e.xy),
5         map(pos + e.yx) - map(pos - e.yx)
6     ));
7 }


### Analytical solutions

Analytical solutions rely on analytical partial derivatives. For this first we need to make our separate definitions for the heightmaps and 2D->3D mappings the same. We can convert any heightmap into a 2D->3D mapping by doing.

1 vec3 heightmapToMapping(vec2 pos) {
2     return vec3(pos.x, height(pos), pos.y);
3 }


To get the normal at any point we need to get the partial derivatives of this function in respect to both of the inputs.

When we take the partial derivative of a function in respect of any of it’s parameters, we are essentially asking “How much do the values of the output change if I change one of the input parameters by a very small amount?”. How this is done exactly is beyond the scope of this document, but WolframAlpha is great at doing this. For instance if we want to calculate the normals for the surface of this shape ${\displaystyle (x,\sin(x)+\sin(z),z)}$:

We can just ask WolframAlpha to derivate it for us by saying partial derivative <mapping>. In our case this would be partial derivative f(x,z)=(x, sin(x) + sin(z), z). We get

Reading this out is easier than it seems, we only need the part after the last “=”. One extra thing we need to know is that ${\displaystyle x'(z)}$ and ${\displaystyle z'(x)}$ are 0, since these are how much ${\displaystyle x}$ changes based on ${\displaystyle z}$ and how much ${\displaystyle z}$ changes based on ${\displaystyle x}$ respectively. So our two derivatives will be ${\displaystyle x'=(1,cos(x),0)}$ and ${\displaystyle z'=(0,cos(z),1)}$. To get the normal vector from this we can take their cross product in the ${\displaystyle x'\times z'}$ order and normalize it. Proof here, moving point A around will move the point A’ around, which is just A projected on the surface and the n vector shows the calculated normal vector. Additionally in mathematics xy is the horizontal plane and z is the vertical axis, so the values there will be in a strange order.

If you do this, you should also pre-compute the cross product for the two vectors to make it faster. I would implement this as

1 vec3 getNormal(vec2 pos) {
2     return normalize(vec3(cos(pos.x), 1, cos(pos.z)));
3 }


This is much faster than the numeric approach, since for that we would’ve had to evaluate a sine function 8 times (2 times for each sampling position), but in this case we only need 2 cosines.

### Explanation

The partial derivatives are essentially telling us what direction the surface is pointing in at the current position in both axes, therefore they describe the tangent plane of the surface at that position and the two partial derivatives will be the basis vectors of it. The cross product by definition creates a vector which is perpendicular to both of the input vectors, so it is also perpendicular to the tangent plane.

The numerical solutions are just approximations of the partial derivatives.

## View position from depth

1 // projInv is the inverse of the projection matrix
2 vec3 depthToView(vec2 texCoord, float depth, mat4 projInv) {
3     vec4 ndc = vec4(texCoord, depth, 1) * 2 - 1;
4     vec4 viewPos = projInv * ndc;
5     return viewPos.xyz / viewPos.w;
6 }


You can plug in 1 for depth and normalize the result if all you need is a ray going from the camera towards the fragment positioned at ${\displaystyle texCoord}$.

### Explanation

To draw something that’s positioned in view space, we need to transform it into normalized device coordinates. This is done in 2 steps. First we transform our vertices into clip space using a projection matrix. This is where, as the name implies, clipping happens (cutting off geometry outside of the visible area). The next step is done by OpenGL, since clip space is a homogeneous coordinate system we can divide by w to apply the perspective projection and to put everything into normalized device coordinates, which then get rendered.

So in short:

${\displaystyle clippos=projectionMatrix\cdot viewpos}$
${\displaystyle NDC={\frac {clippos}{clippos_{w}}}}$

The ${\displaystyle z}$ coordinate from ${\displaystyle NDC}$ gets put into the depth buffer. To reverse this we just need to do the operations backwards. Sadly we don’t have the original value of ${\displaystyle clippos_{w}}$, but we can use the fact that the ${\displaystyle w}$ coordinate of ${\displaystyle viewpos}$ is always 1. If we multiply ${\displaystyle NDC}$ by the inverse of the projection matrix, we get

${\displaystyle projectionMatrix^{-1}\cdot NDC=projectionMatrix^{-1}\cdot {\frac {clippos}{clippos_{w}}}}$

We can extract the division:

${\displaystyle projectionMatrix^{-1}\cdot {\frac {clippos}{clippos_{w}}}={\frac {projectionMatrix^{-1}\cdot clippos}{clippos_{w}}}}$

${\displaystyle projectionMatrix^{-1}\cdot clippos}$ is just ${\displaystyle viewpos}$, so we can substitute that. Our current formula is

${\displaystyle projectionMatrix^{-1}\cdot NDC={\frac {viewpos}{clippos_{w}}}}$

And since we know that the ${\displaystyle w}$ component of viewpos is 1, we know that the w component of the resulting vector is ${\displaystyle {\frac {1}{clippos_{w}}}}$, therefore if we divide ${\displaystyle projectionMatrix^{-1}\cdot NDC}$ by the ${\displaystyle w}$ component, it’s exactly like multiplying by ${\displaystyle clippos_{w}}$.

## Linearizing depth

This takes the depth value you read from a depth buffer and transforms it into a more usable, linear range

 1 // depth is the value you read from the depth buffer
2 // near is the near plane distance
3 // far is the far plane distance
4 // Ideally the last two should come from the projection matrix
5 float linearizeDepth(float depth, float near, float far) {
6     // Convert depth back to NDC depth
7     depth = depth * 2 - 1;
8     return 2 * far * near / (far + near - depth * (far - near));
9 }
10
11 // Same algorithm, but faster, thanks to Kneemund/Niemand
12 float linearizeDepthFast(float depth, float near, float far) {
13     return (near * far) / (depth * (near - far) + far);
14 }


### Explanation

The usual perspective projection matrix looks like this:

${\displaystyle {\begin{bmatrix}{\frac {1}{aspect\cdot \tan {\frac {fov}{2}}}}&0&0&0\\0&{\frac {1}{\tan {\frac {fov}{2}}}}&0&0\\0&0&-{\frac {far+near}{far-near}}&-{\frac {2\cdot far\cdot near}{far-near}}\\0&0&-1&0\end{bmatrix}}}$

${\displaystyle aspect}$ is the aspect ratio of the screen (width in pixels divided by height in pixels)

${\displaystyle fov}$ is the vertical field of view

${\displaystyle far}$ is the far clipping plane, the distance where things stop rendering

${\displaystyle near}$ is the near clipping plane, the distance where things start rendering at. Should be strictly larger than 0.

The purpose of the matrix is to take the position of the vertices in view space and convert them to clipping space. As noted in the last “chapter”, the full process is

${\displaystyle clippos=projectionMatrix\cdot viewPos}$
${\displaystyle NDC={\frac {clippos}{clippos_{w}}}}$

Knowing this we can plug in an arbitrary vector and see what the result is:

${\displaystyle {\begin{bmatrix}{\frac {1}{aspect\cdot \tan {\frac {fov}{2}}}}&0&0&0\\0&{\frac {1}{\tan {\frac {fov}{2}}}}&0&0\\0&0&-{\frac {far+near}{far-near}}&-{\frac {2\cdot far\cdot near}{far-near}}\\0&0&-1&0\end{bmatrix}}\cdot {\begin{bmatrix}x\\y\\z\\1\end{bmatrix}}={\begin{bmatrix}x\cdot {\frac {1}{aspect\cdot \tan {\frac {fov}{2}}}}\\y\cdot {\frac {1}{\tan {\frac {fov}{2}}}}\\z\cdot \left(-{\frac {far+near}{far-near}}\right)-{\frac {2\cdot far\cdot near}{far-near}}\\-z\end{bmatrix}}}$

After this the division by ${\displaystyle w}$ happens and we get the following vector:

${\displaystyle {\begin{bmatrix}-{\frac {x}{z}}\cdot {\frac {1}{aspect\cdot \tan {\frac {fov}{2}}}}\\-{\frac {y}{z}}\cdot {\frac {1}{\tan {\frac {fov}{2}}}}\\{\frac {far+near}{far-near}}+{\frac {2\cdot far\cdot near}{z\cdot (far-near)}}\\1\end{bmatrix}}}$

From this we only care about the z coordinate for now, since that’s what gets written to the depth buffer (OpenGL compresses it to the ${\displaystyle [0,1]}$ range too, but we won’t care about this now, since the first line of the linearization is essentially just reversing this).

${\displaystyle depth={\frac {far+near}{far-near}}+{\frac {2\cdot far\cdot near}{z\cdot (far-near)}}}$

${\displaystyle depth}$ is the non-linear depth and we want to get z back from it. We can do this by using some basic algebra:

${\displaystyle depth\cdot (far-near)=far+near+{\frac {2\cdot far\cdot near}{z}}}$
${\displaystyle depth\cdot (far-near)-far-near={\frac {2\cdot far\cdot near}{z}}}$
${\displaystyle {\frac {depth\cdot (far-near)-far-near}{2\cdot far\cdot near}}={\frac {1}{z}}}$
${\displaystyle {\frac {2\cdot far\cdot near}{depth\cdot (far-near)-far-near}}=z}$

One additional difference is that the equation in the code seems to be negated. The reason for this is that in OpenGL the z axis points out of the screen, so anything in front of the player would have a negative z coordinate. This would just be an extra thing for people to keep in mind, so the usual linearization function also removes this.

## Constructing perspective projection matrices

This is only really useful if you need to either modify the FOV of the already existing projection matrix or you want to generate them for realtime cubemap rendering.

 1 // fov is the vertical field of view angle of the camera in radians
2 // aspect is the width of the resulting image divided by height
3 // near is the position of the near clipping plane, must be
4 //		strictly larger than 0
5 // far is the position of the far clipping plane
6 mat4 perspectiveProjection(float fov, float aspect, float near,
7 							float far) {
8 	float inverseTanFovHalf = 1.0 / tan(fov/ 2);
9
10 	return mat4(
11 		inverseTanFovHalf / aspect, 0, 0, 0,
12 		0, inverseTanFovHalf, 0, 0,
13 		0, 0, -(far + near) / (far - near), -1,
14 		0, 0, -2 * far * near / (far - near), 0
15 	);
16 }


Additional disclaimers for the parameters: ${\displaystyle near}$ shouldn’t be too small and ${\displaystyle far}$ shouldn’t be too large, try to keep moderation when setting those values, something around ${\displaystyle 0.01}$ for near and ${\displaystyle 100}$ to ${\displaystyle 1000}$ for far should be enough (${\displaystyle 1000}$ blocks is about ${\displaystyle 64}$ chunks). The reason for this is the nonlinear nature of the depth buffer, if I create a projection matrix with a near of ${\displaystyle 0.01}$ and a far of ${\displaystyle 1000}$, the resulting depth of the points ${\displaystyle (0,0,-500)}$ and ${\displaystyle (0,0,-501)}$ will be around ${\displaystyle 0.99998}$ and ${\displaystyle 0.99999001996}$ respectively. The difference seems small, but a good rule of thumb is that floats can store values up to around 8 significant decimal places and the difference in our case is at the 5th, so we are fine. However, if we want to avoid the near clipping as much as we can and set near to a not so unreasonable looking 0.0001, the difference becomes too small to make it reasonably precise and we get Z fighting (WolframAlpha actually gave up on me and just spit out 1 for the depths).

Additionally ${\displaystyle fov}$ can’t be greater or equal to ${\displaystyle 180}$ degrees (in radians), because ${\displaystyle tan({\frac {fov}{2}})}$ and so we’d get a division by 0 error, with values larger than 180 degrees we’d get a division by 0 error, with values larger than 180 degrees we’d just flip the screen.

### Explanation

To understand perspective matrices, we first need to know how you’d calculate perspective projection mathematically. Let’s first create an example:

We have a camera looking at our scene. It has a field of view (FOV), and a near plane, which in this case is just our screen. The size of the screen will by definition be 1 unit and it will be 1 unit away from out camera (this means our camera has a FOV of 53.13° by the way). We have 2 lines in our scene, the height of each is also 1 unit. The red line is 2 units from the screen and the blue line is at a distance of 3 units.

We want to know what the size of the lines will be when we project them onto the screen. Let’s visualize the red line first:

I marked the points with names to make it easier to refer to them. We have 2 triangles, ${\displaystyle OAB}$ and ${\displaystyle OCD}$. Since these triangles share a point (${\displaystyle O}$), an angle (angle at ${\displaystyle O}$) and one of their faces is parallel (${\displaystyle AB}$ with ${\displaystyle CD}$), they are similar. If two triangles are similar, the ratio of the length of their matching sides are the same (${\displaystyle {\frac {|AB|}{|CD|}}={\frac {|OA|}{|OC|}}}$). We can cut the triangles in half and get the same results:

Now we can say that

${\displaystyle {\frac {|Z_{1}B|}{|Z_{2}D|}}={\frac {|OZ_{1}|}{|OZ_{2}|}}}$

Which in english means that the height of the projected line on the screen is the height of the original line divided by the distance of the line to the camera. In this case distance to the camera is 3 units (2 units from the screen + 1 unit to the camera), so the height of the projected line is ${\displaystyle {\frac {1}{3}}}$. For the blue line we can do the same calculation and get that the projected height is ${\displaystyle {\frac {1}{4}}}$.

So all we need to do to achieve perspective projection is divide by z. Doing this inside a shader is not recommended because it destroys texture mapping very fast, since we don’t give OpenGL any actual information about Z to do perspective correction (it would look like a PS1 game, since that console had this problem on a hardware level). To address this, OpenGL let’s us define the position of a vertex in a vec4 and it will divide by ${\displaystyle w}$ after we give it the result.

Now we can start constructing our projection matrix. Since we’ll need to add and subtract from our coordinates, we’ll start out with the assumption, that the w coordinate is 1. The first thing we need to do is put the ${\displaystyle z}$ coordinate in the w coordinate and negate it (we need to negate it, because in OpenGL forward is the ${\displaystyle -z}$ direction):

${\displaystyle {\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&-1&0\end{bmatrix}}}$

Then we need to convert our z coordinate such that when dividing by w it will be in the range of ${\displaystyle [-1,1]}$. We’ll use the near and far clipping planes. When ${\displaystyle z}$ equals the near clipping plane, ${\displaystyle w}$ will also be the near clipping plane and we need to get -1 and similarly, when z equals the far clipping plane, w will be the far clipping plane and the result has to be 1. This means that we need to convert the current ${\displaystyle [-near,-far]}$ range to ${\displaystyle [-near,far]}$. To do this we first negate the z coordinate, then we subtract ${\displaystyle near}$ and divide by ${\displaystyle far-near}$. This brings z into the ${\displaystyle [0,1]}$ range. Next we can multiply by ${\displaystyle far+near}$ and subtract near to bring it into ${\displaystyle [-near,far]}$. So the equation becomes

${\displaystyle {\frac {-z-near}{far-near}}\cdot (far+near)-near}$

We can’t use this in a matrix in this state, so with algebra we’ll separate the multiplications and division from the additions and subtractions:

${\displaystyle z\cdot \left(-{\frac {far+near}{far-near}}\right)-{\frac {near\cdot (far+near)}{far-near}}-near}$
${\displaystyle z\cdot \left(-{\frac {far+near}{far-near}}\right)-{\frac {near\cdot far+near\cdot near+near\cdot far-near\cdot near}{far-near}}}$
${\displaystyle z\cdot \left(-{\frac {far+near}{far-near}}\right)-{\frac {-2\cdot far\cdot near}{far-near}}}$

Now we can put this into our matrix:

${\displaystyle {\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&-{\frac {far+near}{far-near}}&-{\frac {2\cdot far\cdot near}{far-near}}\\0&0&-1&0\end{bmatrix}}}$

The next step we should take is to correct the aspect ratio, otherwise this matrix can only work with square-shaped cameras. We have two options for this: Either we can multiply the y coordinate by the aspect ratio or divide x. Usually the latter is preferred, because the former would cut a bit off of the screen, where the latter only adds to it, so I’ll do the same here:

${\displaystyle {\begin{bmatrix}{\frac {1}{aspect}}&0&0&0\\0&1&0&0\\0&0&-{\frac {far+near}{far-near}}&-{\frac {2\cdot far\cdot near}{far-near}}\\0&0&-1&0\end{bmatrix}}}$

Now technically this is already a usable projection matrix. We can define the near and far clipping planes and it does the perspective transformation too. One downside to it however is that we can’t tell it to use a specific field of view, it will always be limited to 90 degrees vertically. Since we are projecting everything onto a screen 1 unit away from the camera (this can be seen from the fact that at that distance the sizes of objects don’t change), we need to take into account how large that screen is. We can do a little trigonometry to find this size:

From this we can see that ${\displaystyle {\frac {x}{1}}=\tan({\frac {fov}{2}})}$. One additional thing is that OpenGL wants the resulting image to be in the ${\displaystyle [-1,1]}$ range, which has a width and height of 2, so we won’t be multiplying this by 2 (otherwise we’d divide the ${\displaystyle x}$ and ${\displaystyle y}$ coordinate by 2 later). We can now scale our coordinates and get our final projection matrix. We need to divide the x and y coordinates by the screen size, because we want to take them from the ${\displaystyle [-screenWidth/2,screenWidth/2]}$ and ${\displaystyle [-screenHeight/2,screenHeight/2]}$ ranges to ${\displaystyle [-1,1]}$:

${\displaystyle {\begin{bmatrix}{\frac {1}{aspect\cdot \tan({\frac {fov}{2}})}}&0&0&0\\0&{\frac {1}{\tan({\frac {fov}{2}})}}&0&0\\0&0&-{\frac {far+near}{far-near}}&-{\frac {2\cdot far\cdot near}{far-near}}\\0&0&-1&0\end{bmatrix}}}$

When we recreate this in GLSL, we also need to keep in mind that OpenGL uses column-major matrices, so the order of the elements will start from the top-left element, go down in the column, then the second column, then third and fourth.

## Rotating with quaternions

 1 // a is the left side of the multiplication
2 // b is the right side of the multiplication
3 vec4 quaternionMultiply(vec4 a, vec4 b) {
4     return vec4(
5         a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x,
6         -a.x * b.z + a.y * b.w + a.z * b.x + a.w * b.y,
7         a.x * b.y - a.y * b.x + a.z * b.w + a.w * b.z,
8         -a.x * b.x - a.y * b.y - a.z * b.z + a.w * b.w
9     );
10 }
11
12 // pos is the position you want to rotate
13 // axis is the unit length axis you want to rotate it around
14 // angle is the angle you want to rotate the object by
15 vec3 quaternionRotate(vec3 pos, vec3 axis, float angle) {
16     vec4 q = vec4(sin(angle / 2.0) * axis, cos(angle / 2.0));
17     vec4 qInv = vec4(-q.xyz, q.w);
18     return quaternionMultiply(quaternionMultiply(q, vec4(pos, 0)), qInv).xyz;
19 }
20
21 // Fast versions, but less intuitive
22
23 vec4 fastQuaternionMultiply(vec4 a, vec4 b) {
24     return vec4(
25         a.w * b.xyz + b.w * a.xyz + cross(a.xyz, b.xyz),
26         a.w * b.w - dot(a.xyz, b.xyz)
27     );
28 }
29
30 vec3 fastQuaternionRotate(vec3 pos, vec3 axis, float angle) {
31     vec4 q = vec4(sin(angle / 2.0) * axis, cos(angle / 2.0));
32     vec4 partial = fastQuaternionMultiply(q, vec4(pos, 0));
33     // Skip calculating the real part, since it's always 0
34     return -partial.w * q.xyz + q.w * partial.xyz + cross(q.xyz, partial.xyz);
35 }


### Explanation

Visualization of quaternion rotation. We rotate P around a given axis by ${\textstyle \theta }$

Quaternions are essentially an extension to complex numbers, but instead of using one imaginary component, they use three: ${\textstyle i}$, ${\textstyle j}$ and ${\displaystyle k}$ with the following fundamental formula:

${\displaystyle i^{2}=j^{2}=k^{2}=ijk=-1}$

In graphics programming we use them to describe 3D rotation in a way, where we don't run into common issues, such as gimbal lock. We can do this by taking our axis of rotation ${\textstyle u}$ and our angle ${\textstyle \theta }$ and constructing the following quaternion from them:

${\displaystyle q=\cos \left({\frac {\theta }{2}}\right)+\sin \left({\frac {\theta }{2}}\right)u_{x}i+\sin \left({\frac {\theta }{2}}\right)u_{y}j+\sin \left({\frac {\theta }{2}}\right)u_{y}k}$

Then inverting it by negating the imaginary components (this only works, because ${\textstyle q}$ is already unit length):

${\displaystyle q^{-1}=\cos \left({\frac {\theta }{2}}\right)-\sin \left({\frac {\theta }{2}}\right)u_{x}i-\sin \left({\frac {\theta }{2}}\right)u_{y}j-\sin \left({\frac {\theta }{2}}\right)u_{y}k}$

And applying them using quaternion multiplication in the following way:

${\displaystyle p'=q\cdot p\cdot q^{-1}}$

Where ${\textstyle p}$ is the position we want to rotate with the x, y, and z components being multiplied by ${\textstyle i}$, ${\textstyle j}$ and ${\displaystyle k}$ respectively and ${\textstyle p'}$ is the rotated position in a similar manner.

Since quaternions are far too complex for the scope of this page, instead of providing an explanation built up from scratch, I will use a formal proof to verify these claims. To do this all we need to check is that when rotating a given position around a given axis by a certain angle, the resulting position...

1. ... has the same angle with the axis of rotation as the original position
2. ... forms the given angle with the original position when projected onto the plane perpendicular to the axis

To answer either of these questions, we need to create a generalized formula for ${\textstyle p'}$. For this we will use the scalar+vector representation of a quaternion where ${\displaystyle (r+v_{x}i+v_{y}j+v_{z}k)=(r,{\vec {v}})}$. Quaternion multiplication in this version can be written as:

${\displaystyle (r_{1},{\vec {v}}_{1})(r_{2},{\vec {v}}_{2})=(r_{1}r_{2}-{\vec {v}}_{1}\cdot {\vec {v}}_{2},r_{1}{\vec {v}}_{2}+r_{2}{\vec {v}}_{1}+{\vec {v}}_{1}\times {\vec {v}}_{2})}$

With this we can expand the original formula:

{\displaystyle {\begin{aligned}c:&=\cos \left({\frac {\theta }{2}}\right)\\s:&=\sin \left({\frac {\theta }{2}}\right)\\p'&=q\cdot p\cdot q'\\&=(c,s{\vec {u}})\cdot (0,{\vec {p}})\cdot (c,-s{\vec {u}})\\&=\left(-s{\vec {u}}\cdot {\vec {p}},c{\vec {p}}+(s{\vec {u}}\times {\vec {p}})\right)\cdot (c,-s{\vec {u}})\\&=(0,s^{2}\cdot ({\vec {u}}\cdot {\vec {p}})\cdot {\vec {u}}+c^{2}{\vec {p}}+2sc({\vec {u}}\times {\vec {p}})+s^{2}{\vec {u}}\times ({\vec {u}}\times p))\end{aligned}}}
(Note: in mathematics, ${\textstyle :=}$ means assign)

From this we can see that our new, rotated position is ${\textstyle s^{2}\cdot ({\vec {u}}\cdot {\vec {p}})\cdot u+c^{2}{\vec {p}}+2sc({\vec {u}}\times {\vec {p}})+s^{2}{\vec {u}}\times ({\vec {u}}\times p)}$. Now we can look at the first requirement. Instead of finding the angle, we can just check the dot product between the rotated vector and the rotation axis and it should be equal to the dot product between the original vector and the rotation axis. For this we need 2 important identities, ${\textstyle {\vec {v}}\cdot {\vec {v}}=1}$ and ${\textstyle {\vec {a}}\cdot ({\vec {a}}\times {\vec {b}})=0}$:

{\displaystyle {\begin{aligned}{\vec {p}}'\cdot {\vec {u}}&=(s^{2}({\vec {u}}\cdot {\vec {p}})\cdot {\vec {u}}+c^{2}{\vec {p}}+2sc({\vec {u}}\times {\vec {p}})+s^{2}{\vec {u}}\times ({\vec {u}}\times {\vec {p}}))\cdot {\vec {u}}\\&=s^{2}(({\vec {u}}\cdot {\vec {p}})\cdot {\vec {u}})\cdot {\vec {u}}+c^{2}{\vec {p}}\cdot {\vec {u}}+2sc({\vec {u}}\times {\vec {p}})\cdot {\vec {u}}+s^{2}({\vec {u}}\times ({\vec {u}}\times {\vec {p}}))\cdot {\vec {u}}\\&=s^{2}({\vec {u}}\cdot {\vec {p}})\cdot ({\vec {u}}\cdot {\vec {u}})+c^{2}({\vec {u}}\cdot {\vec {p}})\\&=(s^{2}+c^{2})\cdot ({\vec {u}}\cdot {\vec {p}})\\&=\left(\sin \left({\frac {\theta }{2}}\right)^{2}+\cos \left({\frac {\theta }{2}}\right)^{2}\right)\cdot ({\vec {u}}\cdot {\vec {p}})\\&={\vec {u}}\cdot {\vec {p}}\end{aligned}}}

Therefore the first requirement is fulfilled. For the second requirement we will project the two points onto the plane perpendicular to the rotation axis. This can be done by creating tangent and bitangent vectors using the vector pointing to ${\textstyle p}$ and the rotation axis (if they are parallel, then the rotation will leave it in the same spot, so we can ignore this option):

{\displaystyle {\begin{aligned}{\vec {b}}&={\vec {u}}\times {\vec {p}}\\{\vec {t}}&={\vec {b}}\times {\vec {u}}\end{aligned}}}

Now we can project the two points onto the plane defined by the two vectors:

For ${\textstyle {\vec {p}}}$:

{\displaystyle {\begin{aligned}{\vec {p}}\cdot {\vec {b}}&={\vec {p}}\cdot ({\vec {u}}\times {\vec {p}})=0\\{\vec {p}}\cdot {\vec {t}}&={\vec {p}}\cdot (({\vec {u}}\times {\vec {p}})\times {\vec {u}})\end{aligned}}}

For ${\textstyle {\vec {p}}'}$ (The actual algebra was left out as a space and time saving measure):

{\displaystyle {\begin{aligned}{\vec {p}}'\cdot {\vec {b}}&=...=2sc\cdot |{\vec {u}}\times {\vec {p}}|^{2}=\sin(\theta )\cdot |{\vec {u}}\times {\vec {p}}|^{2}\\{\vec {p}}'\cdot {\vec {t}}&=...=c^{2}{\vec {p}}\cdot (({\vec {u}}\times {\vec {p}})\times {\vec {u}})-s^{2}\cdot |({\vec {u}}\times {\vec {p}})\times {\vec {u}}|^{2}\end{aligned}}}

The second row from this is very hard to convert. Ideally we would like to force the ${\textstyle cos(2x)=cos(x)^{2}-sin(x)^{2}}$ identity into it, but the multipliers on the squared functions at first seem to be different. They are actually the same, the reason for it is the following:

{\displaystyle {\begin{aligned}{\vec {p}}\cdot (({\vec {u}}\times {\vec {p}})\times {\vec {u}})&=|({\vec {u}}\times {\vec {p}})\times {\vec {u}}|^{2}\\|{\vec {p}}|\cdot |({\vec {u}}\times {\vec {p}})\times {\vec {u}}|\cdot \cos(\theta _{1})&=||{\vec {u}}\times {\vec {p}}|\cdot |{\vec {u}}|\cdot \sin(\theta _{2})|^{2}\\\end{aligned}}}

${\textstyle \theta _{1}}$ is the angle between ${\textstyle {\vec {p}}}$ and ${\textstyle ({\vec {u}}\times {\vec {p}})\times {\vec {u}}}$ and ${\textstyle \theta _{2}}$ is the angles between ${\textstyle {\vec {u}}}$ and ${\textstyle {\vec {u}}\times {\vec {p}}}$. The latter is actually 90°, so I will omit it from this point forward. Similarly the length of ${\textstyle {\vec {u}}}$ is 1, so it will receive the same fate.

{\displaystyle {\begin{aligned}|{\vec {p}}|\cdot |({\vec {u}}\times {\vec {p}})\times {\vec {u}}|\cdot \cos(\theta _{1})&=|{\vec {u}}\times {\vec {p}}|^{2}\\|{\vec {p}}|\cdot |{\vec {u}}\times {\vec {p}}|\cdot \cos(\theta _{1})&=|{\vec {u}}\times {\vec {p}}|^{2}\\|{\vec {p}}|^{2}\cdot \sin(\theta _{3})\cdot \cos(\theta _{1})&=|{\vec {p}}|^{2}\cdot sin(\theta _{3})^{2}\\\end{aligned}}}

${\textstyle \theta _{3}}$ is the angle between ${\textstyle {\vec {u}}}$ and ${\textstyle {\vec {p}}}$. Finally, we have arrived at our final equation in this set:

${\displaystyle \cos(\theta _{1})=sin(\theta _{3})}$

These two are equal because of how their angles are set up. Now we can get back to the original problem. We'll extract ${\textstyle {\vec {p}}\cdot (({\vec {u}}\times {\vec {p}})\times {\vec {u}})}$ into ${\textstyle l}$ for simplicity's sake:

{\displaystyle {\begin{aligned}{\vec {p}}\cdot {\vec {t}}&=l\\{\vec {p}}\cdot {\vec {b}}&=0\\\\{\vec {p}}'\cdot {\vec {t}}&=cos(\theta )\cdot l\\{\vec {p}}'\cdot {\vec {b}}&=sin(\theta )\cdot l\end{aligned}}}

This by definition means that when we project ${\textstyle {\vec {p}}}$ and ${\textstyle {\vec {p}}'}$ onto the plane perpendicular to the rotation axis, we get two vectors which are exactly ${\displaystyle \theta }$ angle apart counter clockwise, which proofs the second requirement and the rotation with quaternions.