## Friday, February 3, 2017

### OpenGL 4 with OpenTK in C# Part 7: Vectors and Matrices

In this post we will look at vector and matrix math with OpenTK.

This is part 7 of my series on OpenGL4 with OpenTK.
For other posts in this series:
OpenGL 4 with OpenTK in C# Part 1: Initialize the GameWindow
OpenGL 4 with OpenTK in C# Part 2: Compiling shaders and linking them
OpenGL 4 with OpenTK in C# Part 3: Passing data to shaders
OpenGL 4 with OpenTK in C# Part 4: Refactoring and adding error handling
OpenGL 4 with OpenTK in C# Part 5: Buffers and Triangle
OpenGL 4 with OpenTK in C# Part 6: Rotations and Movement of objects
OpenGL 4 with OpenTK in C# Part 7: Vectors and Matrices
OpenGL 4 with OpenTK in C# Part 8: Drawing multiple objects
OpenGL 4 with OpenTK in C# Part 9: Texturing
OpenGL 4 with OpenTK in C# Part 10: Asteroid Invaders
Basic bullet movement patterns in Asteroid Invaders
OpenGL 4 with OpenTK in C# Part 11: Mipmap
OpenGL 4 with OpenTK in C# Part 12: Basic Moveable Camera
OpenGL 4 with OpenTK in C# Part 13: IcoSphere
OpenGL 4 with OpenTK in C# Part 14: Basic Text
OpenGL 4 with OpenTK in C# Part 15: Object picking by mouse

As stated in the previous post, I am in no way an expert in OpenGL, or math. I write these posts as a way to learn and if someone else finds these posts useful then all the better :)
If you think that the progress is slow, then know that I am a slow learner :P
This part will not really build on any previous code, we will instead look at the VectorN and MatrixN structs in OpenTK and see how to do basic calculations with help of their methods.

## Vectors

In mathematics, physics, and engineering, a Euclidean vector (sometimes called a geometric or spatial vector, or—as here—simply a vector) is a geometric object that has magnitude (or length) and direction - wikipedia
So the difference between a point (x, y, z) and a vector (x, y, z) is that where the point is just a point in space, the vector has its magnitude (distance from origin (0, 0, 0))
The distance can be calculated with following method:
```public static double Magnitude(Vector3 v)
{
return Math.Sqrt(Math.Pow(v.X, 2.0) + Math.Pow(v.Y, 2.0) + Math.Pow(v.Z, 2.0));
}
```
As we are using OpenTK, it already provides this for us.
```var v = new Vector3(10, 10, 10);
v.Length;```
So a point in space (x, y, z) can represent both a point, a vector and a vertex. This is great as we have the need them all in 3D.

### Unit vectors

Unit vectors always have the magnitude 1.0.
One great usage for them is to store only direction, without the magnitude. They are also great for calculations that include the magnitude as it will always be 1.
In OpenTK there are 2 ways to normalize and 1 way to estimate a normalization if accuracy is not priority.
```[TestMethod]
public void VectorTest_NormalizeVsNormalized()
{
var normalize = new Vector4(2, 2, 2, 2);
var fast = new Vector4(2, 2, 2, 2);
var normalized = normalize.Normalized(); // returns a copy that is normalized
normalize.Normalize(); // normalizes this vector
fast.NormalizeFast(); // estimates a normalized vector, inaccurate
Assert.AreEqual(normalize, normalized);
// AreEqual fails Expected:<(0,5; 0,5; 0,5; 0,5)>. Actual:<(0,4991541; 0,4991541; 0,4991541; 0,4991541)>
Assert.AreNotEqual(normalized, fast);
}
```

Scalar multiplication
If you have a unit vector with a direction, you can scale it by multiplying it with a scalar value, in the example below we scale scale it with the original vectors length to get a vector that is equal to the original.
```[TestMethod]
public void VectorTest_NormalizeMultipliedByLength()
{
var original = new Vector4(2, 2, 2, 2);
var normalized = original.Normalized();
var scaled = normalized * original.Length;
Assert.AreEqual(original, scaled);
}
```

### Vector operations

Vectors can be added, subtracted multiplied just as any numerical value.
```[TestMethod]
{
var v1 = new Vector4(2, 2, 2, 1);
var v2 = new Vector4(2, 2, 2, 1);
var actual = v1 + v2;
Assert.AreEqual(4, actual.X);
Assert.AreEqual(4, actual.Y);
Assert.AreEqual(4, actual.Z);
Assert.AreEqual(2, actual.W);
}
[TestMethod]
public void VectorTest_Subtraction()
{
var v1 = new Vector4(2, 2, 2, 1);
var v2 = new Vector4(2, 2, 2, 1);
var actual = v1 - v2;
Assert.AreEqual(0, actual.X);
Assert.AreEqual(0, actual.Y);
Assert.AreEqual(0, actual.Z);
Assert.AreEqual(0, actual.W);
}
[TestMethod]
public void VectorTest_Multiplication()
{
var v1 = new Vector4(2, 2, 2, 1);
var v2 = new Vector4(2, 2, 2, 1);
var actual = v1 * v2;
Assert.AreEqual(4, actual.X);
Assert.AreEqual(4, actual.Y);
Assert.AreEqual(4, actual.Z);
Assert.AreEqual(1, actual.W);
}
```
Vectors can only be divided with scalars, don't ask me why. Tried to understand a thread on it over at Physics Stack Exchange.
```[TestMethod]
public void VectorTest_Division()
{
var v1 = new Vector4(2, 2, 2, 1);
var actual = v1 / 2;
Assert.AreEqual(1, actual.X);
Assert.AreEqual(1, actual.Y);
Assert.AreEqual(1, actual.Z);
Assert.AreEqual(0.5f, actual.W);
}
```

Dot Product (inner product)
Geometrically, it is the product of the Euclidean magnitudes of the two vectors and the cosine of the angle between them -wikipedia
If the two vectors are unit vectors, the value will be between -1 and 1, i.e. directly equal to the cosine of the angle between them.
Ok, so what will we use this for? Well, lightning..
So, how to do it
```[TestMethod]
public void VectorTest_DotProduct()
{
var v1 = new Vector2(1, 1).Normalized();
var v2 = new Vector2(0, 1).Normalized();
var actual = Vector2.Dot(v1, v2);
// we know that the angle is 45 degrees, just need the radians
Assert.AreEqual((float)(Math.Cos(45 * (Math.PI / 180f))), actual);
}
```
The order of vectors does not matter for the dot product.

Cross Product (vector product)
Lets check wikipedia here as well
Given two linearly independent vectors a and b, the cross product, a × b, is a vector that is perpendicular to both a and b and therefore normal to the plane containing them -wikipedia
The order of vectors matters for the cross product, if you change the order the resulting vector would be inversed.
```[TestMethod]
public void VectorTest_CrossProduct()
{
var v1 = new Vector3(1, 0, 0).Normalized();
var v2 = new Vector3(0, 1, 0).Normalized();
var actual = Vector3.Cross(v1, v2);

Assert.AreEqual(0, actual.X);
Assert.AreEqual(0, actual.Y);
Assert.AreEqual(1, actual.Z);
}
[TestMethod]
public void VectorTest_CrossProduct_Inverse()
{
var v1 = new Vector3(1, 0, 0).Normalized();
var v2 = new Vector3(0, 1, 0).Normalized();
var actual = Vector3.Cross(v2, v1);

Assert.AreEqual(0, actual.X);
Assert.AreEqual(0, actual.Y);
Assert.AreEqual(-1, actual.Z);
}
```

## Matrices

In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. -wikipedia
We saw in the previous post that matrices can be really handy when working with 3D when we did the rotations and translations (move) of the object.

The reason we are using a Matrix4 object to store our transformation matrices in is because they can store both the rotation part and the translation part in the same structure.

### Identity

Basically a square matrix with its diagonal set to 1 and the rest to 0. It is its own inverse. For more info, wikipedia.
 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
In OpenTK
```var m = Matrix4.Identity;
```

### Translation

 1 0 0 tx 0 1 0 ty 0 0 1 tz 0 0 0 1
Built from a identity matrix, the last column contains the x, y, z values of where you want to move the vertex.
```[TestMethod]
public void MatrixTest_Translate()
{
var translate = Matrix4.CreateTranslation(1, 1, 1);

var v1 = new Vector4(0, 0, 0, 1);
var actual = Vector4.Transform(v1, translate);
Assert.AreEqual(1, actual.X);
Assert.AreEqual(1, actual.Y);
Assert.AreEqual(1, actual.Z);
}
```
Or in a shader, just v * m as we did in previous post.

### Rotation

Rotation.X
 1 0 0 0 0 cos(a) sin(a) 0 0 -sin(a) cos(a) 0 0 0 0 1
Rotation.Y
 cos(a) 0 -sin(a) 0 0 1 0 0 sin(a) 0 cos(a) 0 0 0 0 1
Rotation.Z
 cos(a) -sin(a) 0 0 sin(a) cos(a) 0 0 0 0 1 0 0 0 0 1
Where a being the angle.
All these can be multiplied together to form a rotation matrix for all x, y, z.
```var rx = Matrix4.CreateRotationX(k * 13.0f);
var ry = Matrix4.CreateRotationY(k * 13.0f);
var rz = Matrix4.CreateRotationZ(k * 3.0f);
var rotation = rx*ry*rz;
```
Not going to write a test for rotation just now :)

### Scaling

 sx 0 0 0 0 sy 0 0 0 0 sz 0 0 0 0 1
To scale an object, we generate a scaling matrix that is basically a diagonal with the x, y and z scaling factors set for the 3 first items.
```[TestMethod]
public void MatrixTest_Scale()
{
var translate = Matrix4.CreateScale(2, 4, 8);

var v1 = new Vector4(2, 2, 2, 1);
var actual = Vector4.Transform(v1, translate);
Assert.AreEqual(4, actual.X);
Assert.AreEqual(8, actual.Y);
Assert.AreEqual(16, actual.Z);
}
```

So this has been a long post, at least I am starting to get a hang of things. Hopefully this has helped someone out there.
To get you in a better mood after all the theory, here is a GIF of 2 of our cats fighting.. Again.

#### 1 comment:

1. You can multiply vectors with scalars in the same way you can divide. (multiplication is division of the inverse). v1 = [1,1,1], s = 3. v1 * s = [3,3,3]. v1 / s = [~0.33, ~0.33, ~0.33].