Skip to content
This repository has been archived by the owner on Sep 16, 2019. It is now read-only.

Commit

Permalink
Fix face clipping bug causing big errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LogicAndTrick committed Sep 8, 2018
1 parent 44b7e50 commit f8bca94
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public Task Convert(BufferBuilder builder, MapDocument document, IMapObject obj,

internal static async Task ConvertFaces(BufferBuilder builder, MapDocument document, IMapObject obj, List<Face> faces, ResourceCollector resourceCollector)
{
faces = faces.Where(x => x.Vertices.Count > 2).ToList();

var displayFlags = document.Map.Data.GetOne<DisplayFlags>();
var hideNull = displayFlags?.HideNullTextures == true;

Expand Down
9 changes: 5 additions & 4 deletions Sledge.BspEditor.Tools/Brush/BrushTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,13 @@ public override void Render(BufferBuilder builder, ResourceCollector resourceCol
private async Task Convert(BufferBuilder builder, MapDocument document, IMapObject obj, ResourceCollector resourceCollector)
{
var solid = (Solid)obj;
var faces = solid.Faces.Where(x => x.Vertices.Count > 2).ToList();

// Pack the vertices like this [ f1v1 ... f1vn ] ... [ fnv1 ... fnvn ]
var numVertices = (uint)solid.Faces.Sum(x => x.Vertices.Count);
var numVertices = (uint)faces.Sum(x => x.Vertices.Count);

// Pack the indices like this [ solid1 ... solidn ] [ wireframe1 ... wireframe n ]
var numSolidIndices = (uint)solid.Faces.Sum(x => (x.Vertices.Count - 2) * 3);
var numSolidIndices = (uint)faces.Sum(x => (x.Vertices.Count - 2) * 3);
var numWireframeIndices = numVertices * 2;

var points = new VertexStandard[numVertices];
Expand All @@ -316,7 +317,7 @@ private async Task Convert(BufferBuilder builder, MapDocument document, IMapObje
var vi = 0u;
var si = 0u;
var wi = numSolidIndices;
foreach (var face in solid.Faces)
foreach (var face in faces)
{
var t = await tc.GetTextureItem(face.Texture.Name);
var w = t?.Width ?? 0;
Expand Down Expand Up @@ -361,7 +362,7 @@ private async Task Convert(BufferBuilder builder, MapDocument document, IMapObje
var groups = new List<BufferGroup>();

uint texOffset = 0;
foreach (var f in solid.Faces)
foreach (var f in faces)
{
var texInd = (uint)(f.Vertices.Count - 2) * 3;

Expand Down
10 changes: 6 additions & 4 deletions Sledge.BspEditor.Tools/Vertex/VertexTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,13 @@ private async Task Convert(BufferBuilder builder, MapDocument document, MutableS
var displayFlags = document.Map.Data.GetOne<DisplayFlags>();
var hideNull = displayFlags?.HideNullTextures == true;

var faces = solid.Faces.Where(x => x.Vertices.Count > 2).ToList();

// Pack the vertices like this [ f1v1 ... f1vn ] ... [ fnv1 ... fnvn ]
var numVertices = (uint)solid.Faces.Sum(x => x.Vertices.Count);
var numVertices = (uint)faces.Sum(x => x.Vertices.Count);

// Pack the indices like this [ solid1 ... solidn ] [ wireframe1 ... wireframe n ]
var numSolidIndices = (uint)solid.Faces.Sum(x => (x.Vertices.Count - 2) * 3);
var numSolidIndices = (uint)faces.Sum(x => (x.Vertices.Count - 2) * 3);
var numWireframeIndices = numVertices * 2;

var points = new VertexStandard[numVertices];
Expand All @@ -281,7 +283,7 @@ private async Task Convert(BufferBuilder builder, MapDocument document, MutableS
var vi = 0u;
var si = 0u;
var wi = numSolidIndices;
foreach (var face in solid.Faces)
foreach (var face in faces)
{
var opacity = tc.GetOpacity(face.Texture.Name);
var t = await tc.GetTextureItem(face.Texture.Name);
Expand Down Expand Up @@ -328,7 +330,7 @@ private async Task Convert(BufferBuilder builder, MapDocument document, MutableS
var groups = new List<BufferGroup>();

uint texOffset = 0;
foreach (var f in solid.Faces)
foreach (var f in faces)
{
var texInd = (uint)(f.Vertices.Count - 2) * 3;

Expand Down
54 changes: 40 additions & 14 deletions Sledge.BspEditor/Providers/VmfBspSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
using Sledge.Common.Shell.Documents;
using Sledge.Common.Transport;
using Sledge.DataStructures.Geometric;
using Plane = Sledge.DataStructures.Geometric.Plane;
using Plane = Sledge.DataStructures.Geometric.Precision.Plane;
using Polygon = Sledge.DataStructures.Geometric.Precision.Polygon;
using Polyhedron = Sledge.DataStructures.Geometric.Precision.Polyhedron;
using PVector3 = Sledge.DataStructures.Geometric.Precision.Vector3;

namespace Sledge.BspEditor.Providers
{
Expand Down Expand Up @@ -419,12 +422,12 @@ private static string FormatDecimal(float d)
return d.ToString("0.00####", CultureInfo.InvariantCulture);
}

private static bool ParseDecimalArray(string input, char[] splitChars, int expected, out float[] array)
private static bool ParseFloatArray(string input, char[] splitChars, int expected, out float[] array)
{
var spl = input.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
if (spl.Length == expected)
{
var parsed = spl.Select(x => float.TryParse(x, NumberStyles.Float, CultureInfo.InvariantCulture, out float o) ? (float?)o : null).ToList();
var parsed = spl.Select(x => float.TryParse(x, NumberStyles.Float, CultureInfo.InvariantCulture, out var o) ? (float?) o : null).ToList();
if (parsed.All(x => x.HasValue))
{
// ReSharper disable once PossibleInvalidOperationException
Expand All @@ -436,6 +439,23 @@ private static bool ParseDecimalArray(string input, char[] splitChars, int expec
return false;
}

private static bool ParseDoubleArray(string input, char[] splitChars, int expected, out double[] array)
{
var spl = input.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
if (spl.Length == expected)
{
var parsed = spl.Select(x => double.TryParse(x, NumberStyles.Float, CultureInfo.InvariantCulture, out var o) ? (double?) o : null).ToList();
if (parsed.All(x => x.HasValue))
{
// ReSharper disable once PossibleInvalidOperationException
array = parsed.Select(x => x.Value).ToArray();
return true;
}
}
array = new double[expected];
return false;
}

private abstract class VmfObject
{
public long ID { get; set; }
Expand Down Expand Up @@ -675,20 +695,25 @@ private void CreateFaces(Solid solid, List<VmfSide> sides, UniqueNumberGenerator
{
continue;
}
side.Vertices.AddRange(pg.Vertices);
side.Vertices.AddRange(pg.Vertices.Select(x => x.ToStandardVector3()));
}
}

foreach (var emptySide in sides.Where(x => !x.Vertices.Any()))
{
Console.WriteLine(emptySide.ID);
}

// We know the vertices, now create the faces
foreach (var side in sides)
{
var face = new Face(generator.Next("Face"))
{
Plane = side.Plane,
Plane = side.Plane.ToStandardPlane(),
Texture = side.Texture
};
face.Vertices.AddRange(side.Vertices);
solid.Data.Add(face);
if (face.Vertices.Any()) solid.Data.Add(face);
}

solid.DescendantsChanged();
Expand Down Expand Up @@ -740,30 +765,31 @@ public VmfSide(SerialisedObject obj)
LightmapScale = obj.Get("lightmapscale", 0);
SmoothingGroups = obj.Get("smoothing_groups", "");

if (ParseDecimalArray(obj.Get("plane", ""), new[] {' ', '(', ')'}, 9, out float[] pl))
if (ParseDoubleArray(obj.Get("plane", ""), new[] {' ', '(', ')'}, 9, out double[] pl))
{
Plane = new Plane(
new Vector3(pl[0], pl[1], pl[2]).Round(),
new Vector3(pl[3], pl[4], pl[5]).Round(),
new Vector3(pl[6], pl[7], pl[8]).Round());
new PVector3(pl[0], pl[1], pl[2]).Round(),
new PVector3(pl[3], pl[4], pl[5]).Round(),
new PVector3(pl[6], pl[7], pl[8]).Round()
);
}
else
{
Plane = new Plane(Vector3.UnitZ, 0);
Plane = new Plane(PVector3.UnitZ, 0);
}

Texture = new Texture
{
Name = obj.Get("material", ""),
Rotation = obj.Get("rotation", 0f)
};
if (ParseDecimalArray(obj.Get("uaxis", ""), new[] {' ', '[', ']'}, 5, out float[] ua))
if (ParseFloatArray(obj.Get("uaxis", ""), new[] {' ', '[', ']'}, 5, out float[] ua))
{
Texture.UAxis = new Vector3(ua[0], ua[1], ua[2]);
Texture.XShift = ua[3];
Texture.XScale = ua[4];
}
if (ParseDecimalArray(obj.Get("vaxis", ""), new[] {' ', '[', ']'}, 5, out float[] va))
if (ParseFloatArray(obj.Get("vaxis", ""), new[] {' ', '[', ']'}, 5, out float[] va))
{
Texture.VAxis = new Vector3(va[0], va[1], va[2]);
Texture.YShift = va[3];
Expand Down Expand Up @@ -791,7 +817,7 @@ public VmfSide(SerialisedObject obj)
public VmfSide(Face face)
{
ID = face.ID;
Plane = face.Plane;
Plane = face.Plane.ToPrecisionPlane();
Texture = face.Texture;
Vertices = face.Vertices.ToList();
}
Expand Down
2 changes: 1 addition & 1 deletion Sledge.DataStructures/Geometric/Precision/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public bool Split(Plane clip, out Polygon back, out Polygon front, out Polygon c
if (sd <= 0) backVerts.Add(s);
if (sd >= 0) frontVerts.Add(s);

if (sd <= 0 != ed <= 0)
if ((sd < 0 && ed > 0) || (ed < 0 && sd > 0))
{
var t = sd / (sd - ed);
var intersect = s * (1 - t) + e * t;
Expand Down

0 comments on commit f8bca94

Please sign in to comment.