From a99f5a81e314616fb0fadd438d2cb4018925e698 Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Fri, 5 Nov 2021 22:30:00 +0900 Subject: [PATCH] Optimized texture pixel getting. --- cube.obj | 2 +- pom.xml | 6 ++++++ src/sig/Panel.java | 16 +++++++--------- src/sig/SigRenderer.java | 21 ++++++++++++-------- src/sig/Texture.java | 37 +++++++++++++++++++++++++++--------- src/sig/Triangle.java | 6 +++--- src/sig/utils/DrawUtils.java | 12 ++++++------ 7 files changed, 64 insertions(+), 36 deletions(-) diff --git a/cube.obj b/cube.obj index c59f3ca..12dbf6e 100644 --- a/cube.obj +++ b/cube.obj @@ -13,7 +13,7 @@ vt 1 0 #3 vt 1 1 #4 f 1/2 3/1 7/3 -f 1/2 7/3 8/4 +f 1/2 7/3 5/4 f 5/2 7/1 8/3 f 5/2 8/3 6/4 f 6/2 8/1 4/3 diff --git a/pom.xml b/pom.xml index 69fba72..4912a45 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,12 @@ + + + org.jogamp.jogl + jogl-all-main + 2.3.2 + diff --git a/src/sig/Panel.java b/src/sig/Panel.java index 5903ceb..65cc6da 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -9,8 +9,6 @@ import java.awt.Color; import java.awt.Image; import java.awt.image.MemoryImageSource; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.awt.image.ColorModel; import java.awt.GraphicsEnvironment; @@ -137,7 +135,7 @@ public class Panel extends JPanel implements Runnable { 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))); + (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)); } @@ -154,7 +152,7 @@ public class Panel extends JPanel implements Runnable { triViewed.B = Matrix.MultiplyVector(matView,triTransformed.B); triViewed.C = Matrix.MultiplyVector(matView,triTransformed.C); triTransformed.copyExtraDataTo(triViewed); - triViewed.setColor(new Color(dp,0,0)); + triViewed.setColor((0)+(0<<8)+((int)(dp*255)<<16)); int clippedTriangles = 0; Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; @@ -212,14 +210,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 (z1>16); } 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()); } if (SigRenderer.WIREFRAME) { - DrawUtils.DrawTriangle(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,Color.WHITE); + DrawUtils.DrawTriangle(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,Color.WHITE.getRGB()); } } } @@ -296,4 +294,4 @@ public class Panel extends JPanel implements Runnable { try {Thread.sleep(5);} catch (InterruptedException e) {} } } -} +} \ No newline at end of file diff --git a/src/sig/SigRenderer.java b/src/sig/SigRenderer.java index e00afc5..239fb5d 100644 --- a/src/sig/SigRenderer.java +++ b/src/sig/SigRenderer.java @@ -15,6 +15,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.awt.Toolkit; import java.awt.BorderLayout; @@ -40,14 +41,14 @@ 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,0.5f); + public static Vector vCamera = new Vector(0.5f,2f,-15f); public static Vector vLookDir = new Vector(0,0,1); - public static float yaw = 0; - public static float pitch = 0; + public static float yaw = (float)(-Math.PI/8); + public static float pitch = (float)(Math.PI/8); public static float roll = 0; - final float MOVESPEED = 0.03f; - final float TURNSPEED = 0.03f; + final float MOVESPEED = 0.2f; + final float TURNSPEED = 0.2f; public static Texture dirtTex; @@ -58,6 +59,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene boolean upHeld=false,downHeld=false,leftHeld=false,rightHeld=false, aHeld=false,sHeld=false,dHeld=false,wHeld=false; + public static MouseEvent storedMouseEvent; + public void runGameLoop() { if (upHeld) { pitch+=MOVESPEED; @@ -110,9 +113,10 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene SigRenderer(JFrame f) { //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false)); - for (int x=0;x<16;x++) { - for (int z=0;z<16;z++) { - for (int y=0;y<16;y++) { + Random r = new Random(438107); + for (int x=0;x<32;x++) { + for (int z=0;z<32;z++) { + for (int y=0;y=tex.getWidth()||sy<0||sy>=tex.getHeight()) { - return new Color(0,0,0,0); + public int getColor(float u,float v,float mult) { + int sx = (int)(u*width-1f); + int sy = (int)(v*height-1f); + if (sx<0||sx>=width||sy<0||sy>=height) { + return 0; } else { - Color newCol = new Color(tex.getRGB(sx,sy)); - return new Color((newCol.getRed()/255f)*mult,(newCol.getGreen()/255f)*mult,(newCol.getBlue()/255f)*mult); + int indice = (int)(u*width-1)+(int)(v*height-1)*width; + //return tex[indice]; + return (int)((tex[indice]&0xFF)*mult) + ((int)(((tex[indice]&0xFF00)>>8)*mult)<<8) + ((int)(((tex[indice]&0xFF0000)>>16)*mult)<<16); + /*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 f9cede5..71a4e5e 100644 --- a/src/sig/Triangle.java +++ b/src/sig/Triangle.java @@ -4,7 +4,7 @@ import java.awt.Color; public class Triangle { Vector A,B,C; Vector2 T,U,V; - Color col = Color.WHITE; + int col = Color.WHITE.getRGB(); Block b = null; public Texture tex = null; public Triangle() { @@ -42,10 +42,10 @@ public class Triangle { return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", T=" + T + ", U=" + U + ", V=" + V + ", col=" + col + "]"; } - public Color getColor() { + public int getColor() { return col; } - public void setColor(Color col) { + public void setColor(int col) { this.col=col; } diff --git a/src/sig/utils/DrawUtils.java b/src/sig/utils/DrawUtils.java index d44f447..c8108d6 100644 --- a/src/sig/utils/DrawUtils.java +++ b/src/sig/utils/DrawUtils.java @@ -8,7 +8,7 @@ import sig.SigRenderer; import sig.Texture; public class DrawUtils { - static void drawLine(int[] canvas,int sx,int ex,int ny,Color col) { + static void drawLine(int[] canvas,int sx,int ex,int ny,int col) { for (int i=sx;i<=ex;i++) { Draw(canvas,i,ny,col); } @@ -140,7 +140,7 @@ public class DrawUtils { } } } - public static void FillTriangle(int[] canvas,int x1, int y1, int x2, int y2, int x3, int y3, Color col) + public static void FillTriangle(int[] canvas,int x1, int y1, int x2, int y2, int x3, int y3, int col) { int t1x=0, t2x=0, y=0, minx=0, maxx=0, t1xp=0, t2xp=0; boolean changed1 = false; @@ -274,12 +274,12 @@ public class DrawUtils { if (y>y3) return; } } - public static void DrawTriangle(int[]canvas,int x1,int y1,int x2,int y2,int x3,int y3,Color col) { + public static void DrawTriangle(int[]canvas,int x1,int y1,int x2,int y2,int x3,int y3,int col) { DrawLine(canvas,x1,y1,x2,y2,col); DrawLine(canvas,x2,y2,x3,y3,col); DrawLine(canvas,x3,y3,x1,y1,col); } - public static void DrawLine(int[] canvas,int x1,int y1,int x2,int y2,Color col) { + public static void DrawLine(int[] canvas,int x1,int y1,int x2,int y2,int col) { int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i; dx=x2-x1;dy=y2-y1; dx1=Math.abs(dx);dy1=Math.abs(dy); @@ -328,10 +328,10 @@ public class DrawUtils { } } } - public static void Draw(int[] canvas,int x,int y,Color col) { + public static void Draw(int[] canvas,int x,int y,int col) { if (x>=0&&y>=0&&x