180 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using OpenTK.Mathematics;
public class MyMatrix
{
private float[,] elements = new float[4, 4];
public MyMatrix(float pRow0Column0,
float pRow0Column1,
float pRow0Column2,
float pRow0Column3,
float pRow1Column0,
float pRow1Column1,
float pRow1Column2,
float pRow1Column3,
float pRow2Column0,
float pRow2Column1,
float pRow2Column2,
float pRow2Column3,
float pRow3Column0,
float pRow3Column1,
float pRow3Column2,
float pRow3Column3)
{
// row 0
SetElement(0, 0, pRow0Column0);
SetElement(0, 1, pRow0Column1);
SetElement(0, 2, pRow0Column2);
SetElement(0, 3, pRow0Column3);
// row 1
SetElement(1, 0, pRow1Column0);
SetElement(1, 1, pRow1Column1);
SetElement(1, 2, pRow1Column2);
SetElement(1, 3, pRow1Column3);
// row 2
SetElement(2, 0, pRow2Column0);
SetElement(2, 1, pRow2Column1);
SetElement(2, 2, pRow2Column2);
SetElement(2, 3, pRow2Column3);
// row 3
SetElement(3, 0, pRow3Column0);
SetElement(3, 1, pRow3Column1);
SetElement(3, 2, pRow3Column2);
SetElement(3, 3, pRow3Column3);
}
public float GetElement(int pRow, int pColumn)
{
return elements[pRow, pColumn];
}
public void SetElement(int pRow, int pColumn, float pValue)
{
elements[pRow, pColumn] = pValue;
}
public static MyMatrix CreateIdentity()
{
return new MyMatrix(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}
public static MyMatrix CreateTranslation(MyVector pTranslation)
{
return new MyMatrix(1f, 0f, 0f, 0f,
0f, 1f, 0f, 0f,
0f, 0f, 1f, 0f,
pTranslation.X, pTranslation.Y, pTranslation.Z, 1f);
}
public static MyMatrix CreateScale(MyVector pScale)
{
return new MyMatrix(1f * pScale.X, 0f, 0f, 0f,
0f, 1f * pScale.Y, 0f, 0f,
0f, 0f, 1f * pScale.Z, 0f,
0f, 0f, 0f, 1f * pScale.W);
}
public static MyMatrix CreateRotationX(float pAngle)
{
float cos = MathF.Cos(pAngle);
float sin = MathF.Sin(pAngle);
return new MyMatrix(1f, 0f, 0f, 0f,
0f, cos, sin, 0f,
0f, -sin, cos, 0f,
0f, 0f, 0f, 1f);
}
public static MyMatrix CreateRotationY(float pAngle)
{
float cos = MathF.Cos(pAngle);
float sin = MathF.Sin(pAngle);
return new MyMatrix(
cos, 0f, -sin, 0f,
0f, 1f, 0f, 0f,
sin, 0f, cos, 0f,
0f, 0f, 0f, 1f
);
}
public static MyMatrix CreateRotationZ(float pAngle)
{
float cos = MathF.Cos(pAngle);
float sin = MathF.Sin(pAngle);
return new MyMatrix(
cos, sin, 0f, 0f,
-sin, cos, 0f, 0f,
0, 0f, 1f, 0f,
0f, 0f, 0f, 1f
);
}
public MyVector Multiply(MyVector pVector)
{
float x = GetElement(0,0) * pVector.X + GetElement(0,1) * pVector.Y + GetElement(0,2) * pVector.Z + GetElement(0,3) * pVector.W;
float y = GetElement(1,0) * pVector.X + GetElement(1,1) * pVector.Y + GetElement(1,2) * pVector.Z + GetElement(1,3) * pVector.W;
float z = GetElement(2,0) * pVector.X + GetElement(2,1) * pVector.Y + GetElement(2,2) * pVector.Z + GetElement(2,3) * pVector.W;
float w = GetElement(3,0) * pVector.X + GetElement(3,1) * pVector.Y + GetElement(3,2) * pVector.Z + GetElement(3,3) * pVector.W;
return new MyVector(x, y, z, w);
}
public MyMatrix Multiply(MyMatrix pMatrix)
{
MyMatrix result = new MyMatrix(
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
);
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
float sum = 0f;
for (int k = 0; k < 4; k++)
{
sum += GetElement(row, k) * pMatrix.GetElement(k, col);
}
result.SetElement(row, col, sum);
}
}
return result;
}
public MyMatrix Inverse()
{
return null;
}
// this method takes our MyMatrix and converts it to an OpenTK Matrix4
// this looks a little odd as the OpenTK Matrix4 constructor takes the elements in row major order
public Matrix4 ToMatrix4()
{
return new Matrix4(
GetElement(0,0), GetElement(0,1), GetElement(0,2), GetElement(0,3),
GetElement(1,0), GetElement(1,1), GetElement(1,2), GetElement(1,3),
GetElement(2,0), GetElement(2,1), GetElement(2,2), GetElement(2,3),
GetElement(3,0), GetElement(3,1), GetElement(3,2), GetElement(3,3)
);
}
}