Skip to content

Commit

Permalink
good cam position + fixed depth mip mapping + trying to understand shit
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Aug 26, 2022
1 parent 7d775a8 commit 0014b6f
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 110 deletions.
4 changes: 4 additions & 0 deletions FModel/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ protected override void OnStartup(StartupEventArgs e)
Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, "Logs"));
Directory.CreateDirectory(Path.Combine(UserSettings.Default.OutputDirectory, ".data"));

#if DEBUG
Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).CreateLogger();
#else
Log.Logger = new LoggerConfiguration().WriteTo.Console(theme: AnsiConsoleTheme.Literate).WriteTo.File(
Path.Combine(UserSettings.Default.OutputDirectory, "Logs", $"FModel-Log-{DateTime.Now:yyyy-MM-dd}.txt"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [FModel] [{Level:u3}] {Message:lj}{NewLine}{Exception}").CreateLogger();
#endif

Log.Information("Version {Version}", Constants.APP_VERSION);
Log.Information("{OS}", GetOperatingSystemProductName());
Expand Down
2 changes: 2 additions & 0 deletions FModel/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static class Constants
public const string ZERO_64_CHAR = "0000000000000000000000000000000000000000000000000000000000000000";
public static readonly FGuid ZERO_GUID = new(0U);

public const float SCALE_DOWN_RATIO = 0.01F;

public const string WHITE = "#DAE5F2";
public const string GRAY = "#BBBBBB";
public const string RED = "#E06C75";
Expand Down
5 changes: 5 additions & 0 deletions FModel/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ public static float DegreesToRadians(float degrees)
{
return MathF.PI / 180f * degrees;
}

public static float RadiansToDegrees(float radians)
{
return radians* 180f / MathF.PI;
}
}
2 changes: 1 addition & 1 deletion FModel/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private async void OnLoaded(object sender, RoutedEventArgs e)
#if DEBUG
await _threadWorkerView.Begin(_ =>
_applicationView.CUE4Parse.Extract(
"FortniteGame/Content/Environments/Props/Winter/Meshes/S_Sled.uasset"));
"/Game/Gadgets/Assets/VinderTech_GliderChute/Glider_Mark_II/Meshes/Glider_Mark_II.uasset"));
#endif
}

Expand Down
38 changes: 22 additions & 16 deletions FModel/Views/Snooper/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ namespace FModel.Views.Snooper;
public class Camera
{
public Vector3 Position { get; set; }
public Vector3 Front { get; set; }

public Vector3 Up { get; private set; }
public float AspectRatio { get; set; }
public Vector3 Direction { get; private set; }
public Vector3 Up = Vector3.UnitY;

public float AspectRatio { get; }
public float Yaw { get; set; } = -90f;
public float Pitch { get; set; }
public float Zoom = 45f;

private float Zoom = 45f;

public Camera(Vector3 position, Vector3 front, Vector3 up, float aspectRatio)
public Camera(Vector3 position, Vector3 direction, float aspectRatio = 16f / 9f)
{
Position = position;
Direction = direction;
AspectRatio = aspectRatio;
Front = front;
Up = up;

// trigonometric math to calculate the cam's yaw/pitch based on position and direction to look
var yaw = MathF.Atan((-Position.X - Direction.X) / (Position.Z - Direction.Z));
var pitch = MathF.Atan((Position.Y - Direction.Y) / (Position.Z - Direction.Z));
ModifyDirection(Helper.RadiansToDegrees(yaw), Helper.RadiansToDegrees(pitch));
}

public void ModifyZoom(float zoomAmount)
Expand All @@ -38,21 +40,25 @@ public void ModifyDirection(float xOffset, float yOffset)
//We don't want to be able to look behind us by going over our head or under our feet so make sure it stays within these bounds
Pitch = Math.Clamp(Pitch, -89f, 89f);

var cameraDirection = Vector3.Zero;
cameraDirection.X = MathF.Cos(Helper.DegreesToRadians(Yaw)) * MathF.Cos(Helper.DegreesToRadians(Pitch));
cameraDirection.Y = MathF.Sin(Helper.DegreesToRadians(Pitch));
cameraDirection.Z = MathF.Sin(Helper.DegreesToRadians(Yaw)) * MathF.Cos(Helper.DegreesToRadians(Pitch));

Front = Vector3.Normalize(cameraDirection);
Direction = Vector3.Normalize(CalculateDirection());
}

public Matrix4x4 GetViewMatrix()
{
return Matrix4x4.CreateLookAt(Position, Position + Front, Up);
return Matrix4x4.CreateLookAt(Position, Position + Direction, Up);
}

public Matrix4x4 GetProjectionMatrix()
{
return Matrix4x4.CreatePerspectiveFieldOfView(Helper.DegreesToRadians(Zoom), AspectRatio, 0.1f, 100.0f);
}

private Vector3 CalculateDirection()
{
var direction = Vector3.Zero;
direction.X = MathF.Cos(Helper.DegreesToRadians(Yaw)) * MathF.Cos(Helper.DegreesToRadians(Pitch));
direction.Y = MathF.Sin(Helper.DegreesToRadians(Pitch));
direction.Z = MathF.Sin(Helper.DegreesToRadians(Yaw)) * MathF.Cos(Helper.DegreesToRadians(Pitch));
return direction;
}
}
128 changes: 128 additions & 0 deletions FModel/Views/Snooper/Mesh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Linq;
using System.Numerics;
using CUE4Parse.UE4.Assets.Exports.Material;
using CUE4Parse.UE4.Assets.Exports.Texture;
using CUE4Parse_Conversion.Meshes.PSK;
using CUE4Parse_Conversion.Textures;
using Silk.NET.OpenGL;

namespace FModel.Views.Snooper;

public class Mesh : IDisposable
{
private uint _handle;
private GL _gl;

private BufferObject<uint> _ebo;
private BufferObject<float> _vbo;
private VertexArrayObject<float, uint> _vao;

private Shader _shader;
private Texture[] _albedoMap;
// private Texture _normalMap;

private const int _vertexSize = 8; // Position + Normals + UV
private const uint _faceSize = 3; // just so we don't have to do .Length
private readonly uint[] _facesIndex = { 1, 0, 2 };

public uint[] Indices;
public float[] Vertices;
public CMaterialParams[] Params;

public Mesh(CBaseMeshLod lod, CMeshVertex[] vertices)
{
var sections = lod.Sections.Value;
Params = new CMaterialParams[sections.Length];
Indices = new uint[sections.Sum(section => section.NumFaces * _faceSize)];
Vertices = new float[Indices.Length * _vertexSize];

for (var s = 0; s < sections.Length; s++)
{
var section = sections[s];
for (uint face = 0; face < section.NumFaces; face++)
{
foreach (var f in _facesIndex)
{
var i = face * _faceSize + f;
var index = section.FirstIndex + i;
var indice = lod.Indices.Value[index];

var vert = vertices[indice];
Vertices[index * _vertexSize] = vert.Position.X * Constants.SCALE_DOWN_RATIO;
Vertices[index * _vertexSize + 1] = vert.Position.Z * Constants.SCALE_DOWN_RATIO;
Vertices[index * _vertexSize + 2] = vert.Position.Y * Constants.SCALE_DOWN_RATIO;
Vertices[index * _vertexSize + 3] = vert.Normal.X;
Vertices[index * _vertexSize + 4] = vert.Normal.Z;
Vertices[index * _vertexSize + 5] = vert.Normal.Y;
Vertices[index * _vertexSize + 6] = vert.UV.U;
Vertices[index * _vertexSize + 7] = vert.UV.V;

Indices[index] = (uint) index;
}
}

Params[s] = new CMaterialParams();
if (section.Material != null && section.Material.TryLoad(out var material) && material is UMaterialInterface unrealMaterial)
{
unrealMaterial.GetParams(Params[s]);
}
}
}

public void Setup(GL gl)
{
_gl = gl;

_handle = _gl.CreateProgram();

_ebo = new BufferObject<uint>(_gl, Indices, BufferTargetARB.ElementArrayBuffer);
_vbo = new BufferObject<float>(_gl, Vertices, BufferTargetARB.ArrayBuffer);
_vao = new VertexArrayObject<float, uint>(_gl, _vbo, _ebo);

_vao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, _vertexSize, 0); // position
_vao.VertexAttributePointer(1, 3, VertexAttribPointerType.Float, _vertexSize, 3); // normals
_vao.VertexAttributePointer(2, 2, VertexAttribPointerType.Float, _vertexSize, 6); // uv

_shader = new Shader(_gl);

_albedoMap = new Texture[Params.Length];
for (int i = 0; i < _albedoMap.Length; i++)
{
if (Params[i].Diffuse is UTexture2D { IsVirtual: false } diffuse && diffuse.GetFirstMip() is { } mip)
{
TextureDecoder.DecodeTexture(mip, diffuse.Format, diffuse.isNormalMap, out var data, out _);
_albedoMap[i] = new Texture(_gl, data, (uint) mip.SizeX, (uint) mip.SizeY);
}
}
}

public unsafe void Bind(Camera camera)
{
_vao.Bind();

_shader.Use();

_shader.SetUniform("uModel", Matrix4x4.Identity);
_shader.SetUniform("uView", camera.GetViewMatrix());
_shader.SetUniform("uProjection", camera.GetProjectionMatrix());
// _shader.SetUniform("viewPos", _camera.Position);

for (int i = 0; i < _albedoMap.Length; i++)
{
_shader.SetUniform("material.albedo", i);
_albedoMap[i].Bind(TextureUnit.Texture0 + i);
}

_gl.DrawElements(PrimitiveType.Triangles, (uint) Indices.Length, DrawElementsType.UnsignedInt, null);
}

public void Dispose()
{
_ebo.Dispose();
_vbo.Dispose();
_vao.Dispose();
_shader.Dispose();
_gl.DeleteProgram(_handle);
}
}
1 change: 0 additions & 1 deletion FModel/Views/Snooper/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ struct Material {
};
uniform Material material;
uniform vec3 viewPos;
out vec4 FragColor;
Expand Down
Loading

0 comments on commit 0014b6f

Please sign in to comment.