Skip to content

Commit

Permalink
cleanup unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Nov 5, 2023
1 parent a5475c7 commit e571c36
Showing 1 changed file with 0 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.joml.Vector3fc;

import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap;
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadFacing;
import me.jellysquid.mods.sodium.client.util.collections.BitArray;
Expand Down Expand Up @@ -37,66 +36,6 @@ private static boolean pointInsideHalfSpace(Vector3fc planeAnchor, Vector3fc pla
return halfspace(planeAnchor, planeNormal, point) < 0;
}

public static void distanceSortModified(IntBuffer indexBuffer, TQuad[] quads, Vector3fc cameraPos) {
int[] indexes = new int[quads.length];
BitArray visible = new BitArray(quads.length);
float[] centerDist = new float[quads.length];
for (int i = 0; i < indexes.length; i++) {
indexes[i] = i;
TQuad quad = quads[i];
if (pointOutsideHalfspace(quad.center(), quad.normal(), cameraPos)) {
visible.set(i);
centerDist[i] = cameraPos.distanceSquared(quads[i].center());
}
}

IntArrays.quickSort(indexes, (a, b) -> {
// returning -1 results in the ordering a, b, returning 1 produces b, a

boolean aVisible = visible.get(a);
boolean bVisible = visible.get(b);

// compare only pairs where both are visible
if (aVisible && bVisible) {
var quadA = quads[a];
var quadB = quads[b];
var normalA = quadA.normal();
var normalB = quadB.normal();

// this is a heuristic that attempts to reduce the sorting mistakes that happen
// when two parallel planes made up of many quads are seen from different
// angles. For quads with equal normals it sorts them only by their
// normal-relative distance to the camera. This eliminates some of wrong sorting
// that happens in tangent directions.
if (normalA.equals(normalB)) {
var centerA = quadA.center();
var centerB = quadB.center();
var cameraDistance = normalB.dot(cameraPos);
var result = Float.compare(Math.abs(cameraDistance - normalB.dot(centerB)),
Math.abs(cameraDistance - normalB.dot(centerA)));
if (result != 0) {
return result;
}
}

return Float.compare(centerDist[b], centerDist[a]);

}

// put invisible quads last
if (aVisible) {
return -1;
}
if (bVisible) {
return 1;
}

return 0;
});

TranslucentData.writeQuadVertexIndexes(indexBuffer, indexes);
}

private static int[] distanceSortIndexes(TQuad[] quads, Vector3fc cameraPos) {
float[] keys = new float[quads.length];
for (int i = 0; i < quads.length; i++) {
Expand Down Expand Up @@ -436,171 +375,6 @@ private static boolean quadVisibleThrough(TQuad quad, TQuad other,
return result;
}

/**
* Performs a topological sort but constructs the full forward graph without
* using a compression technique like
* {@link #topoSortAlignedScanningCyclic(IntBuffer, TQuad[], Vector3fc)} does.
* Only
* sort visible quads if a camera position is provided.
*
* @param indexBuffer the buffer to write the topo sort result to
* @param allQuads the quads to sort
* @param distancesByNormal a map of normals to sorted arrays of face plane
* distances, null to disable
* @param cameraPos the camera position, or null to disable the
* visibility check
* @return true if the quads were sorted, false if there was a cycle
*/
public static boolean topoSortLinkedListAcyclic(
IntBuffer indexBuffer, TQuad[] allQuads,
Object2ReferenceOpenHashMap<Vector3fc, double[]> distancesByNormal,
Vector3fc cameraPos) {
// TODO: quads visibility filter copy-pasted from topoSortAlignedAcyclic
// if enabled, check for visibility and produce a mapping of indices
TQuad[] quads = null;
int[] activeToRealIndex = null;
int activeQuads = 0;
if (cameraPos != null) {
// allocate the working quads and index map at the full size to avoid needing to
// iterate the quads again after checking visibility
quads = new TQuad[allQuads.length];
activeToRealIndex = new int[allQuads.length];

for (int i = 0; i < allQuads.length; i++) {
TQuad quad = allQuads[i];
if (pointOutsideHalfspace(quad.center(), quad.normal(), cameraPos)) {
activeToRealIndex[activeQuads] = i;
quads[activeQuads] = quad;
activeQuads++;
} else {
// write the invisible quads right away
TranslucentData.putQuadVertexIndexes(indexBuffer, i);
}
}
} else {
quads = allQuads;
activeQuads = allQuads.length;
}

// special case for 0 or 1 quads because the algorithm below doesn't work for
// those cases (and it's just faster to skip it)
if (activeQuads == 0) {
return true;
}
if (activeQuads == 1) {
TranslucentData.putMappedQuadVertexIndexes(indexBuffer, 0, activeToRealIndex);
return true;
}

// special case 2 quads for performance
if (activeQuads == 2) {
var a = 0;
var b = 1;
if (quadVisibleThrough(quads[a], quads[b], null, null)) {
a = 1;
b = 0;
}
TranslucentData.putMappedQuadVertexIndexes(indexBuffer, a, activeToRealIndex);
TranslucentData.putMappedQuadVertexIndexes(indexBuffer, b, activeToRealIndex);
return true;
}

// int-based doubly linked list of active quad indexes
// TODO: put this linked list in a class
// TODO: sorting is sometimes wrong, see example in sandstone world
int[] next = new int[activeQuads];
int[] prev = new int[activeQuads];
int start = 0;
for (int i = 0; i < activeQuads - 1; i++) {
next[i] = i + 1;
prev[i + 1] = i;
}
next[activeQuads - 1] = -1;
prev[0] = -1;

// go through all the active quads and insert them into the list at the point
// where they are after all the quads that are visible through them
for (int quadIndex = 0; quadIndex < activeQuads; quadIndex++) {
TQuad quad = quads[quadIndex];

// test all other quads
int insertAfter = -1;
int otherQuadIndex = start;
while (otherQuadIndex != -1) {
TQuad otherQuad = quads[otherQuadIndex];
if (quadIndex != otherQuadIndex
&& quadVisibleThrough(quad, otherQuad, distancesByNormal, cameraPos)) {
insertAfter = otherQuadIndex;
}
otherQuadIndex = next[otherQuadIndex];
}

// if the move index is lower than insert after, instead move the insert after
// to be right before the move index
int removeIndex = quadIndex;
boolean swapped = quadIndex < insertAfter; // implies insertAfter != -1
if (swapped) {
removeIndex = insertAfter;
}

// remove quadIndex or insertAfter from current location
int currentNext = next[removeIndex];
int currentPrev = prev[removeIndex];
if (currentNext != -1) {
prev[currentNext] = currentPrev;
}
if (currentPrev != -1) {
next[currentPrev] = currentNext;
} else {
start = currentNext;
}

// insert after the identified last quad that this quad needs to be after
if (insertAfter == -1) {
// insert quadIndex at the start
next[quadIndex] = start;
prev[quadIndex] = -1;
prev[start] = quadIndex;
start = quadIndex;
} else if (swapped) {
// insert insertAfter at the start if quadIndex is the start
if (start == quadIndex) {
next[insertAfter] = start;
prev[insertAfter] = -1;
prev[start] = insertAfter;
start = insertAfter;
} else {
// insert insertAfter before quadIndex
next[insertAfter] = quadIndex;
next[prev[quadIndex]] = insertAfter;
prev[insertAfter] = prev[quadIndex];
prev[quadIndex] = insertAfter;
}
} else {
// insert quadIndex after insertAfter
prev[quadIndex] = insertAfter;
if (next[insertAfter] != -1) { // end of chain
prev[next[insertAfter]] = quadIndex;
}
next[quadIndex] = next[insertAfter];
next[insertAfter] = quadIndex;
}
}

// write the sorted quads to the buffer
for (int i = 0; i < activeQuads; i++) {
TranslucentData.putMappedQuadVertexIndexes(indexBuffer, start, activeToRealIndex);
start = next[start];
}

// detect cycles
if (start != -1) {
return false;
}

return true;
}

/**
* Performs a topological sort without constructing the full graph in memory by
* doing a DFS on the implicit graph. Edges are tested as they are searched for
Expand Down

0 comments on commit e571c36

Please sign in to comment.