diff options
| author | 2024-06-14 20:47:14 -0500 | |
|---|---|---|
| committer | 2024-06-14 20:47:14 -0500 | |
| commit | 0dfcb09376007cded49a1d2d54bf2c197a2dbe44 (patch) | |
| tree | 6becc57575b232099298fc4821f9c859e8db1b07 /src | |
| parent | initial commit (diff) | |
allow translation using 4x4 matrices
Diffstat (limited to 'src')
7 files changed, 114 insertions, 89 deletions
diff --git a/src/main/java/dev/figboot/cuberender/math/Matrix4f.java b/src/main/java/dev/figboot/cuberender/math/Matrix4f.java index e86db41..8f98b3b 100644 --- a/src/main/java/dev/figboot/cuberender/math/Matrix4f.java +++ b/src/main/java/dev/figboot/cuberender/math/Matrix4f.java @@ -80,7 +80,7 @@ public class Matrix4f { } public Vector4f transform(Vector4f in) { - return transform(in, in); + return transform(in, new Vector4f()); } public Vector4f transform(Vector4f in, Vector4f target) { diff --git a/src/main/java/dev/figboot/cuberender/math/Vector3f.java b/src/main/java/dev/figboot/cuberender/math/Vector3f.java index 40720e8..a2608f1 100644 --- a/src/main/java/dev/figboot/cuberender/math/Vector3f.java +++ b/src/main/java/dev/figboot/cuberender/math/Vector3f.java @@ -34,9 +34,9 @@ public class Vector3f { public Vector3f normalize(Vector3f target) { float len = length(); - target.x /= len; - target.y /= len; - target.z /= len; + target.x = this.x / len; + target.y = this.y / len; + target.z = this.z / len; return target; } } diff --git a/src/main/java/dev/figboot/cuberender/math/Vector4f.java b/src/main/java/dev/figboot/cuberender/math/Vector4f.java index 7a29010..402b5ed 100644 --- a/src/main/java/dev/figboot/cuberender/math/Vector4f.java +++ b/src/main/java/dev/figboot/cuberender/math/Vector4f.java @@ -10,6 +10,10 @@ public class Vector4f { this(0, 0, 0, 0); } + public Vector4f(float x, float y, float z) { + this(x, y, z, 1f); + } + public Vector4f(Vector4f vec) { this(vec.x, vec.y, vec.z, vec.w); } @@ -38,10 +42,36 @@ public class Vector4f { } public Vector4f times(float fact, Vector4f target) { - target.x *= fact; - target.y *= fact; - target.z *= fact; - target.w *= fact; + target.x = x * fact; + target.y = y * fact; + target.z = z * fact; + target.w = w * fact; + return target; + } + + public float dot(Vector4f that) { + return this.x * that.x + this.y * that.y + this.z * that.z + this.w * that.w; + } + + public float lengthSquared() { + // dot(this, this) + return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; + } + + public float length() { + return (float)Math.sqrt(lengthSquared()); + } + + public Vector4f normalize() { + return normalize(this); + } + + public Vector4f normalize(Vector4f target) { + float len = length(); + target.x = this.x / len; + target.y = this.y / len; + target.z = this.z / len; + target.w = this.w / len; return target; } } diff --git a/src/main/java/dev/figboot/cuberender/state/Framebuffer.java b/src/main/java/dev/figboot/cuberender/state/Framebuffer.java index 56e218e..3c2a1ad 100644 --- a/src/main/java/dev/figboot/cuberender/state/Framebuffer.java +++ b/src/main/java/dev/figboot/cuberender/state/Framebuffer.java @@ -1,6 +1,7 @@ package dev.figboot.cuberender.state; import dev.figboot.cuberender.math.Matrix3f; +import dev.figboot.cuberender.math.Matrix4f; import dev.figboot.cuberender.math.Vector3f; import dev.figboot.cuberender.math.Vector4f; import lombok.Getter; @@ -23,9 +24,9 @@ public class Framebuffer { @Getter private final BufferedImage color; private final float[] depth; - private int depthMode = FB_DEPTH_USE | FB_DEPTH_COMMIT; + @Setter private int depthMode = FB_DEPTH_USE | FB_DEPTH_COMMIT; - @Setter private Matrix3f transform; + @Setter private Matrix4f transform; @Setter private BlendMode blendMode = BlendMode.DISABLE; @@ -37,10 +38,6 @@ public class Framebuffer { depth = new float[width * height]; } - public void setDepthMode(int mode) { - this.depthMode = mode; - } - public void clear(int bits, int color) { if ((bits & FB_CLEAR_COLOR) != 0) { Graphics gfx = this.color.getGraphics(); @@ -74,9 +71,9 @@ public class Framebuffer { i1 = mesh.indices[tri * 3 + 1]; i2 = mesh.indices[tri * 3 + 2]; - Vector3f vert0 = mesh.vertices[i0]; - Vector3f vert1 = mesh.vertices[i1]; - Vector3f vert2 = mesh.vertices[i2]; + Vector4f vert0 = mesh.vertices[i0]; + Vector4f vert1 = mesh.vertices[i1]; + Vector4f vert2 = mesh.vertices[i2]; drawTriangle(vert0, vert1, vert2, mesh.normals[tri], s, i0, i1, i2); } @@ -94,9 +91,9 @@ public class Framebuffer { i1 = tri * 3 + 1; i2 = tri * 3 + 2; - Vector3f vert0 = mesh.vertices[i0]; - Vector3f vert1 = mesh.vertices[i1]; - Vector3f vert2 = mesh.vertices[i2]; + Vector4f vert0 = mesh.vertices[i0]; + Vector4f vert1 = mesh.vertices[i1]; + Vector4f vert2 = mesh.vertices[i2]; drawTriangle(vert0, vert1, vert2, mesh.normals[tri], s, i0, i1, i2); } @@ -111,7 +108,7 @@ public class Framebuffer { } // triangles have flat normals (we don't need anything more than that in this renderer and it saves us the trouble of interpolating between 3 normal vectors) - private void drawTriangle(Vector3f vert0, Vector3f vert1, Vector3f vert2, Vector3f normal, Sampleable<Object> sampleable, int i0, int i1, int i2) { + private void drawTriangle(Vector4f vert0, Vector4f vert1, Vector4f vert2, Vector4f normal, Sampleable<Object> sampleable, int i0, int i1, int i2) { Vector4f outColor = new Vector4f(), prevColor = new Vector4f(); vert0 = transform.transform(vert0); diff --git a/src/main/java/dev/figboot/cuberender/state/Mesh.java b/src/main/java/dev/figboot/cuberender/state/Mesh.java index 2f28c16..c18fe04 100644 --- a/src/main/java/dev/figboot/cuberender/state/Mesh.java +++ b/src/main/java/dev/figboot/cuberender/state/Mesh.java @@ -2,7 +2,7 @@ package dev.figboot.cuberender.state; import dev.figboot.cuberender.math.MathUtil; import dev.figboot.cuberender.math.Vector2f; -import dev.figboot.cuberender.math.Vector3f; +import dev.figboot.cuberender.math.Vector4f; import dev.figboot.cuberender.math.Vector4f; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; @@ -11,20 +11,20 @@ import java.util.*; @RequiredArgsConstructor(access = AccessLevel.PACKAGE) public abstract class Mesh<T> implements Sampleable<T> { - final Vector3f[] vertices; - final Vector3f[] normals; + final Vector4f[] vertices; + final Vector4f[] normals; final int[] indices; final Map<AttachmentType, Object> attachments; - protected void applyLighting(Vector4f color, Vector3f normal) { + protected void applyLighting(Vector4f color, Vector4f normal) { Float lightFact = (Float)attachments.get(AttachmentType.LIGHT_FACTOR); if (lightFact == null) { return; } - float fact = 1 - (normal.dot((Vector3f)attachments.get(AttachmentType.LIGHT_VECTOR)) + 1) / 2; + float fact = 1 - (normal.dot((Vector4f)attachments.get(AttachmentType.LIGHT_VECTOR)) + 1) / 2; fact *= lightFact; // lightFact should kinda set the "black level" fact = 1 - fact; @@ -36,8 +36,8 @@ public abstract class Mesh<T> implements Sampleable<T> { } public static class Builder { - private final List<Vector3f> vertices = new ArrayList<>(); - private final List<Vector3f> normals = new ArrayList<>(); + private final List<Vector4f> vertices = new ArrayList<>(); + private final List<Vector4f> normals = new ArrayList<>(); private final List<Integer> indices = new ArrayList<>(); private final List<Vector2f> texCoords = new ArrayList<>(); private int color; @@ -65,12 +65,12 @@ public abstract class Mesh<T> implements Sampleable<T> { return this; } - public Builder vertex(Vector3f... vert) { + public Builder vertex(Vector4f... vert) { vertices.addAll(Arrays.asList(vert)); return this; } - public Builder normals(Vector3f... norm) { + public Builder normals(Vector4f... norm) { normals.addAll(Arrays.asList(norm)); return this; } @@ -99,9 +99,9 @@ public abstract class Mesh<T> implements Sampleable<T> { } if (texture == null) { - return new ColorMesh(vertices.toArray(new Vector3f[0]), normals.toArray(new Vector3f[0]), idxArr, attachments, color); + return new ColorMesh(vertices.toArray(new Vector4f[0]), normals.toArray(new Vector4f[0]), idxArr, attachments, color); } else { - return new TextureMesh(vertices.toArray(new Vector3f[0]), normals.toArray(new Vector3f[0]), idxArr, attachments, texture, texCoords.toArray(new Vector2f[0])); + return new TextureMesh(vertices.toArray(new Vector4f[0]), normals.toArray(new Vector4f[0]), idxArr, attachments, texture, texCoords.toArray(new Vector2f[0])); } } } @@ -109,7 +109,7 @@ public abstract class Mesh<T> implements Sampleable<T> { private static class ColorMesh extends Mesh<Void> { int color; - ColorMesh(Vector3f[] vertices, Vector3f[] normals, int[] indices, Map<AttachmentType, Object> attachments, int color) { + ColorMesh(Vector4f[] vertices, Vector4f[] normals, int[] indices, Map<AttachmentType, Object> attachments, int color) { super(vertices, normals, indices, attachments); this.color = color; } @@ -120,7 +120,7 @@ public abstract class Mesh<T> implements Sampleable<T> { } @Override - public void sample(float b0, float b1, float b2, Vector3f normal, Void u1, Void u2, Void u3, Vector4f outColor) { + public void sample(float b0, float b1, float b2, Vector4f normal, Void u1, Void u2, Void u3, Vector4f outColor) { applyLighting(outColor.fromARGB(color), normal); } } @@ -129,7 +129,7 @@ public abstract class Mesh<T> implements Sampleable<T> { Texture texture; Vector2f[] texCoords; - TextureMesh(Vector3f[] vertices, Vector3f[] normals, int[] indices, Map<AttachmentType, Object> attachments, Texture tex, Vector2f[] texCoords) { + TextureMesh(Vector4f[] vertices, Vector4f[] normals, int[] indices, Map<AttachmentType, Object> attachments, Texture tex, Vector2f[] texCoords) { super(vertices, normals, indices, attachments); this.texture = tex; this.texCoords = texCoords; @@ -141,7 +141,7 @@ public abstract class Mesh<T> implements Sampleable<T> { } @Override - public void sample(float b0, float b1, float b2, Vector3f normal, Vector2f tc1, Vector2f tc2, Vector2f tc3, Vector4f color) { + public void sample(float b0, float b1, float b2, Vector4f normal, Vector2f tc1, Vector2f tc2, Vector2f tc3, Vector4f color) { float texX = b0 * tc1.x + b1 * tc2.x + b2 * tc3.x; float texY = b0 * tc1.y + b1 * tc2.y + b2 * tc3.y; @@ -154,6 +154,6 @@ public abstract class Mesh<T> implements Sampleable<T> { public enum AttachmentType { LIGHT_FACTOR, // float - LIGHT_VECTOR // Vector3f + LIGHT_VECTOR // Vector4f } } diff --git a/src/main/java/dev/figboot/cuberender/state/Sampleable.java b/src/main/java/dev/figboot/cuberender/state/Sampleable.java index 876a248..7b7ad7d 100644 --- a/src/main/java/dev/figboot/cuberender/state/Sampleable.java +++ b/src/main/java/dev/figboot/cuberender/state/Sampleable.java @@ -6,5 +6,5 @@ import dev.figboot.cuberender.math.Vector4f; public interface Sampleable<T> { T extra(int idx); - void sample(float b0, float b1, float b2, Vector3f normal, T e1, T e2, T e3, Vector4f target); + void sample(float b0, float b1, float b2, Vector4f normal, T e1, T e2, T e3, Vector4f outColor); } diff --git a/src/main/java/dev/figboot/cuberender/test/GraphicsPanel.java b/src/main/java/dev/figboot/cuberender/test/GraphicsPanel.java index 38dea64..fa5d954 100644 --- a/src/main/java/dev/figboot/cuberender/test/GraphicsPanel.java +++ b/src/main/java/dev/figboot/cuberender/test/GraphicsPanel.java @@ -1,8 +1,6 @@ package dev.figboot.cuberender.test; -import dev.figboot.cuberender.math.Matrix3f; -import dev.figboot.cuberender.math.Vector2f; -import dev.figboot.cuberender.math.Vector3f; +import dev.figboot.cuberender.math.*; import dev.figboot.cuberender.state.BlendMode; import dev.figboot.cuberender.state.Framebuffer; import dev.figboot.cuberender.state.Mesh; @@ -31,52 +29,52 @@ public class GraphicsPanel extends JPanel { boolean rollOver = false; private static void addCuboid(Mesh.Builder mb, float x1, float y1, float z1, float x2, float y2, float z2, float tx, float ty, float tspanX, float tspanY, float tspanZ, int ibase) { - mb.vertex(new Vector3f(x1, y1, z2), /* front */ - new Vector3f(x1, y2, z2), - new Vector3f(x2, y2, z2), - new Vector3f(x2, y1, z2), - - new Vector3f(x2, y1, z2), /* +X side */ - new Vector3f(x2, y2, z2), - new Vector3f(x2, y2, z1), - new Vector3f(x2, y1, z1), - - new Vector3f(x2, y1, z1), /* back */ - new Vector3f(x2, y2, z1), - new Vector3f(x1, y2, z1), - new Vector3f(x1, y1, z1), - - new Vector3f(x1, y1, z1), /* -X side */ - new Vector3f(x1, y2, z1), - new Vector3f(x1, y2, z2), - new Vector3f(x1, y1, z2), - - new Vector3f(x1, y1, z1), /* top */ - new Vector3f(x1, y1, z2), - new Vector3f(x2, y1, z2), - new Vector3f(x2, y1, z1), - - new Vector3f(x1, y2, z1), /* bottom */ - new Vector3f(x1, y2, z2), - new Vector3f(x2, y2, z2), - new Vector3f(x2, y2, z1)) - .normals(new Vector3f(0, 0, 1), - new Vector3f(0, 0, 1), - - new Vector3f(1, 0, 0), - new Vector3f(1, 0, 0), - - new Vector3f(0, 0, -1), - new Vector3f(0, 0, -1), - - new Vector3f(-1, 0, 0), - new Vector3f(-1, 0, 0), - - new Vector3f(0, -1, 0), - new Vector3f(0, -1, 0), - - new Vector3f(0, 1, 0), - new Vector3f(0, 1, 0)) + mb.vertex(new Vector4f(x1, y1, z2), /* front */ + new Vector4f(x1, y2, z2), + new Vector4f(x2, y2, z2), + new Vector4f(x2, y1, z2), + + new Vector4f(x2, y1, z2), /* +X side */ + new Vector4f(x2, y2, z2), + new Vector4f(x2, y2, z1), + new Vector4f(x2, y1, z1), + + new Vector4f(x2, y1, z1), /* back */ + new Vector4f(x2, y2, z1), + new Vector4f(x1, y2, z1), + new Vector4f(x1, y1, z1), + + new Vector4f(x1, y1, z1), /* -X side */ + new Vector4f(x1, y2, z1), + new Vector4f(x1, y2, z2), + new Vector4f(x1, y1, z2), + + new Vector4f(x1, y1, z1), /* top */ + new Vector4f(x1, y1, z2), + new Vector4f(x2, y1, z2), + new Vector4f(x2, y1, z1), + + new Vector4f(x1, y2, z1), /* bottom */ + new Vector4f(x1, y2, z2), + new Vector4f(x2, y2, z2), + new Vector4f(x2, y2, z1)) + .normals(new Vector4f(0, 0, 1, 0), + new Vector4f(0, 0, 1, 0), + + new Vector4f(1, 0, 0, 0), + new Vector4f(1, 0, 0, 0), + + new Vector4f(0, 0, -1, 0), + new Vector4f(0, 0, -1, 0), + + new Vector4f(-1, 0, 0, 0), + new Vector4f(-1, 0, 0, 0), + + new Vector4f(0, -1, 0, 0), + new Vector4f(0, -1, 0, 0), + + new Vector4f(0, 1, 0, 0), + new Vector4f(0, 1, 0, 0)) .texCoords(new Vector2f(tx, ty), new Vector2f(tx, ty - tspanY), new Vector2f(tx + tspanX, ty - tspanY), @@ -153,7 +151,7 @@ public class GraphicsPanel extends JPanel { private static Mesh.Builder defaultBuilder() { return new Mesh.Builder().attach(Mesh.AttachmentType.LIGHT_FACTOR, 1f) - .attach(Mesh.AttachmentType.LIGHT_VECTOR, new Vector3f(0, 0, 1)); + .attach(Mesh.AttachmentType.LIGHT_VECTOR, new Vector4f(0, 0, 1, 0)); } public GraphicsPanel() { @@ -301,7 +299,7 @@ public class GraphicsPanel extends JPanel { } private void updateTransform() { - framebuffer.setTransform(Matrix3f.rotateY(yRot).times(Matrix3f.rotateX(xRot)).times(Matrix3f.scaleX(0.5f).times(Matrix3f.scaleY(0.5f).times(Matrix3f.scaleZ(0.5f))))); + framebuffer.setTransform(Matrix4f.rotateY(yRot).times(Matrix4f.rotateX(xRot)).times(Matrix4f.scale(0.5f))); } @Override |
