Skip to content

Commit

Permalink
Vif update, vertex normals and importer update
Browse files Browse the repository at this point in the history
  • Loading branch information
osdanova committed Sep 19, 2023
1 parent 17c0621 commit c4b906d
Show file tree
Hide file tree
Showing 15 changed files with 565 additions and 101 deletions.
10 changes: 10 additions & 0 deletions OpenKh.AssimpUtils/Kh2MdlxAssimp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ public static Assimp.Scene getAssimpScene(ModelSkeletal model)
iMesh.VertexColorChannels[0].Add(new Assimp.Color4D(R, G, B, A));
}

// NORMALS
if (vertex.Normal != null)
{
iMesh.Normals.Add(new Assimp.Vector3D(vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z));
}

currentVertex++;
}

Expand Down Expand Up @@ -305,6 +311,10 @@ public static VifMesh getVifMeshFromAssimp(Assimp.Mesh mesh, Matrix4x4[] boneMat
byte A = (byte)(mesh.VertexColorChannels[0][i].A * 255);
vertex.Color = new VifCommon.VertexColor(R, G, B, A);
}
if(mesh.HasNormals)
{
vertex.Normal = new VifCommon.VertexNormal(mesh.Normals[i].X, mesh.Normals[i].Y, mesh.Normals[i].Z);
}

verticesAssimpOrder.Add(vertex);
}
Expand Down
4 changes: 3 additions & 1 deletion OpenKh.Kh2/Models/ModelCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,17 @@ public class UVBVertex
public float U { get; set; }
public float V { get; set; }
public VifCommon.VertexColor Color { get; set; }
public VifCommon.VertexNormal Normal { get; set; }

public UVBVertex() { }
public UVBVertex(List<BPosition> BonePositions, float U = 0, float V = 0, Vector3 position = new Vector3(), VifCommon.VertexColor color = null)
public UVBVertex(List<BPosition> BonePositions, float U = 0, float V = 0, Vector3 position = new Vector3(), VifCommon.VertexColor color = null, VifCommon.VertexNormal normal = null)
{
this.BPositions = BonePositions;
this.U = U;
this.V = V;
Position = position;
Color = color;
Normal = normal;
}

public override string ToString()
Expand Down
25 changes: 17 additions & 8 deletions OpenKh.Kh2/Models/ModelSkeletal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ public static SkeletalMesh GetSkeletalMeshFromVpuGroup(VpuGroup vpuGroup, Matrix
OpenKh.Ps2.VpuPacket.VertexIndex index = vpuGroup.Indices[i];
UVBVertex vertex = vpuGroup.Vertices[index.Index];

if(index.Color != null)
{
mesh.Vertices.Add(new UVBVertex(vertex.BPositions, index.U, index.V, ModelCommon.getAbsolutePosition(vertex.BPositions, boneMatrices), new VifCommon.VertexColor((byte)index.Color.R, (byte)index.Color.G, (byte)index.Color.B, (byte)index.Color.A)));
}
else
{
mesh.Vertices.Add(new UVBVertex(vertex.BPositions, index.U, index.V, ModelCommon.getAbsolutePosition(vertex.BPositions, boneMatrices)));
}
UVBVertex completeVertex = new UVBVertex(vertex.BPositions, index.U, index.V, ModelCommon.getAbsolutePosition(vertex.BPositions, boneMatrices));

if (index.Color != null)
completeVertex.Color = new VifCommon.VertexColor((byte)index.Color.R, (byte)index.Color.G, (byte)index.Color.B, (byte)index.Color.A);
if (index.Normal != null)
completeVertex.Normal = new VifCommon.VertexNormal(index.Normal.X, index.Normal.Y, index.Normal.Z);

mesh.Vertices.Add(completeVertex);

// Triangles
if (index.Function == OpenKh.Ps2.VpuPacket.VertexFunction.DrawTriangle ||
Expand Down Expand Up @@ -469,6 +469,15 @@ public static VpuGroup getVpuGroup(List<OpenKh.Ps2.VpuPacket> vpuPackets, List<D
}
}

// Normals
if (vpuPacket.Indices.Length == vpuPacket.Normals.Length)
{
for (int j = 0; j < vpuPacket.Indices.Length; j++)
{
vpuGroup.Indices[previousIndexCount + j].Normal = vpuPacket.Normals[j];
}
}

indexCount = vpuGroup.Vertices.Count;
}

Expand Down
13 changes: 10 additions & 3 deletions OpenKh.Kh2/Models/VIF/DmaVifPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,34 @@ public DmaVifPacket()
BoneList = new List<int>();
}

public void calcHeader(VifPacket vifPacket, bool singleWeightMode = false)
public void calcHeader(VifPacket vifPacket, bool singleWeightMode = false, bool hasColors = false, bool hasNormals = false)
{
Header.Type = 1;
Header.VertexColorPtrInc = 0;
Header.MagicNumber = 0;
Header.VertexBufferPointer = 0;

int currentAddress = 0;
Header.TriStripNodeOffset = 4;
Header.TriStripNodeOffset = hasNormals ? 5 : 4;
Header.TriStripNodeCount = vifPacket.UvCoords.Count;

currentAddress += Header.TriStripNodeOffset;
currentAddress += Header.TriStripNodeCount;

if(vifPacket.Colors != null && vifPacket.Colors.Count > 0)
if(hasColors)
{
Header.ColorOffset = currentAddress;
Header.ColorCount = vifPacket.Colors.Count;
currentAddress += Header.ColorCount;
}

if (hasNormals)
{
Header.NormalOffset = currentAddress;
Header.NormalCount = vifPacket.Normals.Count;
currentAddress += Header.NormalCount;
}

Header.VertexCoordOffset = currentAddress;
//Header.VertexCoordCount = vifPacket.PositionCoordsVector.Count;
Header.VertexCoordCount = vifPacket.PositionCoords.Count;
Expand Down
16 changes: 16 additions & 0 deletions OpenKh.Kh2/Models/VIF/VifCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class VifVertex
public UVCoord UvCoord;
public byte TriFlag;
public VertexColor Color;
public VertexNormal Normal;
public Vector3 AbsolutePosition;
public List<BoneRelativePosition> RelativePositions; // WeightGroup

Expand All @@ -48,6 +49,7 @@ public int worstCaseScenarioVpuSize()
vpuSize += (4 * RelativePositions.Count); // Bone matrices

if(Color != null) vpuSize += 1; // Color
if(Normal != null) vpuSize += 1; // Normal

return vpuSize;
}
Expand Down Expand Up @@ -171,6 +173,20 @@ public VertexColor(byte R, byte G, byte B, byte A)
}
}

public class VertexNormal
{
public float X;
public float Y;
public float Z;

public VertexNormal(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
}

// A position in space relative to a bone instead of 0,0,0
public class BoneRelativePosition
{
Expand Down
5 changes: 4 additions & 1 deletion OpenKh.Kh2/Models/VIF/VifPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class VifPacket
public List<int> BoneCounts;
public List<List<List<int>>> WeightGroups; // List by weight count (Eg: 1 weight, 2 weights, 3 weights...) - list by entries (Eg: Weight group for vertex 1, WG for vertex 2...) - list of entry's weights (Eg: This weight group has coords 1, 2 & 3)
public List<VifCommon.VertexColor> Colors;
public List<VifCommon.VertexNormal> Normals;
public Dictionary<int, List<WeightGroup>> WeightGroupsByWeightCount;

public VifPacket()
Expand All @@ -30,6 +31,7 @@ public VifPacket()
BoneCounts = new List<int>();
WeightGroups = new List<List<List<int>>>();
Colors = new List<VifCommon.VertexColor>();
Normals = new List<VifCommon.VertexNormal>();
WeightGroupsByWeightCount = new Dictionary<int, List<WeightGroup>>();
}

Expand Down Expand Up @@ -86,7 +88,8 @@ public int getPacketLengthInVPU()
packetLength += VifUtils.getAmountIfGroupedBy(WeightGroups[i].Count * (i + 1), 4);
}

packetLength += Colors.Count; // 1 per vertex color; not implemented
packetLength += Colors.Count; // 1 per vertex color
packetLength += Normals.Count; // 1 per vertex normal

return packetLength;
}
Expand Down
Loading

0 comments on commit c4b906d

Please sign in to comment.