From aade86712881e6067c7e255338ed0c8e6e674433 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Wed, 3 Nov 2021 13:54:04 +0000 Subject: [PATCH] Texture lighting / color configuration. Co-authored-by: sigonasr2 --- src/sig/Panel.java | 21 ++++++++---- src/sig/SigRenderer.java | 9 +++-- src/sig/Texture.java | 5 +-- src/sig/Triangle.java | 6 +++- src/sig/Vector2.java | 4 +++ src/sig/utils/DrawUtils.java | 6 ++-- src/sig/utils/OBJReader.java | 65 ++++++++++++++++++++++++++++-------- 7 files changed, 87 insertions(+), 29 deletions(-) diff --git a/src/sig/Panel.java b/src/sig/Panel.java index 3ac18fc..ffdea77 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -17,8 +17,6 @@ import java.awt.GraphicsEnvironment; import java.awt.GraphicsConfiguration; import java.awt.Toolkit; -import sig.Texture; - public class Panel extends JPanel implements Runnable { long startTime = System.nanoTime(); long endTime = System.nanoTime(); @@ -77,6 +75,8 @@ public class Panel extends JPanel implements Runnable { int[] p = pixel; // this avoid crash when resizing //a=h/w + fTheta+=0.05f; + final int h=SigRenderer.SCREEN_HEIGHT; if(p.length != width * height) return; for (int x=0;x=tex.getWidth()||sy<0||sy>=tex.getHeight()) { return new Color(0,0,0,0); } else { - return new Color(tex.getRGB(sx,sy)); + Color newCol = new Color(tex.getRGB(sx,sy)); + return new Color((newCol.getRed()/255f)*mult,(newCol.getGreen()/255f)*mult,(newCol.getBlue()/255f)*mult); } } diff --git a/src/sig/Triangle.java b/src/sig/Triangle.java index b26f9d3..77ffedc 100644 --- a/src/sig/Triangle.java +++ b/src/sig/Triangle.java @@ -5,6 +5,7 @@ public class Triangle { Vector A,B,C; Vector2 T,U,V; Color col = Color.WHITE; + public boolean textured=false; public Triangle() { this(new Vector(),new Vector(),new Vector()); } @@ -27,7 +28,8 @@ public class Triangle { } @Override public String toString() { - return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", col=" + col + "]"; + return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", T=" + T + ", U=" + U + ", V=" + V + ", col=" + col + + "]"; } public Color getColor() { return col; @@ -85,6 +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].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); @@ -100,6 +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].A = inside_points[0]; out_tri[0].B = inside_points[1]; out_tri[0].T = inside_tex[0]; diff --git a/src/sig/Vector2.java b/src/sig/Vector2.java index 55558ff..e5090a9 100644 --- a/src/sig/Vector2.java +++ b/src/sig/Vector2.java @@ -17,4 +17,8 @@ public class Vector2 { protected Object clone(){ return new Vector2(u,v,w); } + @Override + public String toString() { + return "Vector2 [u=" + u + ", v=" + v + ", w=" + w + "]"; + } } diff --git a/src/sig/utils/DrawUtils.java b/src/sig/utils/DrawUtils.java index 6b41ed2..a71faa6 100644 --- a/src/sig/utils/DrawUtils.java +++ b/src/sig/utils/DrawUtils.java @@ -17,7 +17,7 @@ public class DrawUtils { int x1, int y1, float u1,float v1,float w1, int x2, int y2, float u2,float v2,float w2, int x3, int y3, float u3,float v3,float w3, - Texture texture + Texture texture, int colorMult ) { if (y2 ReadOBJFile(String f) { + public static List ReadOBJFile(String f,boolean textured) { String[] data = FileUtils.readFromFile(f); List vertices = new ArrayList<>(); + List texs = new ArrayList<>(); List tris = new ArrayList<>(); for (String s : data) { String[] split = s.split(Pattern.quote(" ")); if (split[0].equalsIgnoreCase("v")) { vertices.add(new float[]{Float.parseFloat(split[1]),Float.parseFloat(split[2]),Float.parseFloat(split[3])}); } else + if (split[0].equalsIgnoreCase("vt")) { + texs.add(new float[]{Float.parseFloat(split[1]),Float.parseFloat(split[2])}); + } else if (split[0].equalsIgnoreCase("f")) { - tris.add(new Triangle( - new Vector( - vertices.get(Integer.parseInt(split[1])-1)[0], - vertices.get(Integer.parseInt(split[1])-1)[1], - vertices.get(Integer.parseInt(split[1])-1)[2]), - new Vector( - vertices.get(Integer.parseInt(split[2])-1)[0], - vertices.get(Integer.parseInt(split[2])-1)[1], - vertices.get(Integer.parseInt(split[2])-1)[2]), - new Vector( - vertices.get(Integer.parseInt(split[3])-1)[0], - vertices.get(Integer.parseInt(split[3])-1)[1], - vertices.get(Integer.parseInt(split[3])-1)[2]))); + if (textured) { + String[] spl1=split[1].split(Pattern.quote("/")); + String[] spl2=split[2].split(Pattern.quote("/")); + String[] spl3=split[3].split(Pattern.quote("/")); + 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]), + 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]), + 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]), + new Vector2( + texs.get(Integer.parseInt(spl1[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]), + new Vector2( + texs.get(Integer.parseInt(spl1[1])-1)[0], + texs.get(Integer.parseInt(spl2[1])-1)[1])); + tri.textured=textured; + tris.add(tri); + } else { + Triangle tri = new Triangle( + new Vector( + vertices.get(Integer.parseInt(split[1])-1)[0], + vertices.get(Integer.parseInt(split[1])-1)[1], + vertices.get(Integer.parseInt(split[1])-1)[2]), + new Vector( + vertices.get(Integer.parseInt(split[2])-1)[0], + vertices.get(Integer.parseInt(split[2])-1)[1], + vertices.get(Integer.parseInt(split[2])-1)[2]), + new Vector( + 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); + } } } return tris;