Pass by value instead of pass by reference for arrays right now....bugs out.

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
origin
sigonasr2, Sig, Sigo 3 years ago
parent 129d0e71b1
commit 2e86db49f2
  1. 3
      src/sig/Panel.java
  2. 4
      src/sig/SigRenderer.java
  3. 21
      src/sig/Triangle.java
  4. 4
      src/sig/Vector.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);

@ -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<Pixel> 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();

@ -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;

@ -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);

Loading…
Cancel
Save