Textured cube with model loading.

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
origin
sigonasr2, Sig, Sigo 3 years ago
parent aade867128
commit 0192ab1ebe
  1. 26
      cube.obj
  2. 13
      src/sig/Mesh.java
  3. 10
      src/sig/Panel.java
  4. 6
      src/sig/SigRenderer.java
  5. 6
      src/sig/Triangle.java
  6. 22
      src/sig/utils/OBJReader.java

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

@ -1,11 +1,24 @@
package sig; package sig;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import sig.utils.OBJReader;
public class Mesh { public class Mesh {
List<Triangle> triangles = new ArrayList<>(); List<Triangle> triangles = new ArrayList<>();
Mesh(List<Triangle> tris) { Mesh(List<Triangle> tris) {
this.triangles=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;
}
}
} }

@ -114,7 +114,7 @@ public class Panel extends JPanel implements Runnable {
triTransformed.T = t.T; triTransformed.T = t.T;
triTransformed.U = t.U; triTransformed.U = t.U;
triTransformed.V = t.V; triTransformed.V = t.V;
triTransformed.textured = t.textured; triTransformed.tex = t.tex;
Vector normal=new Vector(),line1=new Vector(),line2=new Vector(); Vector normal=new Vector(),line1=new Vector(),line2=new Vector();
line1 = Vector.subtract(triTransformed.B,triTransformed.A); line1 = Vector.subtract(triTransformed.B,triTransformed.A);
@ -139,7 +139,7 @@ public class Panel extends JPanel implements Runnable {
triViewed.T = triTransformed.T; triViewed.T = triTransformed.T;
triViewed.U = triTransformed.U; triViewed.U = triTransformed.U;
triViewed.V = triTransformed.V; triViewed.V = triTransformed.V;
triViewed.textured = triTransformed.textured; triViewed.tex = triTransformed.tex;
int clippedTriangles = 0; int clippedTriangles = 0;
Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; 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.B = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].B);
triProjected.C = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].C); triProjected.C = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].C);
triProjected.col = clipped[i].col; triProjected.col = clipped[i].col;
triProjected.textured = clipped[i].textured; triProjected.tex = clipped[i].tex;
triProjected.T = (Vector2)clipped[i].T.clone(); triProjected.T = (Vector2)clipped[i].T.clone();
triProjected.U = (Vector2)clipped[i].U.clone(); triProjected.U = (Vector2)clipped[i].U.clone();
triProjected.V = (Vector2)clipped[i].V.clone(); triProjected.V = (Vector2)clipped[i].V.clone();
@ -230,12 +230,12 @@ public class Panel extends JPanel implements Runnable {
} }
for (Triangle tt : triList) { for (Triangle tt : triList) {
if (tt.textured) { if (tt.tex!=null) {
DrawUtils.TexturedTriangle(p, DrawUtils.TexturedTriangle(p,
(int)tt.A.x,(int)tt.A.y,tt.T.u,tt.T.v,tt.T.w, (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.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, (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 { } 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()); 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());
} }

@ -79,8 +79,6 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
} }
SigRenderer(JFrame f) { SigRenderer(JFrame f) {
dirtTex = new Texture(new File("dirt.png"));
//cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false)); //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false));
cube = new Mesh(Arrays.asList( cube = new Mesh(Arrays.asList(
new Triangle[]{ 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)), 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(); Panel p = new Panel();
f.getContentPane().addMouseListener(this); f.getContentPane().addMouseListener(this);

@ -5,7 +5,7 @@ public class Triangle {
Vector A,B,C; Vector A,B,C;
Vector2 T,U,V; Vector2 T,U,V;
Color col = Color.WHITE; Color col = Color.WHITE;
public boolean textured=false; public Texture tex = null;
public Triangle() { public Triangle() {
this(new Vector(),new Vector(),new Vector()); this(new Vector(),new Vector(),new Vector());
} }
@ -87,7 +87,7 @@ public class Triangle {
if (insidePointCount==1&&outsidePointCount==2) { if (insidePointCount==1&&outsidePointCount==2) {
ExtraData t = new ExtraData(0); ExtraData t = new ExtraData(0);
out_tri[0].col = in.col; 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].A = inside_points[0];
out_tri[0].T = inside_tex[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); 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) { if (insidePointCount==2&&outsidePointCount==1) {
ExtraData t = new ExtraData(0); ExtraData t = new ExtraData(0);
out_tri[0].col=out_tri[1].col=in.col; 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].A = inside_points[0];
out_tri[0].B = inside_points[1]; out_tri[0].B = inside_points[1];
out_tri[0].T = inside_tex[0]; out_tri[0].T = inside_tex[0];

@ -31,26 +31,25 @@ public class OBJReader {
Triangle tri = new Triangle( Triangle tri = new Triangle(
new Vector( new Vector(
vertices.get(Integer.parseInt(spl1[0])-1)[0], vertices.get(Integer.parseInt(spl1[0])-1)[0],
vertices.get(Integer.parseInt(spl2[0])-1)[1], vertices.get(Integer.parseInt(spl1[0])-1)[1],
vertices.get(Integer.parseInt(spl3[0])-1)[2]), vertices.get(Integer.parseInt(spl1[0])-1)[2]),
new Vector( 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(spl2[0])-1)[1],
vertices.get(Integer.parseInt(spl3[0])-1)[2]), vertices.get(Integer.parseInt(spl2[0])-1)[2]),
new Vector( new Vector(
vertices.get(Integer.parseInt(spl1[0])-1)[0], vertices.get(Integer.parseInt(spl3[0])-1)[0],
vertices.get(Integer.parseInt(spl2[0])-1)[1], vertices.get(Integer.parseInt(spl3[0])-1)[1],
vertices.get(Integer.parseInt(spl3[0])-1)[2]), vertices.get(Integer.parseInt(spl3[0])-1)[2]),
new Vector2( new Vector2(
texs.get(Integer.parseInt(spl1[1])-1)[0], 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( 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]), texs.get(Integer.parseInt(spl2[1])-1)[1]),
new Vector2( new Vector2(
texs.get(Integer.parseInt(spl1[1])-1)[0], texs.get(Integer.parseInt(spl3[1])-1)[0],
texs.get(Integer.parseInt(spl2[1])-1)[1])); texs.get(Integer.parseInt(spl3[1])-1)[1]));
tri.textured=textured;
tris.add(tri); tris.add(tri);
} else { } else {
Triangle tri = new Triangle( 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)[0],
vertices.get(Integer.parseInt(split[3])-1)[1], vertices.get(Integer.parseInt(split[3])-1)[1],
vertices.get(Integer.parseInt(split[3])-1)[2])); vertices.get(Integer.parseInt(split[3])-1)[2]));
tri.textured=textured;
tris.add(tri); tris.add(tri);
} }
} }

Loading…
Cancel
Save