From 16e2b942bbedd779b2c73f7aa404478b8f2b5a06 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Thu, 4 Nov 2021 13:35:30 +0000 Subject: [PATCH] Make sure untextured surfaces still read properly. Co-authored-by: sigonasr2 --- src/sig/Panel.java | 27 ++++++++++++++++++--------- src/sig/SigRenderer.java | 4 +++- src/sig/utils/DrawUtils.java | 2 +- src/sig/utils/OBJReader.java | 21 ++++++++++++--------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/sig/Panel.java b/src/sig/Panel.java index 0b8445a..5903ceb 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -106,7 +106,9 @@ public class Panel extends JPanel implements Runnable { for (Triangle t : SigRenderer.triRender) { Triangle triProjected = new Triangle(),triTransformed=new Triangle(),triViewed=new Triangle(); - matWorld = Matrix.MakeTranslation(t.b.pos.x,t.b.pos.y,t.b.pos.z); + if (t.b!=null) { + matWorld = Matrix.MakeTranslation(t.b.pos.x,t.b.pos.y,t.b.pos.z); + } triTransformed.A = Matrix.MultiplyVector(matWorld,t.A); triTransformed.B = Matrix.MultiplyVector(matWorld,t.B); @@ -127,25 +129,32 @@ public class Panel extends JPanel implements Runnable { Vector cameraRay = Vector.subtract(triTransformed.A,SigRenderer.vCamera); if (Vector.dotProduct(normal,cameraRay)<0) { - /*Vector lightDir = Vector.multiply(SigRenderer.vLookDir, -1); - lightDir = Vector.normalize(lightDir);*/ + Vector lightDir = Vector.multiply(SigRenderer.vLookDir, -1); + lightDir = Vector.normalize(lightDir); //System.out.println(-Vector.dotProduct(normal,Vector.normalize(cameraRay))); - //float dp = Math.max(0.1f,Math.min(1,-1/Vector.dotProduct(normal,cameraRay))); + float dp = 0.1f; + if (t.b!=null) { + dp = Math.max(0.1f,Math.min(1,(1f/((triTransformed.b.pos.x-SigRenderer.vCamera.x)*(triTransformed.b.pos.x-SigRenderer.vCamera.x)+ + (triTransformed.b.pos.y-SigRenderer.vCamera.y)*(triTransformed.b.pos.y-SigRenderer.vCamera.y)+ + (triTransformed.b.pos.z-SigRenderer.vCamera.z)*(triTransformed.b.pos.z-SigRenderer.vCamera.z))*4))); + } else { + dp = Math.max(0.1f,Vector.dotProduct(lightDir,normal)); + } /*Vector center = Vector.divide(Vector.add(triTransformed.A,Vector.add(triTransformed.B,triTransformed.C)),3); Vector cameraRay2 = Vector.subtract(center,SigRenderer.vCamera); float dp = Math.max(0.1f,Math.min(1,(1f/((cameraRay2.x-center.x)*(cameraRay2.x-center.x)+ (cameraRay2.y-center.y)*(cameraRay2.y-center.y)+ (cameraRay2.z-center.z)*(cameraRay2.z-center.z))*4)));*/ - float dp = Math.max(0.1f,Math.min(1,(1f/((triTransformed.b.pos.x-SigRenderer.vCamera.x)*(triTransformed.b.pos.x-SigRenderer.vCamera.x)+ + /*float dp = Math.max(0.1f,Math.min(1,(1f/((triTransformed.b.pos.x-SigRenderer.vCamera.x)*(triTransformed.b.pos.x-SigRenderer.vCamera.x)+ (triTransformed.b.pos.y-SigRenderer.vCamera.y)*(triTransformed.b.pos.y-SigRenderer.vCamera.y)+ - (triTransformed.b.pos.z-SigRenderer.vCamera.z)*(triTransformed.b.pos.z-SigRenderer.vCamera.z))*4))); + (triTransformed.b.pos.z-SigRenderer.vCamera.z)*(triTransformed.b.pos.z-SigRenderer.vCamera.z))*4)));*/ triViewed.A = Matrix.MultiplyVector(matView,triTransformed.A); triViewed.B = Matrix.MultiplyVector(matView,triTransformed.B); triViewed.C = Matrix.MultiplyVector(matView,triTransformed.C); triTransformed.copyExtraDataTo(triViewed); - triViewed.setColor(new Color(dp,dp,dp)); + triViewed.setColor(new Color(dp,0,0)); int clippedTriangles = 0; Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; @@ -203,14 +212,14 @@ public class Panel extends JPanel implements Runnable { } } - /*Collections.sort(accumulatedTris, new Comparator() { + Collections.sort(accumulatedTris, new Comparator() { @Override public int compare(Triangle t1, Triangle t2) { float z1=(t1.A.z+t1.B.z+t1.C.z)/3f; float z2=(t2.A.z+t2.B.z+t2.C.z)/3f; return (z1SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j]) { Draw(canvas,j,i,texture.getColor(tex_u/tex_w,tex_v/tex_w,colorMult/255f)); SigRenderer.depthBuffer[i*SigRenderer.SCREEN_WIDTH+j] = tex_w; - } + } t+=tstep; } } diff --git a/src/sig/utils/OBJReader.java b/src/sig/utils/OBJReader.java index 45bec49..7c51e29 100644 --- a/src/sig/utils/OBJReader.java +++ b/src/sig/utils/OBJReader.java @@ -52,19 +52,22 @@ public class OBJReader { texs.get(Integer.parseInt(spl3[1])-1)[1])); tris.add(tri); } else { + 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(split[1])-1)[0], - vertices.get(Integer.parseInt(split[1])-1)[1], - vertices.get(Integer.parseInt(split[1])-1)[2]), + vertices.get(Integer.parseInt(spl1[0])-1)[0], + vertices.get(Integer.parseInt(spl1[0])-1)[1], + vertices.get(Integer.parseInt(spl1[0])-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]), + vertices.get(Integer.parseInt(spl2[0])-1)[0], + vertices.get(Integer.parseInt(spl2[0])-1)[1], + vertices.get(Integer.parseInt(spl2[0])-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])); + vertices.get(Integer.parseInt(spl3[0])-1)[0], + vertices.get(Integer.parseInt(spl3[0])-1)[1], + vertices.get(Integer.parseInt(spl3[0])-1)[2])); tris.add(tri); } }