aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormakamys <makamys@outlook.com>2022-06-21 00:06:08 +0200
committermakamys <makamys@outlook.com>2022-06-21 00:10:43 +0200
commit762db36a9a06b58a2e7a6315ac9368b4d456881f (patch)
tree0822b61921f9202a2219d32c530cb54d4494f294 /src
parent740382fb780b1f73bc9f7a733dc4884db94d0033 (diff)
downloadNeodymium-762db36a9a06b58a2e7a6315ac9368b4d456881f.tar.gz
Neodymium-762db36a9a06b58a2e7a6315ac9368b4d456881f.tar.bz2
Neodymium-762db36a9a06b58a2e7a6315ac9368b4d456881f.zip
Rewrite MeshQuad to store normal instead of plane
Diffstat (limited to 'src')
-rw-r--r--src/main/java/makamys/neodymium/renderer/ChunkMesh.java4
-rw-r--r--src/main/java/makamys/neodymium/renderer/MeshQuad.java57
-rw-r--r--src/main/java/makamys/neodymium/renderer/QuadNormal.java31
3 files changed, 72 insertions, 20 deletions
diff --git a/src/main/java/makamys/neodymium/renderer/ChunkMesh.java b/src/main/java/makamys/neodymium/renderer/ChunkMesh.java
index 12dd538..abe9a2d 100644
--- a/src/main/java/makamys/neodymium/renderer/ChunkMesh.java
+++ b/src/main/java/makamys/neodymium/renderer/ChunkMesh.java
@@ -99,8 +99,8 @@ public class ChunkMesh extends Mesh {
quadsByPlaneDir.add(new ArrayList<MeshQuad>());
}
for(MeshQuad quad : quads) {
- if(quad.plane != MeshQuad.Plane.NONE) {
- quadsByPlaneDir.get(quad.plane.ordinal() - 1).add(quad);
+ if(quad.getPlane() != MeshQuad.Plane.NONE) {
+ quadsByPlaneDir.get(quad.getPlane().ordinal() - 1).add(quad);
}
}
for(int plane = 0; plane < 3; plane++) {
diff --git a/src/main/java/makamys/neodymium/renderer/MeshQuad.java b/src/main/java/makamys/neodymium/renderer/MeshQuad.java
index ff1cb40..2024efd 100644
--- a/src/main/java/makamys/neodymium/renderer/MeshQuad.java
+++ b/src/main/java/makamys/neodymium/renderer/MeshQuad.java
@@ -4,6 +4,9 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
+
+import org.lwjgl.util.vector.Vector3f;
+
import makamys.neodymium.util.BufferWriter;
/*
@@ -40,7 +43,7 @@ public class MeshQuad {
public boolean deleted;
public boolean noMerge;
- public Plane plane;
+ public QuadNormal normal;
public int offset;
public ChunkMesh.Flags flags;
@@ -56,6 +59,10 @@ public class MeshQuad {
// Keep a reference to the quad we first merged with, and use it as a reminder.
public MeshQuad mergeReference;
+ private static Vector3f vectorA = new Vector3f();
+ private static Vector3f vectorB = new Vector3f();
+ private static Vector3f vectorC = new Vector3f();
+
private int minPositive(int a, int b) {
if(a == -1) {
return b;
@@ -96,15 +103,11 @@ public class MeshQuad {
updateMinMaxXYZ();
- if(ys[0] == ys[1] && ys[1] == ys[2] && ys[2] == ys[3]) {
- plane = Plane.XZ;
- } else if(xs[0] == xs[1] && xs[1] == xs[2] && xs[2] == xs[3]) {
- plane = Plane.YZ;
- } else if(zs[0] == zs[1] && zs[1] == zs[2] && zs[2] == zs[3]) {
- plane = Plane.XY;
- } else {
- plane = Plane.NONE;
- }
+ vectorA.set(xs[1] - xs[0], ys[1] - ys[0], zs[1] - zs[0]);
+ vectorB.set(xs[2] - xs[1], ys[2] - ys[1], zs[2] - zs[1]);
+ Vector3f.cross(vectorA, vectorB, vectorC);
+
+ normal = QuadNormal.fromVector(vectorC);
}
public void writeToBuffer(BufferWriter out) throws IOException {
@@ -172,7 +175,7 @@ public class MeshQuad {
}
private boolean isTranslatedCopyOf(MeshQuad o, boolean checkValid) {
- if((!isValid(this) && checkValid) || !isValid(o) || plane != o.plane) return false;
+ if((!isValid(this) && checkValid) || !isValid(o) || normal != o.normal) return false;
if(mergeReference != null) {
return mergeReference.isTranslatedCopyOf(o, false);
@@ -225,7 +228,7 @@ public class MeshQuad {
quadCountByDirection[1] += o.quadCountByDirection[1];
}
- totalMergeCountByPlane[plane.ordinal() - 1]++;
+ totalMergeCountByPlane[getPlane().ordinal() - 1]++;
mergeReference = o;
@@ -267,10 +270,14 @@ public class MeshQuad {
}
public boolean onSamePlaneAs(MeshQuad o) {
- return isValid(this) && isValid(o) && plane == o.plane &&
- ((plane == Plane.XY && minZ == o.minZ) ||
- (plane == Plane.XZ && minY == o.minY) ||
- (plane == Plane.YZ && minX == o.minX));
+ return isValid(this) && isValid(o) && getPlane() == o.getPlane() &&
+ ((getPlane() == Plane.XY && minZ == o.minZ) ||
+ (getPlane() == Plane.XZ && minY == o.minY) ||
+ (getPlane() == Plane.YZ && minX == o.minX));
+ }
+
+ public Plane getPlane() {
+ return Plane.fromNormal(normal);
}
public static boolean isValid(MeshQuad q) {
@@ -331,7 +338,23 @@ public class MeshQuad {
NONE,
XY,
XZ,
- YZ
+ YZ;
+
+ public static Plane fromNormal(QuadNormal normal) {
+ switch(normal) {
+ case POSITIVE_X:
+ case NEGATIVE_X:
+ return YZ;
+ case POSITIVE_Y:
+ case NEGATIVE_Y:
+ return XZ;
+ case POSITIVE_Z:
+ case NEGATIVE_Z:
+ return XY;
+ default:
+ return NONE;
+ }
+ }
}
public boolean isPosEqual(MeshQuad b) {
diff --git a/src/main/java/makamys/neodymium/renderer/QuadNormal.java b/src/main/java/makamys/neodymium/renderer/QuadNormal.java
index 23e2eeb..9ef32ac 100644
--- a/src/main/java/makamys/neodymium/renderer/QuadNormal.java
+++ b/src/main/java/makamys/neodymium/renderer/QuadNormal.java
@@ -1,5 +1,34 @@
package makamys.neodymium.renderer;
+import org.lwjgl.util.vector.Vector3f;
+
public enum QuadNormal {
- NONE, POSITIVE_X, NEGATIVE_X, POSITIVE_Y, NEGATIVE_Y, POSITIVE_Z, NEGATIVE_Z
+ NONE, POSITIVE_X, NEGATIVE_X, POSITIVE_Y, NEGATIVE_Y, POSITIVE_Z, NEGATIVE_Z;
+
+ public static QuadNormal fromVector(Vector3f normal) {
+ if(normal.getX() == 0f) {
+ if(normal.getY() == 0f) {
+ if(normal.getZ() == 1f) {
+ return POSITIVE_Z;
+ } else if(normal.getZ() == -1f) {
+ return NEGATIVE_Z;
+ }
+ } else if(normal.getZ() == 0f) {
+ if(normal.getY() == 1f) {
+ return POSITIVE_Y;
+ } else if(normal.getY() == -1f) {
+ return NEGATIVE_Y;
+ }
+ }
+ } else if(normal.getY() == 0f) {
+ if(normal.getZ() == 0f) {
+ if(normal.getX() == 1f) {
+ return POSITIVE_X;
+ } else if(normal.getX() == -1f) {
+ return NEGATIVE_X;
+ }
+ }
+ }
+ return NONE;
+ }
}