Do not render faces that are on the "insides" of a structure.

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
origin
sigonasr2, Sig, Sigo 3 years ago
parent c39412e144
commit fb007ad472
  1. 39
      src/sig/Block.java
  2. 10
      src/sig/FaceList.java
  3. 23
      src/sig/Panel.java
  4. 39
      src/sig/SigRenderer.java
  5. 15
      src/sig/Triangle.java

@ -1,10 +1,47 @@
package sig; package sig;
import java.util.ArrayList;
import java.util.List;
public class Block { public class Block {
Vector pos; Vector pos;
Mesh block; Mesh block;
FaceList neighbors;
Block(Vector pos,Mesh block) { Block(Vector pos,Mesh block) {
this.neighbors=new FaceList();
this.pos=pos; this.pos=pos;
this.block=block; List<Triangle> newTris = new ArrayList<>();
for (Triangle t : block.triangles) {
Triangle newT = (Triangle)t.clone();
newT.b=this;
newTris.add(newT);
}
this.block=new Mesh(newTris);
}
public void updateFaces() {
if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y+1)+"_"+pos.z)) {
neighbors.UP=true;
SigRenderer.blockGrid.get(pos.x+"_"+(pos.y+1)+"_"+pos.z).neighbors.DOWN=true;
}
if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y-1)+"_"+pos.z)) {
neighbors.DOWN=true;
SigRenderer.blockGrid.get(pos.x+"_"+(pos.y-1)+"_"+pos.z).neighbors.UP=true;
}
if (SigRenderer.blockGrid.containsKey((pos.x-1)+"_"+(pos.y)+"_"+pos.z)) {
neighbors.LEFT=true;
SigRenderer.blockGrid.get((pos.x-1)+"_"+(pos.y)+"_"+pos.z).neighbors.RIGHT=true;
}
if (SigRenderer.blockGrid.containsKey((pos.x+1)+"_"+(pos.y)+"_"+pos.z)) {
neighbors.RIGHT=true;
SigRenderer.blockGrid.get((pos.x+1)+"_"+(pos.y)+"_"+pos.z).neighbors.LEFT=true;
}
if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y)+"_"+(pos.z+1))) {
neighbors.FORWARD=true;
SigRenderer.blockGrid.get(pos.x+"_"+(pos.y)+"_"+(pos.z+1)).neighbors.BACKWARD=true;
}
if (SigRenderer.blockGrid.containsKey(pos.x+"_"+(pos.y)+"_"+(pos.z-1))) {
neighbors.BACKWARD=true;
SigRenderer.blockGrid.get(pos.x+"_"+(pos.y)+"_"+(pos.z-1)).neighbors.FORWARD=true;
}
} }
} }

@ -0,0 +1,10 @@
package sig;
public class FaceList {
boolean UP=false; //+Y
boolean DOWN=false;
boolean FORWARD=false; //+Z
boolean RIGHT=false; //+X
boolean LEFT=false;
boolean BACKWARD=false;
}

@ -103,11 +103,10 @@ public class Panel extends JPanel implements Runnable {
Matrix matCamera = Matrix.PointAt(SigRenderer.vCamera, vTarget, vUp); Matrix matCamera = Matrix.PointAt(SigRenderer.vCamera, vTarget, vUp);
Matrix matView = Matrix.QuickInverse(matCamera); Matrix matView = Matrix.QuickInverse(matCamera);
for (Block b : SigRenderer.blocks) { for (Triangle t : SigRenderer.triRender) {
for (Triangle t : b.block.triangles) {
Triangle triProjected = new Triangle(),triTransformed=new Triangle(),triViewed=new Triangle(); Triangle triProjected = new Triangle(),triTransformed=new Triangle(),triViewed=new Triangle();
matWorld = Matrix.MakeTranslation(b.pos.x,b.pos.y,b.pos.z); matWorld = Matrix.MakeTranslation(t.b.pos.x,t.b.pos.y,t.b.pos.z);
triTransformed.A = Matrix.MultiplyVector(matWorld,t.A); triTransformed.A = Matrix.MultiplyVector(matWorld,t.A);
triTransformed.B = Matrix.MultiplyVector(matWorld,t.B); triTransformed.B = Matrix.MultiplyVector(matWorld,t.B);
@ -116,6 +115,7 @@ public class Panel extends JPanel implements Runnable {
triTransformed.U = t.U; triTransformed.U = t.U;
triTransformed.V = t.V; triTransformed.V = t.V;
triTransformed.tex = t.tex; triTransformed.tex = t.tex;
triTransformed.b=t.b;
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);
@ -137,18 +137,15 @@ public class Panel extends JPanel implements Runnable {
float dp = Math.max(0.1f,Math.min(1,(1f/((cameraRay2.x-center.x)*(cameraRay2.x-center.x)+ 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.y-center.y)*(cameraRay2.y-center.y)+
(cameraRay2.z-center.z)*(cameraRay2.z-center.z))*4)));*/ (cameraRay2.z-center.z)*(cameraRay2.z-center.z))*4)));*/
float dp = Math.max(0.1f,Math.min(1,(1f/((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)+
(b.pos.y-SigRenderer.vCamera.y)*(b.pos.y-SigRenderer.vCamera.y)+ (triTransformed.b.pos.y-SigRenderer.vCamera.y)*(triTransformed.b.pos.y-SigRenderer.vCamera.y)+
(b.pos.z-SigRenderer.vCamera.z)*(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.A = Matrix.MultiplyVector(matView,triTransformed.A);
triViewed.B = Matrix.MultiplyVector(matView,triTransformed.B); triViewed.B = Matrix.MultiplyVector(matView,triTransformed.B);
triViewed.C = Matrix.MultiplyVector(matView,triTransformed.C); triViewed.C = Matrix.MultiplyVector(matView,triTransformed.C);
triTransformed.copyExtraDataTo(triViewed);
triViewed.setColor(new Color(dp,dp,dp)); triViewed.setColor(new Color(dp,dp,dp));
triViewed.T = triTransformed.T;
triViewed.U = triTransformed.U;
triViewed.V = triTransformed.V;
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()};
@ -166,6 +163,7 @@ public class Panel extends JPanel implements Runnable {
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();
triProjected.b=clipped[i].b;
triProjected.T.u = triProjected.T.u/triProjected.A.w; triProjected.T.u = triProjected.T.u/triProjected.A.w;
triProjected.U.u = triProjected.U.u/triProjected.B.w; triProjected.U.u = triProjected.U.u/triProjected.B.w;
@ -204,7 +202,6 @@ public class Panel extends JPanel implements Runnable {
} }
} }
} }
}
/*Collections.sort(accumulatedTris, new Comparator<Triangle>() { /*Collections.sort(accumulatedTris, new Comparator<Triangle>() {
@Override @Override
@ -214,8 +211,8 @@ public class Panel extends JPanel implements Runnable {
return (z1<z2)?1:(z1==z2)?0:-1; return (z1<z2)?1:(z1==z2)?0:-1;
} }
});*/ });*/
for (Triangle t : accumulatedTris) { for (Triangle t : accumulatedTris) {
Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()};
List<Triangle> triList = new ArrayList<>(); List<Triangle> triList = new ArrayList<>();
triList.add(t); triList.add(t);
@ -258,11 +255,11 @@ public class Panel extends JPanel implements Runnable {
public void repaint() { public void repaint() {
super.repaint(); super.repaint();
startTime = System.nanoTime();
} }
@Override @Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
startTime = System.nanoTime();
super.paintComponent(g); super.paintComponent(g);
// perform draws on pixels // perform draws on pixels
render(); render();

@ -12,7 +12,9 @@ import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.BorderLayout; import java.awt.BorderLayout;
@ -20,7 +22,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static boolean WIREFRAME = false; public static boolean WIREFRAME = false;
public static List<Block> blocks = new ArrayList<Block>(); public static List<Triangle> triRender = new ArrayList<>();
public static int SCREEN_WIDTH=1280; public static int SCREEN_WIDTH=1280;
public static int SCREEN_HEIGHT=720; public static int SCREEN_HEIGHT=720;
public final static long TIMEPERTICK = 16666667l; public final static long TIMEPERTICK = 16666667l;
@ -28,6 +30,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static float DRAWLOOPTIME=0; public static float DRAWLOOPTIME=0;
public static final float RESOLUTION=1; public static final float RESOLUTION=1;
public static float rot = (float)Math.PI/4; //In radians. public static float rot = (float)Math.PI/4; //In radians.
public static Map<String,Block> blockGrid = new HashMap<>();
public static List<Pixel> pixels; public static List<Pixel> pixels;
@ -37,7 +40,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static float fAspectRatio = (float)SCREEN_HEIGHT/SCREEN_WIDTH; public static float fAspectRatio = (float)SCREEN_HEIGHT/SCREEN_WIDTH;
public static Matrix matProj = Matrix.MakeProjection(fFov,fAspectRatio,fNear,fFar); public static Matrix matProj = Matrix.MakeProjection(fFov,fAspectRatio,fNear,fFar);
public static Vector vCamera = new Vector(); public static Vector vCamera = new Vector(0.5f,2f,0.5f);
public static Vector vLookDir = new Vector(0,0,1); public static Vector vLookDir = new Vector(0,0,1);
public static float yaw = 0; public static float yaw = 0;
public static float pitch = 0; public static float pitch = 0;
@ -50,6 +53,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static float[] depthBuffer; public static float[] depthBuffer;
public static Mesh DIRT_CUBE=new Mesh("cube.obj","dirt.png");
boolean upHeld=false,downHeld=false,leftHeld=false,rightHeld=false, boolean upHeld=false,downHeld=false,leftHeld=false,rightHeld=false,
aHeld=false,sHeld=false,dHeld=false,wHeld=false; aHeld=false,sHeld=false,dHeld=false,wHeld=false;
@ -83,15 +88,31 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
} }
} }
public static void addBlock(Vector pos,Mesh type) {
Block b = new Block(pos,type);
blockGrid.put(pos.x+"_"+pos.y+"_"+pos.z,b);
b.updateFaces();
updateRenderGrid();
}
public static void updateRenderGrid() {
triRender.clear();
for (String key : blockGrid.keySet()) {
Block b = blockGrid.get(key);
if (!b.neighbors.UP) {triRender.add(b.block.triangles.get(8));triRender.add(b.block.triangles.get(9));}
if (!b.neighbors.DOWN) {triRender.add(b.block.triangles.get(10));triRender.add(b.block.triangles.get(11));}
if (!b.neighbors.LEFT) {triRender.add(b.block.triangles.get(6));triRender.add(b.block.triangles.get(7));}
if (!b.neighbors.RIGHT) {triRender.add(b.block.triangles.get(2));triRender.add(b.block.triangles.get(3));}
if (!b.neighbors.FORWARD) {triRender.add(b.block.triangles.get(4));triRender.add(b.block.triangles.get(5));}
if (!b.neighbors.BACKWARD) {triRender.add(b.block.triangles.get(0));triRender.add(b.block.triangles.get(1));}
}
}
SigRenderer(JFrame f) { SigRenderer(JFrame f) {
//cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false)); //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false));
Mesh dirtCube = new Mesh("cube.obj","dirt.png"); for (int x=0;x<16;x++) {
for (int x=0;x<32;x++) { for (int z=0;z<16;z++) {
for (int y=0;y<32;y++) { addBlock(new Vector(x,0,z),DIRT_CUBE);
blocks.add(new Block(
new Vector(x,0,y),
dirtCube
));
} }
} }

@ -5,6 +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;
Block b = null;
public Texture tex = null; public Texture tex = null;
public Triangle() { public Triangle() {
this(new Vector(),new Vector(),new Vector()); this(new Vector(),new Vector(),new Vector());
@ -20,10 +21,20 @@ public class Triangle {
this.U=U; this.U=U;
this.V=V; this.V=V;
} }
public void copyExtraDataTo(Triangle targetTriangle) {
targetTriangle.T=this.T;
targetTriangle.U=this.U;
targetTriangle.V=this.V;
targetTriangle.col=this.col;
targetTriangle.tex=this.tex;
targetTriangle.b=this.b;
}
@Override @Override
protected Object clone(){ protected Object clone(){
Triangle t = new Triangle((Vector)this.A.clone(),(Vector)this.B.clone(),(Vector)this.C.clone()); Triangle t = new Triangle((Vector)this.A.clone(),(Vector)this.B.clone(),(Vector)this.C.clone(),(Vector2)this.T.clone(),(Vector2)this.U.clone(),(Vector2)this.V.clone());
t.col = col; t.col = col;
t.tex=tex;
t.b=b;
return t; return t;
} }
@Override @Override
@ -88,6 +99,7 @@ public class Triangle {
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].tex = in.tex; out_tri[0].tex = in.tex;
out_tri[0].b=in.b;
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);
@ -104,6 +116,7 @@ public class Triangle {
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].tex=out_tri[1].tex=in.tex; out_tri[0].tex=out_tri[1].tex=in.tex;
out_tri[0].b=out_tri[1].b=in.b;
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];

Loading…
Cancel
Save