diff --git a/cube.obj b/cube.obj new file mode 100644 index 0000000..c59f3ca --- /dev/null +++ b/cube.obj @@ -0,0 +1,26 @@ +v 0 0 0 +v 0 0 1 +v 0 1 0 +v 0 1 1 +v 1 0 0 +v 1 0 1 +v 1 1 0 +v 1 1 1 + +vt 0 0 #1 +vt 0 1 #2 +vt 1 0 #3 +vt 1 1 #4 + +f 1/2 3/1 7/3 +f 1/2 7/3 8/4 +f 5/2 7/1 8/3 +f 5/2 8/3 6/4 +f 6/2 8/1 4/3 +f 6/2 4/3 2/4 +f 2/2 4/1 3/3 +f 2/2 3/3 1/4 +f 3/2 4/1 8/3 +f 3/2 8/3 7/4 +f 6/2 2/1 1/3 +f 6/2 1/3 5/4 \ No newline at end of file diff --git a/src/sig/Mesh.java b/src/sig/Mesh.java index de2afd2..d733cbf 100644 --- a/src/sig/Mesh.java +++ b/src/sig/Mesh.java @@ -1,11 +1,24 @@ package sig; +import java.io.File; import java.util.ArrayList; import java.util.List; +import sig.utils.OBJReader; + public class Mesh { List triangles = new ArrayList<>(); Mesh(List tris) { this.triangles=tris; } + Mesh(String obj) { + this.triangles=OBJReader.ReadOBJFile(obj,false); + } + Mesh(String obj,String tex) { + this.triangles=OBJReader.ReadOBJFile(obj,true); + Texture te = new Texture(new File(tex)); + for (Triangle t : triangles) { + t.tex=te; + } + } } diff --git a/src/sig/Panel.java b/src/sig/Panel.java index ffdea77..3e9e9d9 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -114,7 +114,7 @@ public class Panel extends JPanel implements Runnable { triTransformed.T = t.T; triTransformed.U = t.U; triTransformed.V = t.V; - triTransformed.textured = t.textured; + triTransformed.tex = t.tex; Vector normal=new Vector(),line1=new Vector(),line2=new Vector(); line1 = Vector.subtract(triTransformed.B,triTransformed.A); @@ -139,7 +139,7 @@ public class Panel extends JPanel implements Runnable { triViewed.T = triTransformed.T; triViewed.U = triTransformed.U; triViewed.V = triTransformed.V; - triViewed.textured = triTransformed.textured; + triViewed.tex = triTransformed.tex; int clippedTriangles = 0; Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; @@ -153,7 +153,7 @@ public class Panel extends JPanel implements Runnable { triProjected.B = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].B); triProjected.C = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].C); triProjected.col = clipped[i].col; - triProjected.textured = clipped[i].textured; + triProjected.tex = clipped[i].tex; triProjected.T = (Vector2)clipped[i].T.clone(); triProjected.U = (Vector2)clipped[i].U.clone(); triProjected.V = (Vector2)clipped[i].V.clone(); @@ -230,12 +230,12 @@ public class Panel extends JPanel implements Runnable { } for (Triangle tt : triList) { - if (tt.textured) { + if (tt.tex!=null) { DrawUtils.TexturedTriangle(p, (int)tt.A.x,(int)tt.A.y,tt.T.u,tt.T.v,tt.T.w, (int)tt.B.x,(int)tt.B.y,tt.U.u,tt.U.v,tt.U.w, (int)tt.C.x,(int)tt.C.y,tt.V.u,tt.V.v,tt.V.w, - SigRenderer.dirtTex,tt.col.getRed()); + tt.tex,tt.col.getRed()); } else { DrawUtils.FillTriangle(p,(int)tt.A.x,(int)tt.A.y,(int)tt.B.x,(int)tt.B.y,(int)tt.C.x,(int)tt.C.y,tt.getColor()); } diff --git a/src/sig/SigRenderer.java b/src/sig/SigRenderer.java index eadbd72..27c0d0a 100644 --- a/src/sig/SigRenderer.java +++ b/src/sig/SigRenderer.java @@ -79,8 +79,6 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene } SigRenderer(JFrame f) { - dirtTex = new Texture(new File("dirt.png")); - //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false)); cube = new Mesh(Arrays.asList( new Triangle[]{ @@ -98,10 +96,6 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene new Triangle(new Vector(1,0,1),new Vector(0,0,0),new Vector(1,0,0),new Vector2(0,1),new Vector2(1,0),new Vector2(1,1)), })); - for (Triangle t : cube.triangles) { - t.textured=true; - } - Panel p = new Panel(); f.getContentPane().addMouseListener(this); diff --git a/src/sig/Triangle.java b/src/sig/Triangle.java index 77ffedc..a63c48b 100644 --- a/src/sig/Triangle.java +++ b/src/sig/Triangle.java @@ -5,7 +5,7 @@ public class Triangle { Vector A,B,C; Vector2 T,U,V; Color col = Color.WHITE; - public boolean textured=false; + public Texture tex = null; public Triangle() { this(new Vector(),new Vector(),new Vector()); } @@ -87,7 +87,7 @@ public class Triangle { if (insidePointCount==1&&outsidePointCount==2) { ExtraData t = new ExtraData(0); out_tri[0].col = in.col; - out_tri[0].textured = in.textured; + out_tri[0].tex = in.tex; out_tri[0].A = inside_points[0]; out_tri[0].T = inside_tex[0]; out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0],t); @@ -103,7 +103,7 @@ public class Triangle { if (insidePointCount==2&&outsidePointCount==1) { ExtraData t = new ExtraData(0); out_tri[0].col=out_tri[1].col=in.col; - out_tri[0].textured=out_tri[1].textured=in.textured; + out_tri[0].tex=out_tri[1].tex=in.tex; out_tri[0].A = inside_points[0]; out_tri[0].B = inside_points[1]; out_tri[0].T = inside_tex[0]; diff --git a/src/sig/utils/OBJReader.java b/src/sig/utils/OBJReader.java index 90c67c7..45bec49 100644 --- a/src/sig/utils/OBJReader.java +++ b/src/sig/utils/OBJReader.java @@ -31,26 +31,25 @@ public class OBJReader { Triangle tri = new Triangle( new Vector( vertices.get(Integer.parseInt(spl1[0])-1)[0], - vertices.get(Integer.parseInt(spl2[0])-1)[1], - vertices.get(Integer.parseInt(spl3[0])-1)[2]), + vertices.get(Integer.parseInt(spl1[0])-1)[1], + vertices.get(Integer.parseInt(spl1[0])-1)[2]), new Vector( - vertices.get(Integer.parseInt(spl1[0])-1)[0], + vertices.get(Integer.parseInt(spl2[0])-1)[0], vertices.get(Integer.parseInt(spl2[0])-1)[1], - vertices.get(Integer.parseInt(spl3[0])-1)[2]), + vertices.get(Integer.parseInt(spl2[0])-1)[2]), new Vector( - vertices.get(Integer.parseInt(spl1[0])-1)[0], - vertices.get(Integer.parseInt(spl2[0])-1)[1], + vertices.get(Integer.parseInt(spl3[0])-1)[0], + vertices.get(Integer.parseInt(spl3[0])-1)[1], vertices.get(Integer.parseInt(spl3[0])-1)[2]), new Vector2( texs.get(Integer.parseInt(spl1[1])-1)[0], - texs.get(Integer.parseInt(spl2[1])-1)[1]), + texs.get(Integer.parseInt(spl1[1])-1)[1]), new Vector2( - texs.get(Integer.parseInt(spl1[1])-1)[0], + texs.get(Integer.parseInt(spl2[1])-1)[0], texs.get(Integer.parseInt(spl2[1])-1)[1]), new Vector2( - texs.get(Integer.parseInt(spl1[1])-1)[0], - texs.get(Integer.parseInt(spl2[1])-1)[1])); - tri.textured=textured; + texs.get(Integer.parseInt(spl3[1])-1)[0], + texs.get(Integer.parseInt(spl3[1])-1)[1])); tris.add(tri); } else { Triangle tri = new Triangle( @@ -66,7 +65,6 @@ public class OBJReader { vertices.get(Integer.parseInt(split[3])-1)[0], vertices.get(Integer.parseInt(split[3])-1)[1], vertices.get(Integer.parseInt(split[3])-1)[2])); - tri.textured=textured; tris.add(tri); } }