Add rotation of non-cube blocks.

origin
Joshua Sigona 3 years ago
parent 81c8ece1c3
commit decc6535b7
  1. 59
      src/sig/Panel.java
  2. 36
      src/sig/SigRenderer.java

@ -360,39 +360,50 @@ public class Panel extends JPanel implements Runnable {
private void prepareTriForRender(Matrix matWorld, Matrix matView, Triangle t, Collection<Triangle> accumulatedTris, boolean buffer) { private void prepareTriForRender(Matrix matWorld, Matrix matView, Triangle t, Collection<Triangle> accumulatedTris, boolean buffer) {
if (!buffer||(buffer&&t.nextRenderTime<=System.currentTimeMillis())) { if (!buffer||(buffer&&t.nextRenderTime<=System.currentTimeMillis())) {
Triangle triProjected = new Triangle(),triTransformed=new Triangle(),triViewed=new Triangle(); Triangle triProjected = new Triangle(),triPreTransform=new Triangle(),triTransformed=new Triangle(),triViewed=new Triangle(),triRotation = new Triangle();
matWorld = Matrix.MakeTranslation(-0.5f,0,-0.5f);
triPreTransform.A = Matrix.MultiplyVector(matWorld,t.A);
triPreTransform.B = Matrix.MultiplyVector(matWorld,t.B);
triPreTransform.C = Matrix.MultiplyVector(matWorld,t.C);
t.copyExtraDataTo(triPreTransform);
triPreTransform.unmodifiedTri=t;
if (t.b!=null) {
matWorld = Matrix.MakeRotationY((float)(t.b.getFacingDirection().ordinal()*(Math.PI/2)));
}
triTransformed.A = Matrix.MultiplyVector(matWorld,triPreTransform.A);
triTransformed.B = Matrix.MultiplyVector(matWorld,triPreTransform.B);
triTransformed.C = Matrix.MultiplyVector(matWorld,triPreTransform.C);
triPreTransform.copyExtraDataTo(triTransformed);
if (t.b!=null) { if (t.b!=null) {
matWorld = Matrix.MakeTranslation(t.b.pos.x,t.b.pos.y,t.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); triRotation.A = Matrix.MultiplyVector(matWorld,triTransformed.A);
triTransformed.B = Matrix.MultiplyVector(matWorld,t.B); triRotation.B = Matrix.MultiplyVector(matWorld,triTransformed.B);
triTransformed.C = Matrix.MultiplyVector(matWorld,t.C); triRotation.C = Matrix.MultiplyVector(matWorld,triTransformed.C);
triTransformed.T = t.T; triTransformed.copyExtraDataTo(triRotation);
triTransformed.U = t.U;
triTransformed.V = t.V;
triTransformed.tex = t.tex;
triTransformed.b=t.b;
triTransformed.unmodifiedTri=t;
triTransformed.dir=t.dir;
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(triRotation.B,triRotation.A);
line2 = Vector.subtract(triTransformed.C,triTransformed.A); line2 = Vector.subtract(triRotation.C,triRotation.A);
normal = Vector.crossProduct(line1,line2); normal = Vector.crossProduct(line1,line2);
normal = Vector.normalize(normal); normal = Vector.normalize(normal);
Vector center = Vector.divide(Vector.add(triTransformed.A,Vector.add(triTransformed.B,triTransformed.C)),3); Vector center = Vector.divide(Vector.add(triRotation.A,Vector.add(triRotation.B,triRotation.C)),3);
Vector newCamera = Vector.add(SigRenderer.vCamera,SigRenderer.vCameraOffset); Vector newCamera = Vector.add(SigRenderer.vCamera,SigRenderer.vCameraOffset);
Vector cameraRay = Vector.subtract(center,newCamera); Vector cameraRay = Vector.subtract(center,newCamera);
float distSquared = ((triTransformed.b.pos.x-newCamera.x)*(triTransformed.b.pos.x-newCamera.x)+ float distSquared = ((triRotation.b.pos.x-newCamera.x)*(triRotation.b.pos.x-newCamera.x)+
(triTransformed.b.pos.y-newCamera.y)*(triTransformed.b.pos.y-newCamera.y)+ (triRotation.b.pos.y-newCamera.y)*(triRotation.b.pos.y-newCamera.y)+
(triTransformed.b.pos.z-newCamera.z)*(triTransformed.b.pos.z-newCamera.z)); (triRotation.b.pos.z-newCamera.z)*(triRotation.b.pos.z-newCamera.z));
if (Vector.dotProduct(normal,cameraRay)<0&&Vector.dotProduct(cameraRay,SigRenderer.vLookDir)>-0.2f&&distSquared<4096) { if (Vector.dotProduct(normal,cameraRay)<0&&Vector.dotProduct(cameraRay,SigRenderer.vLookDir)>-0.2f&&distSquared<4096) {
Vector lightDir = Vector.multiply(SigRenderer.vLookDir, -1); Vector lightDir = Vector.multiply(SigRenderer.vLookDir, -1);
@ -401,9 +412,9 @@ public class Panel extends JPanel implements Runnable {
//System.out.println(-Vector.dotProduct(normal,Vector.normalize(cameraRay))); //System.out.println(-Vector.dotProduct(normal,Vector.normalize(cameraRay)));
float dp = 0.1f; float dp = 0.1f;
if (t.b!=null) { if (t.b!=null) {
dp = Math.max(0.1f,Math.min(1,(1f/((triTransformed.b.pos.x-newCamera.x)*(triTransformed.b.pos.x-newCamera.x)+ dp = Math.max(0.1f,Math.min(1,(1f/((triRotation.b.pos.x-newCamera.x)*(triRotation.b.pos.x-newCamera.x)+
(triTransformed.b.pos.y-newCamera.y)*(triTransformed.b.pos.y-newCamera.y)+ (triRotation.b.pos.y-newCamera.y)*(triRotation.b.pos.y-newCamera.y)+
(triTransformed.b.pos.z-newCamera.z)*(triTransformed.b.pos.z-newCamera.z))*64)))*0.5f+Math.max(0.1f,Math.min(1,1-Vector.dotProduct(normal,SigRenderer.vLookDir)))*0.5f; (triRotation.b.pos.z-newCamera.z)*(triRotation.b.pos.z-newCamera.z))*64)))*0.5f+Math.max(0.1f,Math.min(1,1-Vector.dotProduct(normal,SigRenderer.vLookDir)))*0.5f;
} else { } else {
dp = Math.max(0.1f,Vector.dotProduct(lightDir,normal)); dp = Math.max(0.1f,Vector.dotProduct(lightDir,normal));
} }
@ -416,10 +427,10 @@ public class Panel extends JPanel implements Runnable {
(triTransformed.b.pos.y-newCamera.y)*(triTransformed.b.pos.y-newCamera.y)+ (triTransformed.b.pos.y-newCamera.y)*(triTransformed.b.pos.y-newCamera.y)+
(triTransformed.b.pos.z-newCamera.z)*(triTransformed.b.pos.z-newCamera.z))*4)));*/ (triTransformed.b.pos.z-newCamera.z)*(triTransformed.b.pos.z-newCamera.z))*4)));*/
triViewed.A = Matrix.MultiplyVector(matView,triTransformed.A); triViewed.A = Matrix.MultiplyVector(matView,triRotation.A);
triViewed.B = Matrix.MultiplyVector(matView,triTransformed.B); triViewed.B = Matrix.MultiplyVector(matView,triRotation.B);
triViewed.C = Matrix.MultiplyVector(matView,triTransformed.C); triViewed.C = Matrix.MultiplyVector(matView,triRotation.C);
triTransformed.copyExtraDataTo(triViewed); triRotation.copyExtraDataTo(triViewed);
triViewed.setColor((0)+(0<<8)+((int)(dp*255)<<16)); triViewed.setColor((0)+(0<<8)+((int)(dp*255)<<16));
int clippedTriangles = 0; int clippedTriangles = 0;

@ -11,6 +11,7 @@ import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster; import java.awt.image.WritableRaster;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.HashMap; import java.util.HashMap;
@ -30,7 +31,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static boolean WIREFRAME = false; public static boolean WIREFRAME = false;
public static boolean PROFILING = false; public static boolean PROFILING = false;
public static boolean FLYING_MODE = true; public static boolean FLYING_MODE = false;
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;
@ -256,22 +257,22 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
if (answer.e.getButton()==MouseEvent.BUTTON1) { if (answer.e.getButton()==MouseEvent.BUTTON1) {
switch (answer.t.dir) { switch (answer.t.dir) {
case BlockType.FRONT:{ case BlockType.FRONT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,-1)),BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,-1)),Cube.class,BlockType.PLANKS,FacingDirection.SOUTH);
}break; }break;
case BlockType.BACK:{ case BlockType.BACK:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,1)),BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,1)),Cube.class,BlockType.PLANKS,FacingDirection.SOUTH);
}break; }break;
case BlockType.LEFT:{ case BlockType.LEFT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(-1,0,0)),BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(-1,0,0)),Cube.class,BlockType.PLANKS,FacingDirection.SOUTH);
}break; }break;
case BlockType.RIGHT:{ case BlockType.RIGHT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(1,0,0)),BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(1,0,0)),Cube.class,BlockType.PLANKS,FacingDirection.SOUTH);
}break; }break;
case BlockType.TOP:{ case BlockType.TOP:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,1,0)),BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,1,0)),Cube.class,BlockType.PLANKS,FacingDirection.SOUTH);
}break; }break;
case BlockType.BOTTOM:{ case BlockType.BOTTOM:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,-1,0)),BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,-1,0)),Cube.class,BlockType.PLANKS,FacingDirection.SOUTH);
}break; }break;
} }
} else } else
@ -285,11 +286,17 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
} }
} }
public static void addBlock(Vector pos,BlockType type,FacingDirection facingDir) { public static void addBlock(Vector pos,Class<?> meshType,BlockType type,FacingDirection facingDir) {
Block b = new Block(pos,new Staircase(type),FacingDirection.SOUTH); Block b;
try {
b = new Block(pos,(Mesh)meshType.getConstructor(BlockType.class).newInstance(type),FacingDirection.SOUTH);
b.setFacingDirection(facingDir); b.setFacingDirection(facingDir);
blockGrid.put(pos.x+"_"+pos.y+"_"+pos.z,b); blockGrid.put(pos.x+"_"+pos.y+"_"+pos.z,b);
b.updateFaces(); b.updateFaces();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
} }
public static void removeBlock(Vector pos) { public static void removeBlock(Vector pos) {
@ -319,7 +326,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
Random r = new Random(438107); Random r = new Random(438107);
for (int x=0;x<64;x++) { for (int x=0;x<64;x++) {
for (int z=0;z<64;z++) { for (int z=0;z<64;z++) {
addBlock(new Vector(x,0,z),BlockType.PLANKS,FacingDirection.SOUTH); addBlock(new Vector(x,0,z),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH);
//addBlock(new Vector(x,1,z),BlockType.JUNGLE_PLANK,FacingDirection.SOUTH); //addBlock(new Vector(x,1,z),BlockType.JUNGLE_PLANK,FacingDirection.SOUTH);
//addBlock(new Vector(x,2,z),BlockType.SPRUCE_PLANK,FacingDirection.SOUTH); //addBlock(new Vector(x,2,z),BlockType.SPRUCE_PLANK,FacingDirection.SOUTH);
/*for (int y=1;y<r.nextInt(5);y++) { /*for (int y=1;y<r.nextInt(5);y++) {
@ -346,10 +353,11 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
}*/ }*/
} }
} }
addBlock(new Vector(31,1,31),Staircase.class,BlockType.PLANKS,FacingDirection.NORTH);
Block b = new Block(new Vector(31,3,31),new Staircase(BlockType.PLANKS),FacingDirection.SOUTH); addBlock(new Vector(31,2,32),Staircase.class,BlockType.PLANKS,FacingDirection.EAST);
b.setFacingDirection(FacingDirection.SOUTH); addBlock(new Vector(31,3,33),Staircase.class,BlockType.PLANKS,FacingDirection.WEST);
blockGrid.put("31.0_3.0_31.0",b); addBlock(new Vector(31,4,34),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH);
addBlock(new Vector(31,5,35),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH);
for (int x=0;x<64;x++) { for (int x=0;x<64;x++) {
for (int y=1;y<5;y++) { for (int y=1;y<5;y++) {

Loading…
Cancel
Save