Block buildling and some camera clipping fixes.

origin
Joshua Sigona 3 years ago
parent 234520ced4
commit 78ffa0d78d
  1. 186
      src/sig/Panel.java
  2. 20
      src/sig/SigRenderer.java

@ -109,7 +109,110 @@ public class Panel extends JPanel implements Runnable {
Matrix matCamera = Matrix.PointAt(SigRenderer.vCamera, vTarget, vUp);
Matrix matView = Matrix.QuickInverse(matCamera);
for (Triangle t : SigRenderer.triRender) {
for (String key : SigRenderer.blockGrid.keySet()) {
Block b = SigRenderer.blockGrid.get(key);
if (!b.neighbors.UP) {
prepareTriForRender(matWorld, matView, b.block.triangles.get(8), accumulatedTris);
prepareTriForRender(matWorld, matView, b.block.triangles.get(9), accumulatedTris);
}
if (!b.neighbors.DOWN) {
prepareTriForRender(matWorld, matView, b.block.triangles.get(10), accumulatedTris);
prepareTriForRender(matWorld, matView, b.block.triangles.get(11), accumulatedTris);
}
if (!b.neighbors.LEFT) {
prepareTriForRender(matWorld, matView, b.block.triangles.get(6), accumulatedTris);
prepareTriForRender(matWorld, matView, b.block.triangles.get(7), accumulatedTris);
}
if (!b.neighbors.RIGHT) {
prepareTriForRender(matWorld, matView, b.block.triangles.get(2), accumulatedTris);
prepareTriForRender(matWorld, matView, b.block.triangles.get(3), accumulatedTris);
}
if (!b.neighbors.FORWARD) {
prepareTriForRender(matWorld, matView, b.block.triangles.get(4), accumulatedTris);
prepareTriForRender(matWorld, matView, b.block.triangles.get(5), accumulatedTris);
}
if (!b.neighbors.BACKWARD) {
prepareTriForRender(matWorld, matView, b.block.triangles.get(0), accumulatedTris);
prepareTriForRender(matWorld, matView, b.block.triangles.get(1), accumulatedTris);
}
}
if (SigRenderer.PROFILING) {
System.out.println((System.currentTimeMillis()-profileStartTime)+"ms");profileStartTime=System.currentTimeMillis();
}
/*Collections.sort(accumulatedTris, new Comparator<Triangle>() {
@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<z2)?1:(z1==z2)?0:-1;
}
});*/
SigRenderer.tempAnswer=null;
long totalTime=0;
long startTime2=0;
for (Triangle t : accumulatedTris) {
Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()};
List<Triangle> triList = new ArrayList<>();
triList.add(t);
int newTriangles=1;
for (int pl=0;pl<4;pl++) {
int trisToAdd=0;
while (newTriangles>0) {
clipped = new Triangle[]{new Triangle(),new Triangle()};
Triangle test = triList.remove(0);
newTriangles--;
switch (pl) {
case 0:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(0,0,0),new Vector(0,1,0),test,clipped);}break;
case 1:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(0,getHeight()-1f,0),new Vector(0,-1,0),test,clipped);}break;
case 2:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(0,0,0),new Vector(1,0,0),test,clipped);}break;
case 3:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(getWidth()-1f,0,0),new Vector(-1,0,0),test,clipped);}break;
}
for (int w=0;w<trisToAdd;w++) {
triList.add(clipped[w]);
}
}
newTriangles=triList.size();
}
if (SigRenderer.PROFILING) {
startTime2 = System.nanoTime();
}
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,
(int)tt.C.x,(int)tt.C.y,tt.V.u,tt.V.v,tt.V.w,
tt.tex,(tt.col&0xFF0000)>>16,tt);
} 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.getRGB());
}
}
if (SigRenderer.PROFILING) {
totalTime+=System.nanoTime()-startTime2;
}
}
for (int x=0;x<SigRenderer.SCREEN_WIDTH;x++) {
for (int y=0;y<SigRenderer.SCREEN_HEIGHT;y++) {
Triangle t = SigRenderer.depthBuffer_tri[x+y*SigRenderer.SCREEN_WIDTH];
if (t!=null) {
t.nextRenderTime=-1;
}
}
}
SigRenderer.request=null;
SigRenderer.answer=SigRenderer.tempAnswer;
if (SigRenderer.PROFILING) {
System.out.println((totalTime/1000000f)+"ms/"+(System.currentTimeMillis()-profileStartTime)+"ms");
}
}
private void prepareTriForRender(Matrix matWorld, Matrix matView, Triangle t, List<Triangle> accumulatedTris) {
if (t.nextRenderTime<=System.currentTimeMillis()) {
Triangle triProjected = new Triangle(),triTransformed=new Triangle(),triViewed=new Triangle();
@ -134,13 +237,15 @@ public class Panel extends JPanel implements Runnable {
normal = Vector.crossProduct(line1,line2);
normal = Vector.normalize(normal);
Vector cameraRay = Vector.subtract(triTransformed.A,SigRenderer.vCamera);
Vector center = Vector.divide(Vector.add(triTransformed.A,Vector.add(triTransformed.B,triTransformed.C)),3);
Vector cameraRay = Vector.subtract(center,SigRenderer.vCamera);
float distSquared = ((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));
if (Vector.dotProduct(normal,cameraRay)<0&&Vector.dotProduct(cameraRay,SigRenderer.vLookDir)>0.1f&&distSquared<4096) {
if (Vector.dotProduct(normal,cameraRay)<0&&Vector.dotProduct(cameraRay,SigRenderer.vLookDir)>-0.2f&&distSquared<4096) {
Vector lightDir = Vector.multiply(SigRenderer.vLookDir, -1);
lightDir = Vector.normalize(lightDir);
@ -227,81 +332,6 @@ public class Panel extends JPanel implements Runnable {
}
}
}
if (SigRenderer.PROFILING) {
System.out.println((System.currentTimeMillis()-profileStartTime)+"ms");profileStartTime=System.currentTimeMillis();
}
/*Collections.sort(accumulatedTris, new Comparator<Triangle>() {
@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<z2)?1:(z1==z2)?0:-1;
}
});*/
SigRenderer.tempAnswer=null;
long totalTime=0;
long startTime2=0;
for (Triangle t : accumulatedTris) {
Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()};
List<Triangle> triList = new ArrayList<>();
triList.add(t);
int newTriangles=1;
for (int pl=0;pl<4;pl++) {
int trisToAdd=0;
while (newTriangles>0) {
clipped = new Triangle[]{new Triangle(),new Triangle()};
Triangle test = triList.remove(0);
newTriangles--;
switch (pl) {
case 0:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(0,0,0),new Vector(0,1,0),test,clipped);}break;
case 1:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(0,getHeight()-1f,0),new Vector(0,-1,0),test,clipped);}break;
case 2:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(0,0,0),new Vector(1,0,0),test,clipped);}break;
case 3:{trisToAdd = Triangle.ClipAgainstPlane(new Vector(getWidth()-1f,0,0),new Vector(-1,0,0),test,clipped);}break;
}
for (int w=0;w<trisToAdd;w++) {
triList.add(clipped[w]);
}
}
newTriangles=triList.size();
}
if (SigRenderer.PROFILING) {
startTime2 = System.nanoTime();
}
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,
(int)tt.C.x,(int)tt.C.y,tt.V.u,tt.V.v,tt.V.w,
tt.tex,(tt.col&0xFF0000)>>16,tt);
} 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.getRGB());
}
}
if (SigRenderer.PROFILING) {
totalTime+=System.nanoTime()-startTime2;
}
}
for (int x=0;x<SigRenderer.SCREEN_WIDTH;x++) {
for (int y=0;y<SigRenderer.SCREEN_HEIGHT;y++) {
Triangle t = SigRenderer.depthBuffer_tri[x+y*SigRenderer.SCREEN_WIDTH];
if (t!=null) {
t.nextRenderTime=-1;
}
}
}
SigRenderer.request=null;
SigRenderer.answer=SigRenderer.tempAnswer;
if (SigRenderer.PROFILING) {
System.out.println((totalTime/1000000f)+"ms/"+(System.currentTimeMillis()-profileStartTime)+"ms");
}
}
public void repaint() {
super.repaint();

@ -96,7 +96,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
yaw+=TURNSPEED;
}
if (answer!=null) {
System.out.println(answer);
addBlock(Vector.add(answer.pos,new Vector(0,1,0)),BlockType.PLANKS);
answer=null;
}
}
@ -107,24 +107,11 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
b.updateFaces();
}
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) {
//cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false));
Random r = new Random(438107);
for (int x=0;x<128;x++) {
for (int z=0;z<128;z++) {
for (int x=0;x<64;x++) {
for (int z=0;z<64;z++) {
if (Math.random()<=0.5) {
addBlock(new Vector(x,0,z),BlockType.DIRT);
} else {
@ -132,7 +119,6 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
}
}
}
updateRenderGrid();
Panel p = new Panel();

Loading…
Cancel
Save