Skip to content

Commit

Permalink
Remove unnecessary triangle order swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
httpdigest committed Nov 3, 2020
1 parent eaac4e9 commit 1844a59
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
32 changes: 6 additions & 26 deletions src/org/lwjgl/demo/opengl/raytracing/VoxelLightmapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,37 +390,18 @@ public void triangulate(List<Face> faces, ByteBuffer positions, ByteBuffer sides
int n00 = f.v >>> 8 & 7, n10 = f.v >>> 11 & 7;
int n01 = f.v >>> 14 & 7, n11 = f.v >>> 17 & 7;
generateSidesAndOffsets(f, n00, n10, n01, n11, sidesAndOffsets);
int ao00 = n00 >>> 4, ao10 = n10 >>> 4, ao01 = n01 >>> 4, ao11 = n11 >>> 4;
generateTexCoords(f, lightmapCoords);
generateIndices(f, i, ao00 + ao11 > ao10 + ao01, indices);
generateIndices(f, i, indices);
}
}

private static void generateIndices(Face f, int i, boolean swap, ShortBuffer indices) {
private static void generateIndices(Face f, int i, ShortBuffer indices) {
if (isPositiveSide(f.s))
generateIndicesPositive(i, swap, indices);
indices.put((short) ((i << 2) + 1)).put((short) ((i << 2) + 3)).put((short) ((i << 2) + 0))
.put((short) ((i << 2) + 2)).put((short) PRIMITIVE_RESTART_INDEX);
else
generateIndicesNegative(i, swap, indices);
}

private static void generateIndicesNegative(int i, boolean swap, ShortBuffer indices) {
if (swap) {
indices.put((short) ((i << 2) + 2)).put((short) ((i << 2) + 3)).put((short) ((i << 2) + 0))
.put((short) ((i << 2) + 1)).put((short) PRIMITIVE_RESTART_INDEX);
} else {
indices.put((short) ((i << 2) + 3)).put((short) ((i << 2) + 1)).put((short) ((i << 2) + 2))
.put((short) ((i << 2) + 0)).put((short) PRIMITIVE_RESTART_INDEX);
}
}

private static void generateIndicesPositive(int i, boolean swap, ShortBuffer indices) {
if (swap) {
indices.put((short) ((i << 2) + 1)).put((short) ((i << 2) + 3)).put((short) ((i << 2) + 0))
.put((short) ((i << 2) + 2)).put((short) PRIMITIVE_RESTART_INDEX);
} else {
indices.put((short) ((i << 2) + 3)).put((short) ((i << 2) + 2)).put((short) ((i << 2) + 1))
.put((short) ((i << 2) + 0)).put((short) PRIMITIVE_RESTART_INDEX);
}
}

private void generateTexCoords(Face f, ShortBuffer lightmapCoords) {
Expand All @@ -436,11 +417,11 @@ private static byte aoFactors(int n00, int n10, int n01, int n11) {
}

private static byte aoFactor(int n) {
return (byte) ((n & 1) == 1 && (n & 4) == 4 ? 0 : (3 - Integer.bitCount(n)));
return (byte) ((n & 1) != 0 && (n & 4) != 0 ? 0 : (3 - Integer.bitCount(n)));
}

private static byte sampleOffset(int n) {
boolean b1 = (n & 1) == 1, b2 = (n & 2) == 2, b4 = (n & 4) == 4;
boolean b1 = (n & 1) != 0, b2 = (n & 2) != 0, b4 = (n & 4) != 0;
int x = b1 && !b2 && !b4 ? 1 : b4 || b2 && !b1 ? -1 : 0,
y = b4 && !b2 && !b1 ? 1 : b1 || b2 && !b4 ? -1 : 0;
return (byte) (x + 1 | y + 1 << 2);
Expand Down Expand Up @@ -706,7 +687,6 @@ private void raster() {
glDrawElements(GL_TRIANGLE_STRIP, cnt * INDICES_PER_FACE, GL_UNSIGNED_SHORT, off * INDICES_PER_FACE * Short.BYTES);
glBindVertexArray(0);
glUseProgram(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

private void trace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ private void generateTexCoords(Face f, ShortBuffer lightmapCoords) {
}

private static byte aoFactor(int n) {
boolean b1 = (n & 1) == 1, b2 = (n & 2) == 2, b4 = (n & 4) == 4;
boolean b1 = (n & 1) != 0, b2 = (n & 2) != 0, b4 = (n & 4) != 0;
int f = b1 && b4 ? 0 : (3 - Integer.bitCount(n));
int x = b1 && !b2 && !b4 ? 1 : b4 || b2 && !b1 ? -1 : 0,
y = b4 && !b2 && !b1 ? 1 : b1 || b2 && !b4 ? -1 : 0;
Expand Down

0 comments on commit 1844a59

Please sign in to comment.