System.Numerics + cached uniforms location

This commit is contained in:
4sval 2022-11-24 19:09:15 +01:00
parent e7ff5c41a6
commit 53dd500e03
15 changed files with 59 additions and 108 deletions

View File

@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using ImGuiNET;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
using ErrorCode = OpenTK.Graphics.OpenGL4.ErrorCode;
@ -453,7 +453,7 @@ outputColor = color * texture(in_fontTexture, texCoord);
// Setup orthographic projection matrix into our constant buffer
ImGuiIOPtr io = ImGui.GetIO();
Matrix4 mvp = Matrix4.CreateOrthographicOffCenter(
var mvp = OpenTK.Mathematics.Matrix4.CreateOrthographicOffCenter(
0.0f,
io.DisplaySize.X,
io.DisplaySize.Y,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 84 KiB

View File

@ -41,7 +41,6 @@ using FModel.Views;
using FModel.Views.Resources.Controls;
using FModel.Views.Snooper;
using Newtonsoft.Json;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using Serilog;
@ -89,7 +88,7 @@ public class CUE4ParseViewModel : ViewModel
return _snooper ??= new Snooper(GameWindowSettings.Default,
new NativeWindowSettings
{
Size = new Vector2i(
Size = new OpenTK.Mathematics.Vector2i(
Convert.ToInt32(SystemParameters.MaximizedPrimaryScreenWidth * .75),
Convert.ToInt32(SystemParameters.MaximizedPrimaryScreenHeight * .85)),
NumberOfSamples = Constants.SAMPLES_COUNT,

View File

@ -1,6 +1,6 @@
using System;
using System.Numerics;
using ImGuiNET;
using OpenTK.Mathematics;
namespace FModel.Views.Snooper;
@ -68,54 +68,14 @@ public class Camera
Direction = Vector3.Normalize(direction);
}
public Matrix4 GetViewMatrix()
public Matrix4x4 GetViewMatrix()
{
return Matrix4.LookAt(Position, Position + Direction, Up);
return Matrix4x4.CreateLookAt(Position, Position + Direction, Up);
}
public Matrix4 GetProjectionMatrix()
public Matrix4x4 GetProjectionMatrix()
{
return CreatePerspectiveFieldOfView(Helper.DegreesToRadians(Zoom), AspectRatio, Near, Far);
}
/// <summary>
/// OpenTK function causes a gap between the faded out grid & the skybox
/// so we use the System.Numerics function instead with OpenTK types
/// </summary>
private Matrix4 CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance)
{
if (fieldOfView is <= 0.0f or >= MathF.PI)
throw new ArgumentOutOfRangeException(nameof(fieldOfView));
if (nearPlaneDistance <= 0.0f)
throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance));
if (farPlaneDistance <= 0.0f)
throw new ArgumentOutOfRangeException(nameof(farPlaneDistance));
if (nearPlaneDistance >= farPlaneDistance)
throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance));
float yScale = 1.0f / MathF.Tan(fieldOfView * 0.5f);
float xScale = yScale / aspectRatio;
Matrix4 result = Matrix4.Zero;
result.M11 = xScale;
result.M12 = result.M13 = result.M14 = 0.0f;
result.M22 = yScale;
result.M21 = result.M23 = result.M24 = 0.0f;
result.M31 = result.M32 = 0.0f;
float negFarRange = float.IsPositiveInfinity(farPlaneDistance) ? -1.0f : farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M33 = negFarRange;
result.M34 = -1.0f;
result.M41 = result.M42 = result.M44 = 0.0f;
result.M43 = nearPlaneDistance * negFarRange;
return result;
return Matrix4x4.CreatePerspectiveFieldOfView(Helper.DegreesToRadians(Zoom), AspectRatio, Near, Far);
}
private const float _step = 0.01f;

View File

@ -1,6 +1,5 @@
using System;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace FModel.Views.Snooper;
@ -33,7 +32,7 @@ public class FramebufferObject : IDisposable
-1.0f, 1.0f, 0.0f, 1.0f
};
public FramebufferObject(Vector2i size)
public FramebufferObject(OpenTK.Mathematics.Vector2i size)
{
_width = size.X;
_height = size.Y;

View File

@ -1,6 +1,6 @@
using System;
using System.Numerics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace FModel.Views.Snooper;
@ -39,7 +39,7 @@ public class Grid : IDisposable
_vao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, 3, 0); // position
}
public void Render(Matrix4 viewMatrix, Matrix4 projMatrix, float near, float far)
public void Render(Matrix4x4 viewMatrix, Matrix4x4 projMatrix, float near, float far)
{
GL.Disable(EnableCap.DepthTest);
_vao.Bind();

View File

@ -2,8 +2,7 @@
using System.Collections.Generic;
using CUE4Parse.UE4.Objects.Core.Misc;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using Vector2 = System.Numerics.Vector2;
using System.Numerics;
namespace FModel.Views.Snooper;
@ -53,7 +52,7 @@ public class PickingTexture : IDisposable
Bind(0);
}
public void Render(Matrix4 viewMatrix, Matrix4 projMatrix, IDictionary<FGuid,Model> models)
public void Render(Matrix4x4 viewMatrix, Matrix4x4 projMatrix, IDictionary<FGuid,Model> models)
{
Bind();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

View File

@ -14,7 +14,6 @@ using CUE4Parse.UE4.Objects.Core.Math;
using CUE4Parse.UE4.Objects.Engine;
using CUE4Parse.UE4.Objects.UObject;
using FModel.Settings;
using Vector3 = OpenTK.Mathematics.Vector3;
namespace FModel.Views.Snooper;

View File

@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Reflection;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace FModel.Views.Snooper;
public class Shader : IDisposable
{
private readonly int _handle;
private readonly Dictionary<string, int> _uniformsLocation = new ();
public Shader() : this("default") {}
@ -54,17 +56,17 @@ public class Shader : IDisposable
GL.UseProgram(_handle);
}
public void Render(Matrix4 viewMatrix, Vector3 viewPos, Vector3 viewDir, Matrix4 projMatrix)
public void Render(Matrix4x4 viewMatrix, Vector3 viewPos, Vector3 viewDir, Matrix4x4 projMatrix)
{
Render(viewMatrix, viewPos, projMatrix);
SetUniform("uViewDir", viewDir);
}
public void Render(Matrix4 viewMatrix, Vector3 viewPos, Matrix4 projMatrix)
public void Render(Matrix4x4 viewMatrix, Vector3 viewPos, Matrix4x4 projMatrix)
{
Render(viewMatrix, projMatrix);
SetUniform("uViewPos", viewPos);
}
public void Render(Matrix4 viewMatrix, Matrix4 projMatrix)
public void Render(Matrix4x4 viewMatrix, Matrix4x4 projMatrix)
{
Use();
SetUniform("uView", viewMatrix);
@ -73,61 +75,51 @@ public class Shader : IDisposable
public void SetUniform(string name, int value)
{
int location = GL.GetUniformLocation(_handle, name);
ThrowIfNotFound(location, name);
GL.Uniform1(location, value);
GL.Uniform1(GetUniformLocation(name), value);
}
public unsafe void SetUniform(string name, Matrix4 value) => UniformMatrix4(name, (float*) &value);
public unsafe void SetUniform(string name, System.Numerics.Matrix4x4 value) => UniformMatrix4(name, (float*) &value);
public unsafe void SetUniform(string name, Matrix4x4 value) => UniformMatrix4(name, (float*) &value);
public unsafe void UniformMatrix4(string name, float* value)
{
//A new overload has been created for setting a uniform so we can use the transform in our shader.
int location = GL.GetUniformLocation(_handle, name);
ThrowIfNotFound(location, name);
GL.UniformMatrix4(location, 1, false, value);
GL.UniformMatrix4(GetUniformLocation(name), 1, false, value);
}
public void SetUniform(string name, bool value) => SetUniform(name, Convert.ToUInt32(value));
public void SetUniform(string name, uint value)
{
int location = GL.GetUniformLocation(_handle, name);
ThrowIfNotFound(location, name);
GL.Uniform1(location, value);
GL.Uniform1(GetUniformLocation(name), value);
}
public void SetUniform(string name, float value)
{
int location = GL.GetUniformLocation(_handle, name);
ThrowIfNotFound(location, name);
GL.Uniform1(location, value);
GL.Uniform1(GetUniformLocation(name), value);
}
public void SetUniform(string name, Vector3 value) => SetUniform3(name, value.X, value.Y, value.Z);
public void SetUniform(string name, System.Numerics.Vector3 value) => SetUniform3(name, value.X, value.Y, value.Z);
public void SetUniform3(string name, float x, float y, float z)
{
int location = GL.GetUniformLocation(_handle, name);
ThrowIfNotFound(location, name);
GL.Uniform3(location, x, y, z);
GL.Uniform3(GetUniformLocation(name), x, y, z);
}
public void SetUniform(string name, Vector4 value) => SetUniform4(name, value.X, value.Y, value.Z, value.W);
public void SetUniform(string name, System.Numerics.Vector4 value) => SetUniform4(name, value.X, value.Y, value.Z, value.W);
public void SetUniform4(string name, float x, float y, float z, float w)
{
int location = GL.GetUniformLocation(_handle, name);
ThrowIfNotFound(location, name);
GL.Uniform4(location, x, y, z, w);
GL.Uniform4(GetUniformLocation(name), x, y, z, w);
}
private void ThrowIfNotFound(int location, string name)
private int GetUniformLocation(string name)
{
if (location == -1)
if (!_uniformsLocation.TryGetValue(name, out int location))
{
throw new Exception($"{name} uniform not found on shader.");
location = GL.GetUniformLocation(_handle, name);
_uniformsLocation.Add(name, location);
if (location == -1)
{
throw new Exception($"{name} uniform not found on shader.");
}
}
return location;
}
public void Dispose()

View File

@ -30,17 +30,23 @@ public class Skeleton : IDisposable
continue;
var transform = Transform.Identity;
for (int j = 0; j <= boneIndex; j++)
while (boneIndex > -1)
{
var t = RefSkel.ReferenceSkeleton.FinalRefBonePose[j].Inverse();
(t.Translation.X, t.Translation.Z, t.Translation.Y) = (-t.Translation.Z, -t.Translation.Y, -t.Translation.X);
var matrix = Matrix4x4.CreateScale(t.Scale3D.ToMapVector());
// matrix *= Matrix4x4.CreateFromQuaternion(t.Rotation);
matrix *= Matrix4x4.CreateTranslation(t.Translation * Constants.SCALE_DOWN_RATIO);
var t = RefSkel.ReferenceSkeleton.FinalRefBonePose[boneIndex];
transform.Position += t.Rotation.RotateVector(t.Translation.ToMapVector()) * Constants.SCALE_DOWN_RATIO;
// Console.WriteLine($@"{t.Translation}");
transform.Relation *= matrix;
boneIndex = RefSkel.ReferenceSkeleton.FinalRefBoneInfo[boneIndex].ParentIndex;
}
// for (int j = 0; j <= 3; j++)
// {
// var t = RefSkel.ReferenceSkeleton.FinalRefBonePose[j];
// // var matrix = Matrix4x4.CreateScale(t.Scale3D.ToMapVector());
// // matrix *= Matrix4x4.CreateFromQuaternion(t.Rotation);
// transform.Position += t.Rotation.UnrotateVector(t.Translation.ToMapVector()) * Constants.SCALE_DOWN_RATIO;
//
// // Console.WriteLine($@"{t.Translation}");
// // transform.Relation *= matrix;
// }
Sockets[i] = new Socket(socket, transform);
}

View File

@ -1,6 +1,6 @@
using System;
using System.Numerics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace FModel.Views.Snooper;
@ -79,7 +79,7 @@ public class Skybox : IDisposable
_vao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, 3, 0); // position
}
public void Render(Matrix4 viewMatrix, Matrix4 projMatrix)
public void Render(Matrix4x4 viewMatrix, Matrix4x4 projMatrix)
{
GL.DepthFunc(DepthFunction.Lequal);

View File

@ -3,10 +3,8 @@ using System.Collections.Generic;
using CUE4Parse.UE4.Objects.Core.Misc;
using FModel.Framework;
using ImGuiNET;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using Vector2 = System.Numerics.Vector2;
using Vector4 = System.Numerics.Vector4;
using System.Numerics;
namespace FModel.Views.Snooper;
@ -88,7 +86,7 @@ public class SnimGui
}
}
private void DrawDockSpace(Vector2i size)
private void DrawDockSpace(OpenTK.Mathematics.Vector2i size)
{
const ImGuiWindowFlags flags =
ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking |

View File

@ -1,10 +1,10 @@
using System.ComponentModel;
using System.Numerics;
using System.Threading;
using System.Windows;
using CUE4Parse.UE4.Assets.Exports;
using ImGuiNET;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
@ -78,7 +78,7 @@ public class Snooper : GameWindow
base.OnLoad();
CenterWindow();
GL.ClearColor(Color4.Black);
GL.ClearColor(OpenTK.Mathematics.Color4.Black);
GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Multisample);

View File

@ -13,11 +13,10 @@ public class Socket : IDisposable
{
Name = socket.SocketName.Text;
Bone = socket.BoneName.Text;
Transform = Transform.Identity;
Transform.Relation = transform.Matrix;
Transform.Position = socket.RelativeLocation.ToMapVector() * Constants.SCALE_DOWN_RATIO;
Transform.Rotation = socket.RelativeRotation;
Transform.Scale = socket.RelativeScale.ToMapVector();
Transform = transform;
// Transform.Relation = transform.Matrix;
// Transform.Position = socket.RelativeRotation.RotateVector(socket.RelativeLocation.ToMapVector()) * Constants.SCALE_DOWN_RATIO;
// Transform.Scale = socket.RelativeScale.ToMapVector();
}
public void Dispose()