Skip to content

Commit

Permalink
Triangle sorting changes.
Browse files Browse the repository at this point in the history
FIX: Triangle sorting was broken for orthographic mode, and was kinda janky for overlapping triangles in every mode. This seems to give an overall better result, at the cost of a smidge of performance.
  • Loading branch information
SolarLune committed Dec 17, 2022
1 parent 7d47447 commit 181d781
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
31 changes: 15 additions & 16 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,14 @@ func (model *Model) ProcessVertices(vpMatrix Matrix4, camera *Camera, meshPart *
mesh := meshPart.Mesh
base := modelTransform

sortMode := TriangleSortModeBackToFront

if meshPart.Material != nil {
sortMode = meshPart.Material.TriangleSortMode
}

camPos := camera.WorldPosition()
invertedCamPos := modelTransform.Inverted().MultVec(camPos)

if mat != nil && mat.BillboardMode != BillboardModeNone {

Expand Down Expand Up @@ -485,7 +492,6 @@ func (model *Model) ProcessVertices(vpMatrix Matrix4, camera *Camera, meshPart *
tri := mesh.Triangles[ti]

meshPart.sortingTriangles[sortingTriIndex].Triangle = mesh.Triangles[ti]
depth := 0.0

outOfBounds := true

Expand Down Expand Up @@ -522,12 +528,6 @@ func (model *Model) ProcessVertices(vpMatrix Matrix4, camera *Camera, meshPart *

w := mesh.vertexTransforms[tri.VertexIndices[i]].W

depth += w

if !camera.Perspective {
w = 1.0
}

// If the trangle is beyond the screen, we'll just pretend it's not and limit it to the closest possible value > 0
// TODO: Replace this with triangle clipping or fix whatever graphical glitch seems to arise periodically
if w < 0 {
Expand All @@ -543,8 +543,6 @@ func (model *Model) ProcessVertices(vpMatrix Matrix4, camera *Camera, meshPart *

}

depth /= 3

if outOfBounds {
continue
}
Expand Down Expand Up @@ -635,20 +633,21 @@ func (model *Model) ProcessVertices(vpMatrix Matrix4, camera *Camera, meshPart *
continue
}

meshPart.sortingTriangles[sortingTriIndex].depth = float32(depth)
// Previously, depth was compared using the lowest W value of all vertices in the triangle; after that, I tried
// averaging them out. Neither of these was completely satisfactory, and in addition, there was no depth sorting
// for orthographic triangles that overlapped using this method.

// I could substitute Z for W, but sorting by distance to the triangle center directly gives a better result overall, it seems.
if sortMode != TriangleSortModeNone {
meshPart.sortingTriangles[sortingTriIndex].depth = float32(invertedCamPos.DistanceSquared(tri.Center))
}

sortingTriIndex++

}

meshPart.sortingTriangles = meshPart.sortingTriangles[:sortingTriIndex]

sortMode := TriangleSortModeBackToFront

if meshPart.Material != nil {
sortMode = meshPart.Material.TriangleSortMode
}

// Preliminary tests indicate sort.SliceStable is faster than sort.Slice for our purposes

if sortMode == TriangleSortModeBackToFront {
Expand Down
5 changes: 5 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func (vec Vector) String() string {
return fmt.Sprintf("{%.2f, %.2f, %.2f}", vec.X, vec.Y, vec.Z)
}

// String returns a string representation of the Vector, excluding its W component (which is primarily used for internal purposes).
func (vec Vector) StringW() string {
return fmt.Sprintf("{%.2f, %.2f, %.2f, %.2f}", vec.X, vec.Y, vec.Z, vec.W)
}

// Plus returns a copy of the calling vector, added together with the other Vector provided (ignoring the W component).
func (vec Vector) Add(other Vector) Vector {
vec.X += other.X
Expand Down

0 comments on commit 181d781

Please sign in to comment.