From 2e86db49f24da8560348569273bc531e13baa4d4 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Tue, 2 Nov 2021 20:43:12 +0000 Subject: [PATCH] Pass by value instead of pass by reference for arrays right now....bugs out. Co-authored-by: sigonasr2 --- src/sig/Panel.java | 3 ++- src/sig/SigRenderer.java | 4 +--- src/sig/Triangle.java | 21 +++++++++++---------- src/sig/Vector.java | 4 ++++ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/sig/Panel.java b/src/sig/Panel.java index b53a4e7..b3069ea 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -129,6 +129,7 @@ public class Panel extends JPanel implements Runnable { triViewed.A = Matrix.MultiplyVector(matView,triTransformed.A); triViewed.B = Matrix.MultiplyVector(matView,triTransformed.B); triViewed.C = Matrix.MultiplyVector(matView,triTransformed.C); + triProjected.setColor(new Color(dp,dp,dp)); int clippedTriangles = 0; Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; @@ -138,7 +139,7 @@ public class Panel extends JPanel implements Runnable { triProjected.A = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].A); triProjected.B = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].B); triProjected.C = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].C); - triProjected.setColor(new Color(dp,dp,dp)); + triProjected.col = clipped[i].col; triProjected.A = Vector.divide(triProjected.A, triProjected.A.w); triProjected.B = Vector.divide(triProjected.B, triProjected.B.w); diff --git a/src/sig/SigRenderer.java b/src/sig/SigRenderer.java index ea08e70..40dab7d 100644 --- a/src/sig/SigRenderer.java +++ b/src/sig/SigRenderer.java @@ -23,8 +23,6 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene public static float DRAWTIME=0; public static float DRAWLOOPTIME=0; public static final float RESOLUTION=1; - - public static Vector origin = new Vector(0,0,10); public static float rot = (float)Math.PI/4; //In radians. public static List pixels; @@ -76,7 +74,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene } SigRenderer(JFrame f) { - cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj")); + cube = new Mesh(OBJReader.ReadOBJFile("axis.obj")); Panel p = new Panel(); diff --git a/src/sig/Triangle.java b/src/sig/Triangle.java index 8724cb6..0c85534 100644 --- a/src/sig/Triangle.java +++ b/src/sig/Triangle.java @@ -1,5 +1,6 @@ package sig; import java.awt.Color; +import java.util.Arrays; public class Triangle { Vector A,B,C; @@ -20,7 +21,7 @@ public class Triangle { } @Override public String toString() { - return "Triangle [A=" + A + ", B=" + B + ", C=" + C + "]"; + return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", col=" + col + "]"; } public Color getColor() { return col; @@ -30,14 +31,13 @@ public class Triangle { } static float dist(Vector plane_p,Vector plane_n,Vector p) { - Vector n = Vector.normalize(p); return plane_n.x*p.x+plane_n.y*p.y+plane_n.z*p.z-Vector.dotProduct(plane_n,plane_p); } public static int ClipAgainstPlane(Vector plane_p,Vector plane_n,Triangle in,Triangle[] out_tri) { plane_n = Vector.normalize(plane_n); - Vector[] inside_points = new Vector[3]; - Vector[] outside_points = new Vector[3]; + Vector[] inside_points = new Vector[]{new Vector(),new Vector(),new Vector()}; + Vector[] outside_points = new Vector[]{new Vector(),new Vector(),new Vector()}; int insidePointCount=0,outsidePointCount=0; float d0=dist(plane_p,plane_n,in.A); @@ -63,15 +63,20 @@ public class Triangle { if (insidePointCount==0) { return 0; } else + if (insidePointCount==3) { + out_tri[0] = in; + return 1; + } else if (insidePointCount==1&&outsidePointCount==2) { - out_tri[0].col = in.col; + out_tri[0].col = Color.BLUE; out_tri[0].A = inside_points[0]; out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); out_tri[0].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[1]); return 1; } else if (insidePointCount==2&&outsidePointCount==1) { - out_tri[0].col=out_tri[1].col=in.col; + out_tri[0].col=Color.RED; + out_tri[1].col=Color.GREEN; out_tri[0].A = inside_points[0]; out_tri[0].B = inside_points[1]; out_tri[0].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); @@ -79,10 +84,6 @@ public class Triangle { out_tri[1].B = out_tri[0].C; out_tri[1].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[1], outside_points[0]); return 2; - } else - if (insidePointCount==3) { - out_tri[0] = in; - return 1; } return 0; diff --git a/src/sig/Vector.java b/src/sig/Vector.java index b3b8151..8842f2f 100644 --- a/src/sig/Vector.java +++ b/src/sig/Vector.java @@ -49,6 +49,10 @@ public class Vector { return v; } + @Override + public String toString() { + return "Vector ["+x+","+y+","+z+","+w+"]"; + } public static Vector IntersectPlane(Vector plane_p,Vector plane_n,Vector lineStart,Vector lineEnd) { plane_n = Vector.normalize(plane_n); float plane_d = -Vector.dotProduct(plane_n,plane_p);