Skip to content

Commit

Permalink
Merge pull request #108 from trigger-segfault/untextured-stp-and-text…
Browse files Browse the repository at this point in the history
…ured-renderflag

Untextured semi-transparency and Textured render flag
  • Loading branch information
rickomax authored Jul 21, 2023
2 parents fa4531c + 6f308e5 commit 34ed02e
Show file tree
Hide file tree
Showing 17 changed files with 415 additions and 467 deletions.
141 changes: 43 additions & 98 deletions Classes/BFFModelReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,16 @@ protected override void Parse(BinaryReader reader, string fileTitle, out List<Ro

private RootEntity ReadModels(BinaryReader reader)
{
var groupedTriangles = new Dictionary<uint, List<Triangle>>();
var groupedTriangles = new Dictionary<RenderInfo, List<Triangle>>();

void AddTriangle(Triangle triangle, uint tPage)
void AddTriangle(Triangle triangle, uint tPage, RenderFlags renderFlags)
{
List<Triangle> triangles;
if (groupedTriangles.ContainsKey(tPage))
{
triangles = groupedTriangles[tPage];
}
else
renderFlags |= RenderFlags.DoubleSided; //todo
var renderInfo = new RenderInfo(tPage, renderFlags);
if (!groupedTriangles.TryGetValue(renderInfo, out var triangles))
{
triangles = new List<Triangle>();
groupedTriangles.Add(tPage, triangles);
groupedTriangles.Add(renderInfo, triangles);
}
triangles.Add(triangle);
}
Expand Down Expand Up @@ -192,14 +189,6 @@ void AddTriangle(Triangle triangle, uint tPage)
uvs[i] = new Vector2(tu, tv);
}

using (var writer = File.CreateText("C:\\USERS\\RICKO\\DESKTOP\\TEST"+ nameCrc+".OBJ"))
{
foreach (var vertex in vertices)
{
writer.WriteLine("v " + vertex.X + " " + vertex.Y + " " + vertex.Z);
}
}

reader.BaseStream.Seek(position, SeekOrigin.Begin);

var numGT3s = ReadInt(reader);
Expand Down Expand Up @@ -247,7 +236,7 @@ void AddTriangle(Triangle triangle, uint tPage)
u1, v1,
u2, v2);

AddTriangle(triangle, tPage);
AddTriangle(triangle, tPage, RenderFlags.Textured);
}
}
else
Expand All @@ -259,8 +248,8 @@ void AddTriangle(Triangle triangle, uint tPage)

var triangle1 = ReadPolyGT3(reader, vertices, vert0, vert1, vert2, out var tPage1);
//var triangle2 = ReadPolyGT3(reader, vertices, vert1, vert3, vert2, out var tPage2);
AddTriangle(triangle1, tPage1);
//AddTriangle(triangle2, 0);
AddTriangle(triangle1, tPage1, RenderFlags.Textured);
//AddTriangle(triangle2, tPage2, RenderFlags.Textured);
}

reader.BaseStream.Seek(position, SeekOrigin.Begin);
Expand Down Expand Up @@ -328,8 +317,8 @@ void AddTriangle(Triangle triangle, uint tPage)
u3, v3,
u2, v2);

AddTriangle(triangle1, tPage);
AddTriangle(triangle2, tPage);
AddTriangle(triangle1, tPage, RenderFlags.Textured);
AddTriangle(triangle2, tPage, RenderFlags.Textured);
}
}
else
Expand All @@ -343,8 +332,9 @@ void AddTriangle(Triangle triangle, uint tPage)
//two POLY_GT3
var triangle1 = ReadPolyGT3(reader, vertices, vert0, vert1, vert2, out var tPage1);
var triangle2 = ReadPolyGT3(reader, vertices, vert1, vert3, vert2, out var tPage2);
AddTriangle(triangle1, tPage1);
AddTriangle(triangle2, tPage2);

AddTriangle(triangle1, tPage1, RenderFlags.Textured);
AddTriangle(triangle2, tPage2, RenderFlags.Textured);
}

reader.BaseStream.Seek(position, SeekOrigin.Begin);
Expand Down Expand Up @@ -386,7 +376,7 @@ void AddTriangle(Triangle triangle, uint tPage)
0, 0,
0, 0);

AddTriangle(triangle, 0);
AddTriangle(triangle, 0, RenderFlags.None);
}
reader.BaseStream.Seek(position, SeekOrigin.Begin);

Expand Down Expand Up @@ -440,24 +430,24 @@ void AddTriangle(Triangle triangle, uint tPage)
0, 0);


AddTriangle(triangle1, 0);
AddTriangle(triangle2, 0);
AddTriangle(triangle1, 0, RenderFlags.None);
AddTriangle(triangle2, 0, RenderFlags.None);
}
reader.BaseStream.Seek(position, SeekOrigin.Begin);

foreach (var kvp in groupedTriangles)
{
var renderInfo = kvp.Key;
var triangles = kvp.Value;
if (triangles.Count > 0)
var model = new ModelEntity
{
var model = new ModelEntity
{
Triangles = triangles.ToArray(),
TexturePage = kvp.Key,
TMDID = 0
};
models.Add(model);
}
Triangles = triangles.ToArray(),
TexturePage = renderInfo.TexturePage,
RenderFlags = renderInfo.RenderFlags,
MixtureRate = renderInfo.MixtureRate,
TMDID = 0
};
models.Add(model);
}

if (models.Count > 0)
Expand Down Expand Up @@ -562,7 +552,7 @@ private Triangle ReadPolyGT3(BinaryReader reader, Vector3[] vertices, uint vert0
//#endif

private Triangle TriangleFromPrimitive(Vector3[] vertices,
uint vertex0, uint vertex1, uint vertex2,
uint vertexIndex0, uint vertexIndex1, uint vertexIndex2,
byte r0, byte g0, byte b0,
byte r1, byte g1, byte b1,
byte r2, byte g2, byte b2,
Expand All @@ -571,74 +561,29 @@ private Triangle TriangleFromPrimitive(Vector3[] vertices,
byte u2, byte v2
)
{
Vector3 ver1, ver2, ver3;
if (vertex0 >= vertices.Length || vertex1 >= vertices.Length || vertex2 >= vertices.Length)
if (vertexIndex0 >= vertices.Length || vertexIndex1 >= vertices.Length || vertexIndex2 >= vertices.Length)
{


throw new Exception("Out of indices");
}
else
{

ver1 = vertices[vertex0];
ver2 = vertices[vertex1];
ver3 = vertices[vertex2];
}
var vertex0 = vertices[vertexIndex0];
var vertex1 = vertices[vertexIndex1];
var vertex2 = vertices[vertexIndex2];

var color0 = new Color(r0/255f, g0/255f, b0/255f);
var color1 = new Color(r1/255f, g1/255f, b1/255f);
var color2 = new Color(r2/255f, g2/255f, b2/255f);

var uv0 = new Vector2(u0/255f, v0/255f);
var uv1 = new Vector2(u1/255f, v1/255f);
var uv2 = new Vector2(u2/255f, v2/255f);

var triangle = new Triangle
{
Colors = new[]
{
new Color
(
r0/255f,
g0/255f,
b0/255f
),
new Color
(
r1/255f,
g1/255f,
b1/255f
),
new Color
(
r2/255f,
g2/255f,
b2/255f
)
},
Normals = new[]
{
Vector3.Zero,
Vector3.Zero,
Vector3.Zero,
},
Vertices = new[]
{
ver1,
ver2,
ver3
},
Uv = new[]
{
new Vector2
{
X = u0/255f,
Y = v0/255f
},
new Vector2
{
X = u1/255f,
Y = v1/255f
},
new Vector2
{
X = u2/255f,
Y = v2/255f
}
},
Vertices = new[] { vertex0, vertex1, vertex2 },
Normals = new[] { Vector3.Zero, Vector3.Zero, Vector3.Zero },
Colors = new[] { color0, color1, color2 },
Uv = new[] { uv0, uv1, uv2 },
AttachableIndices = new[] { uint.MaxValue, uint.MaxValue, uint.MaxValue }
};

Expand Down
62 changes: 44 additions & 18 deletions Classes/CrocModelReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ private static Vector3 ReadVector(BinaryReader reader, bool normal = false, bool

private static RootEntity ReadModels(BinaryReader reader)
{
var groupedTriangles = new Dictionary<RenderInfo, List<Triangle>>();

void AddTriangle(Triangle triangle, uint tPage, RenderFlags renderFlags)
{
renderFlags |= RenderFlags.DoubleSided; //todo
var renderInfo = new RenderInfo(tPage, renderFlags);
if (!groupedTriangles.TryGetValue(renderInfo, out var triangles))
{
triangles = new List<Triangle>();
groupedTriangles.Add(renderInfo, triangles);
}
triangles.Add(triangle);
}

// Some data is properly handled thanks to vs49688's work on CrocUtils.
// Notably: Fixed point size, flags, bounding box indices, color, UVs, and the collision section.
// <https://github.com/vs49688/CrocUtils>
Expand Down Expand Up @@ -111,7 +125,6 @@ private static RootEntity ReadModels(BinaryReader reader)
{
return null;
}
var triangles = new List<Triangle>((int)countFaces);

// Groups are observed as consecutive faces that share some similar properties.
// They are defined by the fourth uint16 in a face. When non-zero, a new group is started.
Expand Down Expand Up @@ -178,11 +191,15 @@ private static RootEntity ReadModels(BinaryReader reader)
normal0 = normal1 = normal2 = GeomUtils.CalculateNormal(vertex0, vertex1, vertex2);
}

var renderFlags = RenderFlags.None;
uint tPage;

// We can't actually use UVs yet, since they're likely scaled for the material, and not the whole VRAM page.
Vector2 uv0, uv1, uv2, uv3;
float r, g, b;
Color color;
if (texture)
{
renderFlags |= RenderFlags.Textured;
if (!quad)
{
// Is uvFlip flag ignored for tri? (flag has been observed both set and unset)
Expand Down Expand Up @@ -210,26 +227,30 @@ private static RootEntity ReadModels(BinaryReader reader)
}

var materialId = faceInfo & 0xffff;
r = g = b = Color.DefaultColorTone;
color = Color.Grey;

tPage = 0; //todo
}
else
{
uv0 = uv1 = uv2 = uv3 = Vector2.Zero;

r = ((faceInfo >> 0) & 0xff) / 255f;
g = ((faceInfo >> 8) & 0xff) / 255f;
b = ((faceInfo >> 16) & 0xff) / 255f;
var r = ((faceInfo >> 0) & 0xff) / 255f;
var g = ((faceInfo >> 8) & 0xff) / 255f;
var b = ((faceInfo >> 16) & 0xff) / 255f;
color = new Color(r, g, b);

tPage = 0; //todo
}
var color = new Color(r, g, b);

triangles.Add(new Triangle
AddTriangle(new Triangle
{
Vertices = new[] { vertex0, vertex1, vertex2 },
Normals = new[] { normal0, normal1, normal2 },
Colors = new[] { color, color, color },
Uv = new[] { Vector2.Zero, Vector2.Zero, Vector2.Zero },
Uv = new[] { Vector2.Zero, Vector2.Zero, Vector2.Zero }, // Can't use UVs yet
AttachableIndices = new [] {uint.MaxValue, uint.MaxValue, uint.MaxValue}
});
}, tPage, renderFlags);
if (quad)
{
var vertex3 = vertices[index3];
Expand All @@ -245,14 +266,14 @@ private static RootEntity ReadModels(BinaryReader reader)
normal1 = normal3 = normal2 = GeomUtils.CalculateNormal(vertex1, vertex3, vertex2);
}

triangles.Add(new Triangle
AddTriangle(new Triangle
{
Vertices = new[] { vertex1, vertex3, vertex2 },
Normals = new[] { normal1, normal3, normal2 },
Colors = new[] { color, color, color },
Uv = new[] { Vector2.Zero, Vector2.Zero, Vector2.Zero },
Uv = new[] { Vector2.Zero, Vector2.Zero, Vector2.Zero }, // Can't use UVs yet
AttachableIndices = new[] { uint.MaxValue, uint.MaxValue, uint.MaxValue }
});
}, tPage, renderFlags);
}
}

Expand All @@ -264,16 +285,21 @@ private static RootEntity ReadModels(BinaryReader reader)
reader.BaseStream.Seek((size1 + size2) * 44, SeekOrigin.Current);
}

// Don't add empty models.
if (triangles.Count > 0)
foreach (var kvp in groupedTriangles)
{
var modelEntity = new ModelEntity
var renderInfo = kvp.Key;
var triangles = kvp.Value;
var model = new ModelEntity
{
Triangles = triangles.ToArray(),
TMDID = i,
TexturePage = renderInfo.TexturePage,
RenderFlags = renderInfo.RenderFlags,
MixtureRate = renderInfo.MixtureRate,
TMDID = i + 1, //todo
};
models.Add(modelEntity);
models.Add(model);
}
groupedTriangles.Clear();
}
if (models.Count > 0)
{
Expand Down
6 changes: 3 additions & 3 deletions Classes/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Mesh

public Matrix4 WorldMatrix { get; set; }
public uint Texture { get; set; }
public RenderFlags RenderFlags { get; set; }
public RenderFlags RenderFlags { get; set; } = RenderFlags.DoubleSided; // Default flags
public MixtureRate MixtureRate { get; set; }

private readonly uint _meshId;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void Draw(TextureBinder textureBinder = null, bool wireframe = false, boo
GL.EnableVertexAttribArray((uint)Scene.AttributeIndexTiledArea);
GL.VertexAttribPointer((uint)Scene.AttributeIndexTiledArea, 4, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);

if (textureBinder != null && Texture != 0)
if (textureBinder != null && Texture != 0 && RenderFlags.HasFlag(RenderFlags.Textured))
{
textureBinder.BindTexture(Texture);
}
Expand All @@ -80,7 +80,7 @@ public void Draw(TextureBinder textureBinder = null, bool wireframe = false, boo
GL.DrawArrays(verticesOnly ? OpenTK.Graphics.OpenGL.PrimitiveType.Points : OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, 0, _numElements);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

if (textureBinder != null && Texture != 0)
if (textureBinder != null && Texture != 0 && RenderFlags.HasFlag(RenderFlags.Textured))
{
textureBinder.Unbind();
}
Expand Down
Loading

0 comments on commit 34ed02e

Please sign in to comment.