Add a very simple matrix class.

This commit is contained in:
H. Utku Maden 2023-06-30 19:57:04 +03:00
parent d72a07354a
commit 8e2db62e56
4 changed files with 72 additions and 12 deletions

@ -11,15 +11,14 @@ namespace Quik.CommandMachine
private QRectangle _viewport; private QRectangle _viewport;
private readonly Stack<QRectangle> _viewportStack = new Stack<QRectangle>(); private readonly Stack<QRectangle> _viewportStack = new Stack<QRectangle>();
private readonly Stack<object> _matrixStack = new Stack<object>(); private readonly Stack<QMat4> _matrixStack = new Stack<QMat4>();
private Command _customCommandBase = Command.CustomCommandBase; private Command _customCommandBase = Command.CustomCommandBase;
private readonly List<QuikCommandHandler> _customCommands = new List<QuikCommandHandler>(); private readonly List<QuikCommandHandler> _customCommands = new List<QuikCommandHandler>();
public QRectangle Viewport => _viewport; public QRectangle Viewport => _viewport;
// TODO: Make a real matrix class. public QMat4 ActiveTransforms { get; }
public float[] ActiveTransforms { get; }
public StyleStack Style { get; } = new StyleStack(new Quik.Style()); public StyleStack Style { get; } = new StyleStack(new Quik.Style());
@ -115,7 +114,7 @@ namespace Quik.CommandMachine
_viewportStack.Clear(); _viewportStack.Clear();
_matrixStack.Clear(); _matrixStack.Clear();
_matrixStack.Push(null); // TODO: replace with identity matrix. _matrixStack.Push(QMat4.Identity);
} }
private void ConditionalHandler(CommandQueue queue) private void ConditionalHandler(CommandQueue queue)

@ -204,5 +204,19 @@ namespace Quik.OpenGL
fixed (float* ptr = &m11) fixed (float* ptr = &m11)
_uniformMatrix4fv(location, count, transpose, ptr); _uniformMatrix4fv(location, count, transpose, ptr);
} }
[MethodImpl(AggressiveInlining)]
public static void UniformMatrix4(int location, bool transpose, in QMat4 m4)
{
fixed (float* ptr = &m4.M11)
_uniformMatrix4fv(location, 1, transpose, ptr);
}
[MethodImpl(AggressiveInlining)]
public static void UniformMatrix4(int location, int count, bool transpose, ref QMat4 m4)
{
fixed (float* ptr = &m4.M11)
_uniformMatrix4fv(location, count, transpose, ptr);
}
} }
} }

@ -89,17 +89,11 @@ namespace Quik.OpenGL
draw.PrepareFrame(); draw.PrepareFrame();
QVec2 size = view.Size; QVec2 size = view.Size;
// TODO: can i has matrices? QMat4.Orthographic(out QMat4 matrix, view);
float[] matrix = new float[] {
1/size.X, 0, 0, 0,
0, 1/size.Y, 0, 0,
0, 0, 1, 0,
-0.5f, -0.5f, 0, 1
};
GL.UseProgram(program); GL.UseProgram(program);
GL.Uniform1(iMaxZ, queue.ZDepth); GL.Uniform1(iMaxZ, queue.ZDepth);
GL.UniformMatrix4(m4Transforms, false, ref matrix[0]); GL.UniformMatrix4(m4Transforms, false, in matrix);
GL.Uniform1(fSdfThreshold, 0.0f); GL.Uniform1(fSdfThreshold, 0.0f);
GL.Uniform1(txTexture, 0); GL.Uniform1(txTexture, 0);
GL.Enable(GL_BLEND); GL.Enable(GL_BLEND);

@ -402,4 +402,57 @@ namespace Quik
/// </summary> /// </summary>
public QVec2 C; public QVec2 C;
} }
[DebuggerDisplay("[{M11} {M12} {M13} {M14}; {M21} {M22} {M23} {M24}; {M31} {M32} {M33} {M34}; {M41} {M42} {M43} {M44}]")]
public struct QMat4
{
public float M11, M21, M31, M41;
public float M12, M22, M32, M42;
public float M13, M23, M33, M43;
public float M14, M24, M34, M44;
public static QMat4 Identity { get; } = new QMat4()
{
M11 = 1.0f,
M22 = 1.0f,
M33 = 1.0f,
M44 = 1.0f
};
public static void Translation(out QMat4 mat, float x, float y, float z)
{
mat = Identity;
mat.M41 = x;
mat.M42 = y;
mat.M43 = z;
}
public static void Scale(out QMat4 mat, float x, float y, float z)
{
mat = default;
mat.M11 = x;
mat.M22 = y;
mat.M33 = z;
mat.M44 = 1.0f;
}
public static void Orthographic(out QMat4 mat, QRectangle bounds, float near = 1, float far = -1)
{
float a, b, c;
mat = default;
a = 1.0f/(bounds.Right - bounds.Left);
b = 1.0f/(bounds.Top - bounds.Bottom);
c = 1.0f/(near - far);
mat.M11 = 2 * a;
mat.M22 = 2 * b;
mat.M33 = 2 * c;
mat.M41 = -a * (bounds.Left + bounds.Right);
mat.M42 = -b * (bounds.Top + bounds.Bottom);
mat.M43 = -c * (near + far);
mat.M44 = 1.0f;
}
}
} }