Update CubeMesh & rename CubeGroupMesh to CubeMeshCollection

This commit is contained in:
miku-666
2024-09-17 19:00:59 +02:00
parent a287c91dd8
commit 10f1220171
7 changed files with 195 additions and 210 deletions

View File

@@ -212,7 +212,7 @@
<Compile Include="Rendering\Camera\PerspectiveCamera.cs" />
<Compile Include="Rendering\Cube.cs" />
<Compile Include="Rendering\CubeMesh.cs" />
<Compile Include="Rendering\CubeGroupMesh.cs" />
<Compile Include="Rendering\CubeMeshCollection.cs" />
<Compile Include="Rendering\FrameBuffer.cs" />
<Compile Include="Rendering\IndexBuffer.cs" />
<Compile Include="Rendering\DrawContext.cs" />

View File

@@ -18,26 +18,25 @@
using System;
using OpenTK;
using PckStudio.Extensions;
using PckStudio.Internal;
using PckStudio.Internal.Skin;
namespace PckStudio.Rendering
{
internal class Cube
internal sealed class Cube
{
internal Vector3 Position { get; set; }
internal Vector3 Position { get; }
internal Vector3 Size { get; set; }
internal Vector3 Size { get; }
internal Vector3 Rotation { get; set; }
internal Vector3 Rotation { get; }
internal Vector2 Uv { get; set; }
internal Vector2 Uv { get; }
internal float Inflate { get; set; }
internal float Inflate { get; }
internal bool MirrorTexture { get; set; }
internal bool MirrorTexture { get; }
internal bool FlipZMapping { get; set; }
internal bool FlipZMapping { get; }
internal Vector3 Center => Position + Size / 2f;
@@ -51,12 +50,10 @@ namespace PckStudio.Rendering
Right
}
internal static Cube FromSkinBox(SkinBOX skinBOX)
{
return new Cube(skinBOX.Pos.ToOpenTKVector(), skinBOX.Size.ToOpenTKVector(), skinBOX.UV.ToOpenTKVector(), skinBOX.Scale, skinBOX.Mirror, false);
}
internal static Cube FromSkinBox(SkinBOX skinBOX) => FromSkinBox(skinBOX, 0f);
public Cube() { }
internal static Cube FromSkinBox(SkinBOX skinBOX, float inflate, bool flipZMapping = false)
=> new Cube(skinBOX.Pos.ToOpenTKVector(), skinBOX.Size.ToOpenTKVector(), skinBOX.UV.ToOpenTKVector(), skinBOX.Scale + inflate, skinBOX.Mirror, flipZMapping);
public Cube(Vector3 position, Vector3 size, Vector2 uv, float inflate, bool mirrorTexture, bool flipZMapping)
{

View File

@@ -22,16 +22,17 @@ using System.Drawing;
using System.Linq;
using OpenTK;
using PckStudio.Extensions;
using PckStudio.Internal;
using PckStudio.Internal.Skin;
namespace PckStudio.Rendering
{
internal class CubeMesh : Cube
internal class CubeMesh : GenericMesh<TextureVertex>
{
internal bool ShouldRender { get; set; } = true;
internal bool ShouldRender { get; } = true;
private static int[] indicesData = [
private Cube _cube;
public Vector3 Center => _cube.Center;
internal static int[] IndicesData { get; } = [
// Face 1 (Back)
0, 1, 2,
2, 3, 0,
@@ -48,93 +49,94 @@ namespace PckStudio.Rendering
16, 17, 18,
18, 19, 16,
// Face 6 (Right)
20, 21, 22,
20, 21, 22,
22, 23, 20
];
private TextureVertex[] vertices;
/// <param name="position"></param>
/// <param name="size"></param>
/// <param name="uv"></param>
/// <param name="inflate"></param>
/// <param name="mirrorTexture"></param>
/// <param name="flipZMapping">Flips the bottom face mapping of the uv mapping</param>
public CubeMesh(bool enabled, Vector3 position, Vector3 size, Vector2 uv, float inflate, bool mirrorTexture, bool flipZMapping)
: base(position, size, uv, inflate, mirrorTexture, flipZMapping)
{
ShouldRender = enabled;
Update();
}
public CubeMesh(Vector3 position, Vector3 size, Vector2 uv, float inflate, bool mirrorTexture, bool flipZMapping)
: this(true, position, size, uv, inflate, mirrorTexture, flipZMapping)
public CubeMesh(Cube cube) : this(cube, true)
{
}
public CubeMesh(Cube cube)
: this(cube.Position, cube.Size, cube.Uv, cube.Inflate, cube.MirrorTexture, cube.FlipZMapping)
private CubeMesh(Cube cube, bool visible)
: base(nameof(CubeMesh), OpenTK.Graphics.OpenGL.PrimitiveType.Triangles)
{
ShouldRender = visible;
_cube = cube;
indices.AddRange(IndicesData);
transform = Matrix4.Identity;
SetData();
}
public CubeMesh SetCube(Cube cube)
{
_ = cube ?? throw new ArgumentNullException(nameof(cube));
return new CubeMesh(cube, ShouldRender);
}
public CubeMesh SetVisible(bool visible) => new CubeMesh(_cube, visible);
public Cube GetCube() => _cube;
private TextureVertex[] GetCubeVertexData()
{
int mirror = MirrorTexture ? 1 : 0;
int mirror = Convert.ToInt32(_cube.MirrorTexture);
List<TextureVertex> vertices = new List<TextureVertex>();
Vector2 uv = Uv;
Vector2 uv = _cube.Uv;
BoundingBox boundingBox = GetBoundingBox();
BoundingBox boundingBox = _cube.GetBoundingBox();
Vector3 from = boundingBox.Start.ToOpenTKVector();
Vector3 to = boundingBox.End.ToOpenTKVector();
var back = new TextureVertex[]
Vector3 size = _cube.Size;
var back = new TextureVertex[4]
{
// Back
new TextureVertex(new Vector3(from.X, to.Y, to.Z), new Vector2(uv.X + Size.Z * 2 + Size.X + Size.X * (1 - mirror), uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3( to.X, to.Y, to.Z), new Vector2(uv.X + Size.Z * 2 + Size.X + Size.X * mirror, uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3( to.X, from.Y, to.Z), new Vector2(uv.X + Size.Z * 2 + Size.X + Size.X * mirror, uv.Y + Size.Z)),
new TextureVertex(new Vector3(from.X, from.Y, to.Z), new Vector2(uv.X + Size.Z * 2 + Size.X + Size.X * (1 - mirror), uv.Y + Size.Z))
new TextureVertex(new Vector3(from.X, to.Y, to.Z), new Vector2(uv.X + size.Z * 2 + size.X + size.X * (1 - mirror), uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3( to.X, to.Y, to.Z), new Vector2(uv.X + size.Z * 2 + size.X + size.X * mirror, uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3( to.X, from.Y, to.Z), new Vector2(uv.X + size.Z * 2 + size.X + size.X * mirror, uv.Y + size.Z)),
new TextureVertex(new Vector3(from.X, from.Y, to.Z), new Vector2(uv.X + size.Z * 2 + size.X + size.X * (1 - mirror), uv.Y + size.Z))
};
var front = new TextureVertex[]
var front = new TextureVertex[4]
{
// Front
new TextureVertex(new Vector3(from.X, to.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X * mirror, uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3( to.X, to.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X * (1 - mirror), uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3( to.X, from.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X * (1 - mirror), uv.Y + Size.Z)),
new TextureVertex(new Vector3(from.X, from.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X * mirror, uv.Y + Size.Z)),
new TextureVertex(new Vector3(from.X, to.Y, from.Z), new Vector2(uv.X + size.Z + size.X * mirror, uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3( to.X, to.Y, from.Z), new Vector2(uv.X + size.Z + size.X * (1 - mirror), uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3( to.X, from.Y, from.Z), new Vector2(uv.X + size.Z + size.X * (1 - mirror), uv.Y + size.Z)),
new TextureVertex(new Vector3(from.X, from.Y, from.Z), new Vector2(uv.X + size.Z + size.X * mirror, uv.Y + size.Z)),
};
var top = new TextureVertex[]
var top = new TextureVertex[4]
{
// Top
new TextureVertex(new Vector3(from.X, from.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X * mirror, uv.Y + Size.Z)),
new TextureVertex(new Vector3(from.X, from.Y, to.Z), new Vector2(uv.X + Size.Z + Size.X * mirror, uv.Y)),
new TextureVertex(new Vector3( to.X, from.Y, to.Z), new Vector2(uv.X + Size.Z + Size.X * (1 - mirror), uv.Y)),
new TextureVertex(new Vector3( to.X, from.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X * (1 - mirror), uv.Y + Size.Z)),
new TextureVertex(new Vector3(from.X, from.Y, from.Z), new Vector2(uv.X + size.Z + size.X * mirror, uv.Y + size.Z)),
new TextureVertex(new Vector3(from.X, from.Y, to.Z), new Vector2(uv.X + size.Z + size.X * mirror, uv.Y)),
new TextureVertex(new Vector3( to.X, from.Y, to.Z), new Vector2(uv.X + size.Z + size.X * (1 - mirror), uv.Y)),
new TextureVertex(new Vector3( to.X, from.Y, from.Z), new Vector2(uv.X + size.Z + size.X * (1 - mirror), uv.Y + size.Z)),
};
var bottom = new TextureVertex[]
var bottom = new TextureVertex[4]
{
// Bottom
new TextureVertex(new Vector3( to.X, to.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X + Size.X * (1 - mirror), uv.Y + (FlipZMapping ? Size.Z : 0))),
new TextureVertex(new Vector3( to.X, to.Y, to.Z), new Vector2(uv.X + Size.Z + Size.X + Size.X * (1 - mirror), uv.Y + (!FlipZMapping ? Size.Z : 0))),
new TextureVertex(new Vector3(from.X, to.Y, to.Z), new Vector2(uv.X + Size.Z + Size.X + Size.X * mirror, uv.Y + (!FlipZMapping ? Size.Z : 0))),
new TextureVertex(new Vector3(from.X, to.Y, from.Z), new Vector2(uv.X + Size.Z + Size.X + Size.X * mirror, uv.Y + (FlipZMapping ? Size.Z : 0))),
new TextureVertex(new Vector3( to.X, to.Y, from.Z), new Vector2(uv.X + size.Z + size.X + size.X * (1 - mirror), uv.Y + (_cube.FlipZMapping ? size.Z : 0))),
new TextureVertex(new Vector3( to.X, to.Y, to.Z), new Vector2(uv.X + size.Z + size.X + size.X * (1 - mirror), uv.Y + (!_cube.FlipZMapping ? size.Z : 0))),
new TextureVertex(new Vector3(from.X, to.Y, to.Z), new Vector2(uv.X + size.Z + size.X + size.X * mirror, uv.Y + (!_cube.FlipZMapping ? size.Z : 0))),
new TextureVertex(new Vector3(from.X, to.Y, from.Z), new Vector2(uv.X + size.Z + size.X + size.X * mirror, uv.Y + (_cube.FlipZMapping ? size.Z : 0))),
};
var left = new TextureVertex[]
var left = new TextureVertex[4]
{
// Left
new TextureVertex(new Vector3(MirrorTexture ? from.X : to.X, from.Y, from.Z), new Vector2(uv.X + Size.X + Size.Z, uv.Y + Size.Z)),
new TextureVertex(new Vector3(MirrorTexture ? from.X : to.X, to.Y , from.Z), new Vector2(uv.X + Size.X + Size.Z, uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3(MirrorTexture ? from.X : to.X, to.Y , to.Z), new Vector2(uv.X + Size.X + Size.Z * 2, uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3(MirrorTexture ? from.X : to.X, from.Y, to.Z), new Vector2(uv.X + Size.X + Size.Z * 2, uv.Y + Size.Z)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? from.X : to.X, from.Y, from.Z), new Vector2(uv.X + size.X + size.Z, uv.Y + size.Z)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? from.X : to.X, to.Y , from.Z), new Vector2(uv.X + size.X + size.Z, uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? from.X : to.X, to.Y , to.Z), new Vector2(uv.X + size.X + size.Z * 2, uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? from.X : to.X, from.Y, to.Z), new Vector2(uv.X + size.X + size.Z * 2, uv.Y + size.Z)),
};
var right = new TextureVertex[]
var right = new TextureVertex[4]
{
// Right
new TextureVertex(new Vector3(MirrorTexture ? to.X : from.X, from.Y, from.Z), new Vector2(uv.X + Size.Z, uv.Y + Size.Z)),
new TextureVertex(new Vector3(MirrorTexture ? to.X : from.X, to.Y, from.Z), new Vector2(uv.X + Size.Z, uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3(MirrorTexture ? to.X : from.X, to.Y, to.Z), new Vector2(uv.X, uv.Y + Size.Z + Size.Y)),
new TextureVertex(new Vector3(MirrorTexture ? to.X : from.X, from.Y, to.Z), new Vector2(uv.X, uv.Y + Size.Z)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? to.X : from.X, from.Y, from.Z), new Vector2(uv.X + size.Z, uv.Y + size.Z)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? to.X : from.X, to.Y , from.Z), new Vector2(uv.X + size.Z, uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? to.X : from.X, to.Y , to.Z), new Vector2(uv.X, uv.Y + size.Z + size.Y)),
new TextureVertex(new Vector3(_cube.MirrorTexture ? to.X : from.X, from.Y, to.Z), new Vector2(uv.X, uv.Y + size.Z)),
};
vertices.AddRange(back);
@@ -147,24 +149,10 @@ namespace PckStudio.Rendering
return vertices.ToArray();
}
internal void Update()
internal override void SetData()
{
vertices = GetCubeVertexData();
}
internal TextureVertex[] GetVertices()
{
return vertices;
}
internal int[] GetIndices()
{
return indicesData;
}
internal static CubeMesh Create(SkinBOX skinBox)
{
return new CubeMesh(FromSkinBox(skinBox));
vertices.Clear();
vertices.AddRange(GetCubeVertexData());
}
}
}

View File

@@ -26,7 +26,7 @@ using PckStudio.Internal.Skin;
namespace PckStudio.Rendering
{
internal class CubeGroupMesh : GenericMesh<TextureVertex>
internal class CubeMeshCollection : GenericMesh<TextureVertex>
{
private List<CubeMesh> cubes;
@@ -36,26 +36,43 @@ namespace PckStudio.Rendering
set
{
_flipZMapping = value;
UpdateCollection();
//foreach (Cube cube in cubes)
//{
// cube.FlipZMapping = FlipZMapping;
//}
}
}
public Vector3 Translation { get; }
public Vector3 Pivot { get; }
public Vector3 Offset { get; set; } = Vector3.Zero;
private Vector3 _offset { get; set; } = Vector3.Zero;
public Vector3 Offset
{
get => _offset;
set
{
if (value != _offset)
{
_offset = value;
transform = Matrix4.CreateTranslation(Translation + _offset) * Matrix4.CreateScale(1f, -1f, -1f);
}
}
}
private bool _flipZMapping = false;
internal CubeGroupMesh(string name) : base(name, PrimitiveType.Triangles)
internal CubeMeshCollection(string name) : base(name, PrimitiveType.Triangles)
{
cubes = new List<CubeMesh>(5);
transform = Matrix4.CreateTranslation(Vector3.Zero) * Matrix4.CreateScale(1f, -1f, -1f);
}
internal CubeGroupMesh(string name, Vector3 translation, Vector3 pivot)
internal CubeMeshCollection(string name, Vector3 translation, Vector3 pivot)
: this(name)
{
Translation = translation;
Pivot = pivot;
transform = Matrix4.CreateTranslation(Translation) * Matrix4.CreateScale(1f, -1f, -1f);
}
public static VertexBufferLayout GetLayout()
@@ -68,10 +85,14 @@ namespace PckStudio.Rendering
internal void AddSkinBox(SkinBOX skinBox, float inflate = 0f)
{
var cube = CubeMesh.Create(skinBox);
cube.FlipZMapping = FlipZMapping;
cube.Inflate += inflate;
cubes.Add(cube);
var cube = Cube.FromSkinBox(skinBox, inflate, FlipZMapping);
cubes.Add(new CubeMesh(cube));
}
private void ResetBuffers()
{
vertices.Clear();
indices.Clear();
}
internal void ClearData()
@@ -83,18 +104,17 @@ namespace PckStudio.Rendering
/// <summary>
/// Uploads vertex data
/// </summary>
// TODO: rename function
internal void UploadData()
internal override void SetData()
{
ResetBuffers();
int indicesOffset = 0;
IEnumerable<int> indexStorage = CubeMesh.IndicesData;
foreach (CubeMesh cube in cubes)
{
if (!cube.ShouldRender)
continue;
cube.Update();
TextureVertex[] cubeVertices = cube.GetVertices();
TextureVertex[] cubeVertices = cube.GetVertices().ToArray();
vertices.AddRange(cubeVertices);
var indexStorage = cube.GetIndices();
indices.AddRange(indexStorage.Select(n => n + indicesOffset));
indicesOffset += cubeVertices.Length;
}
@@ -103,8 +123,8 @@ namespace PckStudio.Rendering
internal void AddCube(Vector3 position, Vector3 size, Vector2 uv, float inflate = 0f, bool mirrorTexture = false)
{
var cube = new CubeMesh(position, size, uv, inflate, mirrorTexture, FlipZMapping);
cubes.Add(cube);
var cube = new Cube(position, size, uv, inflate, mirrorTexture, FlipZMapping);
cubes.Add(new CubeMesh(cube));
}
internal void RemoveCube(int index)
@@ -113,7 +133,7 @@ namespace PckStudio.Rendering
throw new IndexOutOfRangeException();
cubes.RemoveAt(index);
UploadData();
SetData();
}
internal void ReplaceCube(int index, Vector3 position, Vector3 size, Vector2 uv, float inflate = 0f, bool mirrorTexture = false)
@@ -121,26 +141,7 @@ namespace PckStudio.Rendering
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
CubeMesh cube = cubes[index];
cube.Position = position;
cube.Size = size;
cube.Uv = uv;
cube.Inflate = inflate;
cube.MirrorTexture = mirrorTexture;
cube.Update();
}
private void UpdateCollection()
{
foreach (CubeMesh cube in cubes)
{
cube.FlipZMapping = FlipZMapping;
}
}
internal Matrix4 Transform
{
get => Matrix4.CreateTranslation(Translation + Offset) * Matrix4.CreateScale(1f, -1f, -1f);
cubes[index] = cubes[index].SetCube(new Cube(position, size, uv, inflate, mirrorTexture, false));
}
internal Vector3 GetCenter(int index)
@@ -148,7 +149,7 @@ namespace PckStudio.Rendering
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
return (Transform * Matrix4.CreateTranslation(cubes[index].Center)).ExtractTranslation();
return Vector3.TransformPosition(cubes[index].Center, Transform);
}
internal BoundingBox GetCubeBoundingBox(int index)
@@ -156,7 +157,7 @@ namespace PckStudio.Rendering
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
CubeMesh cube = cubes[index];
Cube cube = cubes[index].GetCube();
return cube.GetBoundingBox(Transform);
}
@@ -164,15 +165,16 @@ namespace PckStudio.Rendering
{
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
return Vector3.TransformPosition(cubes[index].GetFaceCenter(face), Transform);
Cube cube = cubes[index].GetCube();
return Vector3.TransformPosition(cube.GetFaceCenter(face), Transform);
}
internal void SetEnabled(int index, bool enable)
internal void SetVisible(int index, bool visible)
{
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
cubes[index].ShouldRender = enable;
cubes[index] = cubes[index].SetVisible(visible);
}
}
}

View File

@@ -18,30 +18,32 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using PckStudio.Rendering.Shader;
namespace PckStudio.Rendering
{
internal class GenericMesh<T> where T : struct
internal abstract class GenericMesh<T> where T : struct
{
internal string Name { get; }
protected List<T> vertices { get; }
protected List<int> indices { get; }
protected int indicesOffset;
protected Matrix4 transform { get; set; }
public Matrix4 Transform => transform;
private VertexArray vertexArray;
private IndexBuffer indexBuffer;
private readonly PrimitiveType drawType;
private DrawContext drawContext;
/* + ---------------------------------------------- + */
/* | */ private VertexArray vertexArray; /* | */
/* | */ private IndexBuffer indexBuffer; /* | */
/* | */ private readonly PrimitiveType drawType; /* | */
/* | */ private DrawContext drawContext; /* | */
/* + ---------------------------------------------- + */
protected GenericMesh(string name, PrimitiveType type)
{
Name = name;
drawType = type;
indicesOffset = 0;
vertices = new List<T>(10);
indices = new List<int>(10);
}
@@ -54,12 +56,9 @@ namespace PckStudio.Rendering
drawContext = new DrawContext(vertexArray, indexBuffer, drawType);
}
protected void ResetBuffers()
{
indicesOffset = 0;
vertices.Clear();
indices.Clear();
}
internal abstract void SetData();
internal IEnumerable<T> GetVertices() => vertices;
protected void Submit()
{

View File

@@ -40,6 +40,7 @@ namespace PckStudio.Rendering
if (_size < size)
{
GL.BufferData(BufferTarget.ElementArrayBuffer, size, indicies, BufferUsageHint.StaticDraw);
_size = size;
return;
}
GL.BufferSubData(BufferTarget.ElementArrayBuffer, IntPtr.Zero, size, indicies);

View File

@@ -222,17 +222,17 @@ namespace PckStudio.Rendering
private DrawContext _skyboxRenderBuffer;
private CubeTexture _skyboxTexture;
private Dictionary<string, CubeGroupMesh> meshStorage;
private Dictionary<string, CubeGroupMesh> offsetSpecificMeshStorage;
private Dictionary<string, CubeMeshCollection> meshStorage;
private Dictionary<string, CubeMeshCollection> offsetSpecificMeshStorage;
private CubeGroupMesh cape;
private CubeMeshCollection cape;
private CubeGroupMesh head;
private CubeGroupMesh body;
private CubeGroupMesh rightArm;
private CubeGroupMesh leftArm;
private CubeGroupMesh rightLeg;
private CubeGroupMesh leftLeg;
private CubeMeshCollection head;
private CubeMeshCollection body;
private CubeMeshCollection rightArm;
private CubeMeshCollection leftArm;
private CubeMeshCollection rightLeg;
private CubeMeshCollection leftLeg;
private float animationCurrentRotationAngle;
private float animationRotationSpeed = 16f;
@@ -266,7 +266,7 @@ namespace PckStudio.Rendering
{
InitializeSkinData();
InitializeCapeData();
meshStorage = new Dictionary<string, CubeGroupMesh>()
meshStorage = new Dictionary<string, CubeMeshCollection>()
{
{ "HEAD", head },
{ "BODY", body },
@@ -301,19 +301,19 @@ namespace PckStudio.Rendering
MakeCurrent();
InitializeShaders();
Renderer.SetClearColor(BackColor);
VertexBufferLayout layout = CubeGroupMesh.GetLayout();
foreach (CubeGroupMesh cubeMesh in meshStorage?.Values)
VertexBufferLayout layout = CubeMeshCollection.GetLayout();
foreach (CubeMeshCollection cubeMesh in meshStorage?.Values)
{
cubeMesh.Initialize(layout);
}
UploadMeshData();
foreach (CubeGroupMesh cubeMesh in offsetSpecificMeshStorage?.Values)
foreach (CubeMeshCollection cubeMesh in offsetSpecificMeshStorage?.Values)
{
cubeMesh.Initialize(layout);
cubeMesh.UploadData();
cubeMesh.SetData();
}
cape.Initialize(layout);
cape.UploadData();
cape.SetData();
GLErrorCheck();
base.Initialize();
GLErrorCheck();
@@ -329,38 +329,38 @@ namespace PckStudio.Rendering
private void InitializeSkinData()
{
head ??= new CubeGroupMesh("Head") { FlipZMapping = true };
head ??= new CubeMeshCollection("Head") { FlipZMapping = true };
head.AddCube(new(-4, -8, -4), new(8, 8, 8), new(0, 0));
head.AddCube(new(-4, -8, -4), new(8, 8, 8), new(32, 0), OverlayScale * 2);
body ??= new CubeGroupMesh("Body");
body ??= new CubeMeshCollection("Body");
body.AddCube(new(-4, 0, -2), new(8, 12, 4), new(16, 16));
body.AddCube(new(-4, 0, -2), new(8, 12, 4), new(16, 32), OverlayScale);
ModelPartSpecifics.PositioningInfo rightArmInfo = ModelPartSpecifics.GetPositioningInfo("ARM0");
rightArm ??= new CubeGroupMesh("Right Arm", rightArmInfo.Translation.ToOpenTKVector(), rightArmInfo.Pivot.ToOpenTKVector());
rightArm ??= new CubeMeshCollection("Right Arm", rightArmInfo.Translation.ToOpenTKVector(), rightArmInfo.Pivot.ToOpenTKVector());
rightArm.AddCube(new(-3, -2, -2), new(4, 12, 4), new(40, 16));
rightArm.AddCube(new(-3, -2, -2), new(4, 12, 4), new(40, 32), OverlayScale);
ModelPartSpecifics.PositioningInfo leftArmInfo = ModelPartSpecifics.GetPositioningInfo("ARM1");
leftArm ??= new CubeGroupMesh("Left Arm", leftArmInfo.Translation.ToOpenTKVector(), leftArmInfo.Pivot.ToOpenTKVector());
leftArm ??= new CubeMeshCollection("Left Arm", leftArmInfo.Translation.ToOpenTKVector(), leftArmInfo.Pivot.ToOpenTKVector());
leftArm.AddCube(new(-1, -2, -2), new(4, 12, 4), new(32, 48));
leftArm.AddCube(new(-1, -2, -2), new(4, 12, 4), new(48, 48), inflate: OverlayScale);
ModelPartSpecifics.PositioningInfo rightLegInfo = ModelPartSpecifics.GetPositioningInfo("LEG0");
rightLeg ??= new CubeGroupMesh("Right Leg", rightLegInfo.Translation.ToOpenTKVector(), rightLegInfo.Pivot.ToOpenTKVector());
rightLeg ??= new CubeMeshCollection("Right Leg", rightLegInfo.Translation.ToOpenTKVector(), rightLegInfo.Pivot.ToOpenTKVector());
rightLeg.AddCube(new(-2, 0, -2), new(4, 12, 4), new(0, 16));
rightLeg.AddCube(new(-2, 0, -2), new(4, 12, 4), new(0, 32), OverlayScale);
ModelPartSpecifics.PositioningInfo leftLegInfo = ModelPartSpecifics.GetPositioningInfo("LEG1");
leftLeg ??= new CubeGroupMesh("Left Leg", leftLegInfo.Translation.ToOpenTKVector(), leftLegInfo.Pivot.ToOpenTKVector());
leftLeg ??= new CubeMeshCollection("Left Leg", leftLegInfo.Translation.ToOpenTKVector(), leftLegInfo.Pivot.ToOpenTKVector());
leftLeg.AddCube(new(-2, 0, -2), new(4, 12, 4), new(16, 48));
leftLeg.AddCube(new(-2, 0, -2), new(4, 12, 4), new(0, 48), OverlayScale);
}
private void InitializeCapeData()
{
cape ??= new CubeGroupMesh("Cape");
cape ??= new CubeMeshCollection("Cape");
cape.AddCube(new(-5, 0, -3), new(10, 16, 1), new(0, 0));
}
@@ -368,34 +368,34 @@ namespace PckStudio.Rendering
{
const float armorInflation = 0.75f;
var helmet = new CubeGroupMesh("HELMET");
var helmet = new CubeMeshCollection("HELMET");
helmet.AddCube(new(-4, -8, -4), new(8, 8, 8), new(0, 0), inflate: armorInflation);
var chest = new CubeGroupMesh("CHEST");
var chest = new CubeMeshCollection("CHEST");
chest.AddCube(new(-4, 0, -2), new(8, 12, 4), new(16, 16), inflate: armorInflation + 0.01f);
var shoulder0 = new CubeGroupMesh("SHOULDER0", rightArm.Translation, rightArm.Pivot);
var shoulder0 = new CubeMeshCollection("SHOULDER0", rightArm.Translation, rightArm.Pivot);
shoulder0.AddCube(new(-3, -2, -2), new(4, 12, 4), new(40, 16), inflate: armorInflation);
var shoulder1 = new CubeGroupMesh("SHOULDER1", leftArm.Translation, leftArm.Pivot);
var shoulder1 = new CubeMeshCollection("SHOULDER1", leftArm.Translation, leftArm.Pivot);
shoulder1.AddCube(new(-1, -2, -2), new(4, 12, 4), new(40, 16), inflate: armorInflation, mirrorTexture: true);
var waist = new CubeGroupMesh("WAIST");
var waist = new CubeMeshCollection("WAIST");
waist.AddCube(new(-4, 0, -2), new(8, 12, 4), new(16, 48), inflate: armorInflation);
var pants0 = new CubeGroupMesh("PANTS0", rightLeg.Translation, rightLeg.Pivot);
var pants0 = new CubeMeshCollection("PANTS0", rightLeg.Translation, rightLeg.Pivot);
pants0.AddCube(new(-2, 0, -2), new(4, 12, 4), new(0, 48), inflate: armorInflation);
var pants1 = new CubeGroupMesh("PANTS1", leftLeg.Translation, leftLeg.Pivot);
var pants1 = new CubeMeshCollection("PANTS1", leftLeg.Translation, leftLeg.Pivot);
pants1.AddCube(new(-2, 0, -2), new(4, 12, 4), new(0, 48), inflate: armorInflation, mirrorTexture: true);
var boot0 = new CubeGroupMesh("BOOT0", rightLeg.Translation, rightLeg.Pivot);
var boot0 = new CubeMeshCollection("BOOT0", rightLeg.Translation, rightLeg.Pivot);
boot0.AddCube(new(-2, 0, -2), new(4, 12, 4), new(0, 16), inflate: armorInflation + 0.25f);
var boot1 = new CubeGroupMesh("BOOT1", leftLeg.Translation, leftLeg.Pivot);
var boot1 = new CubeMeshCollection("BOOT1", leftLeg.Translation, leftLeg.Pivot);
boot1.AddCube(new(-2, 0, -2), new(4, 12, 4), new(0, 16), inflate: armorInflation + 0.25f, mirrorTexture: true);
offsetSpecificMeshStorage = new Dictionary<string, CubeGroupMesh>
offsetSpecificMeshStorage = new Dictionary<string, CubeMeshCollection>
{
{ helmet.Name, helmet },
{ chest.Name, chest },
@@ -649,7 +649,7 @@ namespace PckStudio.Rendering
{
if (!meshStorage.ContainsKey(name))
return;
meshStorage[name]?.UploadData();
meshStorage[name]?.SetData();
}
private void UploadMeshData()
@@ -710,12 +710,12 @@ namespace PckStudio.Rendering
internal IEnumerable<SkinPartOffset> GetOffsets()
{
foreach (KeyValuePair<string, CubeGroupMesh> mesh in meshStorage)
foreach (KeyValuePair<string, CubeMeshCollection> mesh in meshStorage)
{
if (SkinPartOffset.ValidModelOffsetTypes.Contains(mesh.Key) && mesh.Value.Offset.Y != 0f)
yield return new SkinPartOffset(mesh.Key, mesh.Value.Offset.Y);
}
foreach (KeyValuePair<string, CubeGroupMesh> offsetmesh in offsetSpecificMeshStorage)
foreach (KeyValuePair<string, CubeMeshCollection> offsetmesh in offsetSpecificMeshStorage)
{
if (offsetmesh.Value.Offset.Y != 0f)
yield return new SkinPartOffset(offsetmesh.Key, offsetmesh.Value.Offset.Y);
@@ -757,20 +757,20 @@ namespace PckStudio.Rendering
return;
}
CubeGroupMesh cubeMesh = meshStorage[skinBox.Type];
CubeMeshCollection cubeMesh = meshStorage[skinBox.Type];
cubeMesh.AddSkinBox(skinBox, autoInflateOverlayParts && skinBox.IsOverlayPart() ? skinBox.Type == "HEADWEAR" ? OverlayScale * 2 : OverlayScale : 0f);
}
private void OnANIMUpdate()
{
head.SetEnabled(0, !ANIM.GetFlag(SkinAnimFlag.HEAD_DISABLED));
head.SetEnabled(1, !ANIM.GetFlag(SkinAnimFlag.HEAD_OVERLAY_DISABLED));
head.SetVisible(0, !ANIM.GetFlag(SkinAnimFlag.HEAD_DISABLED));
head.SetVisible(1, !ANIM.GetFlag(SkinAnimFlag.HEAD_OVERLAY_DISABLED));
body.SetEnabled(0, !ANIM.GetFlag(SkinAnimFlag.BODY_DISABLED));
rightArm.SetEnabled(0, !ANIM.GetFlag(SkinAnimFlag.RIGHT_ARM_DISABLED));
leftArm.SetEnabled(0, !ANIM.GetFlag(SkinAnimFlag.LEFT_ARM_DISABLED));
rightLeg.SetEnabled(0, !ANIM.GetFlag(SkinAnimFlag.RIGHT_LEG_DISABLED));
leftLeg.SetEnabled(0, !ANIM.GetFlag(SkinAnimFlag.LEFT_LEG_DISABLED));
body.SetVisible(0, !ANIM.GetFlag(SkinAnimFlag.BODY_DISABLED));
rightArm.SetVisible(0, !ANIM.GetFlag(SkinAnimFlag.RIGHT_ARM_DISABLED));
leftArm.SetVisible(0, !ANIM.GetFlag(SkinAnimFlag.LEFT_ARM_DISABLED));
rightLeg.SetVisible(0, !ANIM.GetFlag(SkinAnimFlag.RIGHT_LEG_DISABLED));
leftLeg.SetVisible(0, !ANIM.GetFlag(SkinAnimFlag.LEFT_LEG_DISABLED));
bool slim = ANIM.GetFlag(SkinAnimFlag.SLIM_MODEL);
@@ -778,11 +778,11 @@ namespace PckStudio.Rendering
if (slim || ANIM.GetFlag(SkinAnimFlag.RESOLUTION_64x64))
{
TextureSize = new Size(64, 64);
body.SetEnabled(1, !ANIM.GetFlag(SkinAnimFlag.BODY_OVERLAY_DISABLED));
rightArm.SetEnabled(1, !ANIM.GetFlag(SkinAnimFlag.RIGHT_ARM_OVERLAY_DISABLED));
leftArm.SetEnabled(1, !ANIM.GetFlag(SkinAnimFlag.LEFT_ARM_OVERLAY_DISABLED));
rightLeg.SetEnabled(1, !ANIM.GetFlag(SkinAnimFlag.RIGHT_LEG_OVERLAY_DISABLED));
leftLeg.SetEnabled(1, !ANIM.GetFlag(SkinAnimFlag.LEFT_LEG_OVERLAY_DISABLED));
body.SetVisible(1, !ANIM.GetFlag(SkinAnimFlag.BODY_OVERLAY_DISABLED));
rightArm.SetVisible(1, !ANIM.GetFlag(SkinAnimFlag.RIGHT_ARM_OVERLAY_DISABLED));
leftArm.SetVisible(1, !ANIM.GetFlag(SkinAnimFlag.LEFT_ARM_OVERLAY_DISABLED));
rightLeg.SetVisible(1, !ANIM.GetFlag(SkinAnimFlag.RIGHT_LEG_OVERLAY_DISABLED));
leftLeg.SetVisible(1, !ANIM.GetFlag(SkinAnimFlag.LEFT_LEG_OVERLAY_DISABLED));
int slimValue = slim ? 3 : 4;
rightArm.ReplaceCube(0, new(slim ? -2 : -3, -2, -2), new(slimValue, 12, 4), new(40, 16));
@@ -798,19 +798,19 @@ namespace PckStudio.Rendering
TextureSize = new Size(64, 32);
body.SetEnabled(1, false);
body.SetVisible(1, false);
head.FlipZMapping = false;
rightArm.ReplaceCube(0, new(-3, -2, -2), new(4, 12, 4), new(40, 16));
rightArm.SetEnabled(1, false);
rightArm.SetVisible(1, false);
leftArm.ReplaceCube(0, new(-1, -2, -2), new(4, 12, 4), new(40, 16), mirrorTexture: true);
leftArm.SetEnabled(1, false);
leftArm.SetVisible(1, false);
rightLeg.ReplaceCube(0, new(-2, 0, -2), new(4, 12, 4), new(0, 16));
rightLeg.SetEnabled(1, false);
rightLeg.SetVisible(1, false);
leftLeg.ReplaceCube (0, new(-2, 0, -2), new(4, 12, 4), new(0, 16), mirrorTexture: true);
leftLeg.SetEnabled(1, false);
leftLeg.SetVisible(1, false);
}
protected override bool ProcessDialogKey(Keys keyData)
@@ -851,7 +851,7 @@ namespace PckStudio.Rendering
return;
}
CubeGroupMesh cubeMesh = meshStorage[skinBox.Type];
CubeMeshCollection cubeMesh = meshStorage[skinBox.Type];
Vector3 center = Cube.FromSkinBox(skinBox).Center;
Matrix4 camMat = (Matrix4.CreateTranslation(cubeMesh.Translation) * Matrix4.CreateTranslation(center) * Matrix4.CreateScale(-1, 1, 1));
Vector3 camPos = camMat.ExtractTranslation();
@@ -1025,7 +1025,7 @@ namespace PckStudio.Rendering
if (ShowGuideLines)
{
GL.DepthFunc(DepthFunction.Always);
GL.BlendFunc(BlendingFactor.DstAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
lineShader.Bind();
lineShader.SetUniformMat4("ViewProjection", ref viewProjection);
lineShader.SetUniformMat4("Transform", ref transform);
@@ -1034,7 +1034,6 @@ namespace PckStudio.Rendering
Renderer.SetLineWidth(2.5f);
Renderer.Draw(lineShader, GetGuidelineDrawContext());
Renderer.SetLineWidth(1f);
GL.BlendFunc(BlendingFactor.DstAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.DepthFunc(DepthFunction.Less);
}
@@ -1042,15 +1041,14 @@ namespace PckStudio.Rendering
{
SkinBOX box = ModelData[SelectedIndex];
Cube cube = Cube.FromSkinBox(box);
float inflate = autoInflateOverlayParts && box.IsOverlayPart() ? box.Type == "HEADWEAR" ? OverlayScale * 2 : OverlayScale : 0f;
Cube cube = Cube.FromSkinBox(box, inflate);
cube.Inflate = autoInflateOverlayParts && box.IsOverlayPart() ? box.Type == "HEADWEAR" ? OverlayScale * 2 : OverlayScale : 0f;
BoundingBox cubeBoundingBox = cube.GetBoundingBox();
if (meshStorage.ContainsKey(box.Type))
{
CubeGroupMesh cubeMesh = meshStorage[box.Type];
CubeMeshCollection cubeMesh = meshStorage[box.Type];
Matrix4 GetGroupTransform(string type)
{
@@ -1076,7 +1074,7 @@ namespace PckStudio.Rendering
Matrix4 bbTransform = GetGroupTransform(box.Type);
bbTransform *= cubeMesh.Transform;
bbTransform *= transform;
GL.BlendFunc(BlendingFactor.DstAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.SrcColor);
DrawBoundingBox(bbTransform, cubeBoundingBox, HighlightlingColor);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
}
@@ -1174,7 +1172,7 @@ namespace PckStudio.Rendering
}
}
private void RenderPart(ShaderProgram shader, CubeGroupMesh cubeMesh, Matrix4 partMatrix, Matrix4 globalMatrix)
private void RenderPart(ShaderProgram shader, CubeMeshCollection cubeMesh, Matrix4 partMatrix, Matrix4 globalMatrix)
{
Matrix4 transform = partMatrix;
transform *= cubeMesh.Transform;
@@ -1198,7 +1196,7 @@ namespace PckStudio.Rendering
private void ReInitialzeSkinData()
{
foreach (CubeGroupMesh mesh in meshStorage.Values)
foreach (CubeMeshCollection mesh in meshStorage.Values)
{
mesh.ClearData();
}