From 234520ced4d7d4f669594cda1b1cf3246f7692ce Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Sun, 7 Nov 2021 05:43:24 +0900 Subject: [PATCH] Improvements to rendering speeds. --- src/sig/Panel.java | 200 +++++++++++++++++++---------------- src/sig/SigRenderer.java | 10 +- src/sig/Triangle.java | 6 ++ src/sig/utils/DrawUtils.java | 2 + 4 files changed, 125 insertions(+), 93 deletions(-) diff --git a/src/sig/Panel.java b/src/sig/Panel.java index 5d3bec9..80178f2 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -9,6 +9,7 @@ import java.awt.Color; import java.awt.Image; import java.awt.image.MemoryImageSource; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.awt.image.ColorModel; import java.awt.GraphicsEnvironment; @@ -65,6 +66,7 @@ public class Panel extends JPanel implements Runnable { thread.start(); } SigRenderer.depthBuffer = new float[width*height]; + SigRenderer.depthBuffer_tri = new Triangle[width*height]; } /** * Do your draws in here !! @@ -84,6 +86,7 @@ public class Panel extends JPanel implements Runnable { for (int y=0;y0.1f&&distSquared<4096) { + Vector lightDir = Vector.multiply(SigRenderer.vLookDir, -1); + lightDir = Vector.normalize(lightDir); + + //System.out.println(-Vector.dotProduct(normal,Vector.normalize(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))*64)))*0.5f+Math.max(0.1f,Math.min(1,1-Vector.dotProduct(normal,SigRenderer.vLookDir)))*0.5f; + } 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)+ (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))*64)))*0.5f+Math.max(0.1f,Math.min(1,1-Vector.dotProduct(normal,SigRenderer.vLookDir)))*0.5f; - } 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)+ - (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((0)+(0<<8)+((int)(dp*255)<<16)); + 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((0)+(0<<8)+((int)(dp*255)<<16)); - int clippedTriangles = 0; - Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; + int clippedTriangles = 0; + Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; - clippedTriangles = Triangle.ClipAgainstPlane(new Vector(0,0,0.1f),new Vector(0,0,1), triViewed, clipped); - for (int i=0;i0) { - triProjected = new Triangle(); - } - 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.col = clipped[i].col; - 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(); - triProjected.b=clipped[i].b; + clippedTriangles = Triangle.ClipAgainstPlane(new Vector(0,0,0.1f),new Vector(0,0,1), triViewed, clipped); + for (int i=0;i0) { + triProjected = new Triangle(); + } + 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.col = clipped[i].col; + 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(); + triProjected.b=clipped[i].b; + triProjected.unmodifiedTri=clipped[i].unmodifiedTri; - triProjected.T.u = triProjected.T.u/triProjected.A.w; - triProjected.U.u = triProjected.U.u/triProjected.B.w; - triProjected.V.u = triProjected.V.u/triProjected.C.w; - triProjected.T.v = triProjected.T.v/triProjected.A.w; - triProjected.U.v = triProjected.U.v/triProjected.B.w; - triProjected.V.v = triProjected.V.v/triProjected.C.w; + triProjected.T.u = triProjected.T.u/triProjected.A.w; + triProjected.U.u = triProjected.U.u/triProjected.B.w; + triProjected.V.u = triProjected.V.u/triProjected.C.w; + triProjected.T.v = triProjected.T.v/triProjected.A.w; + triProjected.U.v = triProjected.U.v/triProjected.B.w; + triProjected.V.v = triProjected.V.v/triProjected.C.w; - triProjected.T.w = 1.0f/triProjected.A.w; - triProjected.U.w = 1.0f/triProjected.B.w; - triProjected.V.w = 1.0f/triProjected.C.w; + triProjected.T.w = 1.0f/triProjected.A.w; + triProjected.U.w = 1.0f/triProjected.B.w; + triProjected.V.w = 1.0f/triProjected.C.w; - triProjected.A = Vector.divide(triProjected.A, triProjected.A.w); - triProjected.B = Vector.divide(triProjected.B, triProjected.B.w); - triProjected.C = Vector.divide(triProjected.C, triProjected.C.w); + triProjected.A = Vector.divide(triProjected.A, triProjected.A.w); + triProjected.B = Vector.divide(triProjected.B, triProjected.B.w); + triProjected.C = Vector.divide(triProjected.C, triProjected.C.w); - triProjected.A.x*=-1f; - triProjected.A.y*=-1f; - triProjected.B.x*=-1f; - triProjected.B.y*=-1f; - triProjected.C.x*=-1f; - triProjected.C.y*=-1f; + triProjected.A.x*=-1f; + triProjected.A.y*=-1f; + triProjected.B.x*=-1f; + triProjected.B.y*=-1f; + triProjected.C.x*=-1f; + triProjected.C.y*=-1f; - Vector viewOffset = new Vector(1,1,0); - triProjected.A = Vector.add(triProjected.A,viewOffset); - triProjected.B = Vector.add(triProjected.B,viewOffset); - triProjected.C = Vector.add(triProjected.C,viewOffset); - triProjected.A.x*=0.5f*SigRenderer.SCREEN_WIDTH; - triProjected.A.y*=0.5f*SigRenderer.SCREEN_HEIGHT; - triProjected.B.x*=0.5f*SigRenderer.SCREEN_WIDTH; - triProjected.B.y*=0.5f*SigRenderer.SCREEN_HEIGHT; - triProjected.C.x*=0.5f*SigRenderer.SCREEN_WIDTH; - triProjected.C.y*=0.5f*SigRenderer.SCREEN_HEIGHT; + Vector viewOffset = new Vector(1,1,0); + triProjected.A = Vector.add(triProjected.A,viewOffset); + triProjected.B = Vector.add(triProjected.B,viewOffset); + triProjected.C = Vector.add(triProjected.C,viewOffset); + triProjected.A.x*=0.5f*SigRenderer.SCREEN_WIDTH; + triProjected.A.y*=0.5f*SigRenderer.SCREEN_HEIGHT; + triProjected.B.x*=0.5f*SigRenderer.SCREEN_WIDTH; + triProjected.B.y*=0.5f*SigRenderer.SCREEN_HEIGHT; + triProjected.C.x*=0.5f*SigRenderer.SCREEN_WIDTH; + triProjected.C.y*=0.5f*SigRenderer.SCREEN_HEIGHT; - accumulatedTris.add(triProjected); + accumulatedTris.add(triProjected); + } + } else { + t.nextRenderTime=System.currentTimeMillis()+200; } } } @@ -258,6 +271,7 @@ public class Panel extends JPanel implements Runnable { } for (Triangle tt : triList) { if (tt.tex!=null) { + tt.unmodifiedTri.nextRenderTime=System.currentTimeMillis()+200; 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, @@ -274,6 +288,14 @@ public class Panel extends JPanel implements Runnable { totalTime+=System.nanoTime()-startTime2; } } + for (int x=0;x blockGrid = new HashMap<>(); + public static HashMap renderMap = new HashMap<>(); public static List pixels; @@ -45,16 +46,17 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene public static float fAspectRatio = (float)SCREEN_HEIGHT/SCREEN_WIDTH; public static Matrix matProj = Matrix.MakeProjection(fFov,fAspectRatio,fNear,fFar); - public static Vector vCamera = new Vector(0.5f,2f,-15f); + public static Vector vCamera = new Vector(63.5f,20f,63.5f); public static Vector vLookDir = new Vector(0,0,1); public static float yaw = (float)(-Math.PI/8); - public static float pitch = (float)(Math.PI/8); + public static float pitch = (float)(-Math.PI/6); public static float roll = 0; final float MOVESPEED = 0.2f; final float TURNSPEED = 0.05f; public static float[] depthBuffer; + public static Triangle[] depthBuffer_tri; public static HashMap blockTextures = new HashMap(); @@ -121,8 +123,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene SigRenderer(JFrame f) { //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false)); Random r = new Random(438107); - for (int x=0;x<64;x++) { - for (int z=0;z<64;z++) { + for (int x=0;x<128;x++) { + for (int z=0;z<128;z++) { if (Math.random()<=0.5) { addBlock(new Vector(x,0,z),BlockType.DIRT); } else { diff --git a/src/sig/Triangle.java b/src/sig/Triangle.java index a3ad649..fdd8de5 100644 --- a/src/sig/Triangle.java +++ b/src/sig/Triangle.java @@ -7,6 +7,8 @@ public class Triangle { int col = Color.WHITE.getRGB(); public Block b = null; public Texture tex = null; + public long nextRenderTime = -1l; + public Triangle unmodifiedTri; public Triangle() { this(new Vector(),new Vector(),new Vector()); } @@ -28,6 +30,7 @@ public class Triangle { targetTriangle.col=this.col; targetTriangle.tex=this.tex; targetTriangle.b=this.b; + targetTriangle.unmodifiedTri=this.unmodifiedTri; } @Override protected Object clone(){ @@ -35,6 +38,7 @@ public class Triangle { t.col = col; t.tex=tex; t.b=b; + t.unmodifiedTri=unmodifiedTri; return t; } @Override @@ -100,6 +104,7 @@ public class Triangle { out_tri[0].col = in.col; out_tri[0].tex = in.tex; out_tri[0].b=in.b; + out_tri[0].unmodifiedTri=in.unmodifiedTri; 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); @@ -117,6 +122,7 @@ public class Triangle { out_tri[0].col=out_tri[1].col=in.col; out_tri[0].tex=out_tri[1].tex=in.tex; out_tri[0].b=out_tri[1].b=in.b; + out_tri[0].unmodifiedTri=out_tri[1].unmodifiedTri=in.unmodifiedTri; 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/DrawUtils.java b/src/sig/utils/DrawUtils.java index f644414..5449a3e 100644 --- a/src/sig/utils/DrawUtils.java +++ b/src/sig/utils/DrawUtils.java @@ -86,6 +86,7 @@ public class DrawUtils { } 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; + SigRenderer.depthBuffer_tri[i*SigRenderer.SCREEN_WIDTH+j] = ref.unmodifiedTri; } t+=tstep; } @@ -141,6 +142,7 @@ public class DrawUtils { } 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; + SigRenderer.depthBuffer_tri[i*SigRenderer.SCREEN_WIDTH+j] = ref.unmodifiedTri; } t+=tstep; }