aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormakamys <makamys@outlook.com>2022-06-10 14:25:38 +0200
committermakamys <makamys@outlook.com>2022-06-10 19:20:59 +0200
commit78eb588a19fee4e453f57fe8b4fcb29f2fc6f9bf (patch)
tree94727ab3c69cf180099a0d860082576571aed3af /src
parentdd30daf76fd32d3dae980894b3e2a1c9b69028da (diff)
downloadNeodymium-78eb588a19fee4e453f57fe8b4fcb29f2fc6f9bf.tar.gz
Neodymium-78eb588a19fee4e453f57fe8b4fcb29f2fc6f9bf.tar.bz2
Neodymium-78eb588a19fee4e453f57fe8b4fcb29f2fc6f9bf.zip
Add squadron-based position information to vertices
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.java28
-rw-r--r--src/main/java/makamys/neodymium/renderer/NeoRenderer.java6
-rw-r--r--src/main/java/makamys/neodymium/util/BufferWriter.java10
4 files changed, 43 insertions, 5 deletions
diff --git a/src/main/java/makamys/neodymium/renderer/ChunkMesh.java b/src/main/java/makamys/neodymium/renderer/ChunkMesh.java
index fcfebf0..e175167 100644
--- a/src/main/java/makamys/neodymium/renderer/ChunkMesh.java
+++ b/src/main/java/makamys/neodymium/renderer/ChunkMesh.java
@@ -233,7 +233,7 @@ public class ChunkMesh extends Mesh {
}
private ByteBuffer createBuffer(List<? extends MeshQuad> quads, int quadCount) {
- ByteBuffer buffer = BufferUtils.createByteBuffer(quadCount * 6 * 7 * 4);
+ ByteBuffer buffer = BufferUtils.createByteBuffer(quadCount * 6 * MeshQuad.getStride());
BufferWriter out = new BufferWriter(buffer);
try {
@@ -301,7 +301,7 @@ public class ChunkMesh extends Mesh {
}
public int getStride() {
- return (3 * 4 + (flags.hasTexture ? 8 : 0) + (flags.hasBrightness ? 4 : 0) + (flags.hasColor ? 4 : 0) + (flags.hasNormals ? 4 : 0));
+ return MeshQuad.getStride();
}
static void saveChunks(List<Integer> coords) {
diff --git a/src/main/java/makamys/neodymium/renderer/MeshQuad.java b/src/main/java/makamys/neodymium/renderer/MeshQuad.java
index 6d1221e..bebdafd 100644
--- a/src/main/java/makamys/neodymium/renderer/MeshQuad.java
+++ b/src/main/java/makamys/neodymium/renderer/MeshQuad.java
@@ -21,6 +21,13 @@ import net.minecraft.util.EnumFacing;
* | |
* 3--0
*
+ * We can glue quads together, forming a "quad squadron", or "squadron" for short.
+ * In the fragment shader we need to know which quad of the squadron we are operating on.
+ * For this reason, we store the "squadron X" and "squadron Y" coordinates in the vertices.
+ * Their values at vertex 0: (0, 0)
+ * Their values at vertex 1: (0, squadron height)
+ * Their values at vertex 2: (squadron width, squadron height)
+ * Their values at vertex 3: (squadron width, 0)
*/
public class MeshQuad {
@@ -44,8 +51,8 @@ public class MeshQuad {
public int offset;
public ChunkMesh.Flags flags;
- // 0: quads glued together on edge 0-1 or 2-3 ("row length")
- // 1: quads glued together on edge 1-2 or 3-0 ("column length")
+ // 0: quads glued together on edge 0-1 or 2-3 ("squadron row length")
+ // 1: quads glued together on edge 1-2 or 3-0 ("squadron column length")
private int[] quadCountByDirection = {1, 1};
public static int[] totalMergeCountByPlane = new int[3];
@@ -126,10 +133,27 @@ public class MeshQuad {
out.writeInt(c);
+ out.writeByte(vi < 2 ? (byte)0 : (byte)quadCountByDirection[0]); // squadron x
+ out.writeByte((vi == 0 || vi == 3) ? (byte)0 : (byte)quadCountByDirection[1]); // quad z
+ out.writeByte(vi < 2 ? (byte)0 : 1); // which squadron corner x
+ out.writeByte((vi == 0 || vi == 3) ? (byte)0 : 1); // which squadron corner z
+
+ assert out.position() % getStride() == 0;
+
//System.out.println("[" + vertexI + "] x: " + x + ", y: " + y + " z: " + z + ", u: " + u + ", v: " + v + ", b: " + b + ", c: " + c);
}
}
+ public static int getStride() {
+ return
+ 3 * 4 // XYZ (float)
+ + 2 * 4 // UV (float)
+ + 4 // B (int)
+ + 4 // C (int))
+ + 4
+ ; // squadron XY (byte)
+ }
+
private boolean isTranslatedCopyOf(MeshQuad o, boolean checkValid) {
if((!isValid(this) && checkValid) || !isValid(o) || plane != o.plane) return false;
diff --git a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java
index cd15bda..8cc32f5 100644
--- a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java
+++ b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java
@@ -9,6 +9,7 @@ import static org.lwjgl.opengl.GL11.GL_PROJECTION_MATRIX;
import static org.lwjgl.opengl.GL11.GL_SHORT;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
+import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
import static org.lwjgl.opengl.GL11.GL_VIEWPORT;
import static org.lwjgl.opengl.GL11.glGetFloat;
import static org.lwjgl.opengl.GL11.glGetInteger;
@@ -420,17 +421,20 @@ public class NeoRenderer {
glBindBuffer(GL_ARRAY_BUFFER, mem.VBO);
- int stride = 7 * 4;
+ int stride = MeshQuad.getStride();
glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, stride, 3 * 4);
glVertexAttribPointer(2, 2, GL_SHORT, false, stride, 5 * 4);
glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, false, stride, 6 * 4);
+ glVertexAttribPointer(4, 4, GL_UNSIGNED_BYTE, false, stride, 7 * 4);
+ // we only need 2 bytes for attribute 4, but GL freaks out if the stride isn't a multiple of 4, so we have 2 unused bytes
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
+ glEnableVertexAttribArray(4);
for(int i = 0; i < 2; i++) {
piFirst[i] = BufferUtils.createIntBuffer(MAX_MESHES);
diff --git a/src/main/java/makamys/neodymium/util/BufferWriter.java b/src/main/java/makamys/neodymium/util/BufferWriter.java
index 901f5dc..617bd7a 100644
--- a/src/main/java/makamys/neodymium/util/BufferWriter.java
+++ b/src/main/java/makamys/neodymium/util/BufferWriter.java
@@ -43,4 +43,14 @@ public class BufferWriter {
incrementPosition(4);
}
+ public void writeByte(byte x) {
+ buf.put(x); // this increments the buffer position by 1
+
+ incrementPosition(0);
+ }
+
+ public int position() {
+ return buf.position();
+ }
+
} \ No newline at end of file