From 10d66a47759f98a5073d51174e8f738b1f91680c Mon Sep 17 00:00:00 2001 From: Leonardo Date: Mon, 16 Aug 2021 23:03:21 +0200 Subject: [PATCH] LodRegion now works (might still have bugs) --- src/main/java/com/seibel/lod/Main.java | 21 ++++++ .../com/seibel/lod/objects/LodRegion.java | 72 ++++++++++++------- 2 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/seibel/lod/Main.java diff --git a/src/main/java/com/seibel/lod/Main.java b/src/main/java/com/seibel/lod/Main.java new file mode 100644 index 000000000..51b33264a --- /dev/null +++ b/src/main/java/com/seibel/lod/Main.java @@ -0,0 +1,21 @@ +package com.seibel.lod; + +import com.seibel.lod.objects.LodDataPoint; +import com.seibel.lod.objects.LodRegion; +import com.seibel.lod.objects.RegionPos; + +import java.awt.*; + +public class Main { + public static void main(String[] args){ + LodRegion lodRegion = new LodRegion((byte) 0,new RegionPos(0,0)); + lodRegion.setData((byte) 2,0,0, new LodDataPoint((short) 2,(short) 30, new Color(100,100,100)), (byte) 2,true); + try { + System.out.print("test "); + System.out.println(lodRegion.getData((byte) 6, 0, 0)); + }catch (Exception e){ + e.printStackTrace(); + } + return; + } +} diff --git a/src/main/java/com/seibel/lod/objects/LodRegion.java b/src/main/java/com/seibel/lod/objects/LodRegion.java index 37442f1eb..561fb6e29 100644 --- a/src/main/java/com/seibel/lod/objects/LodRegion.java +++ b/src/main/java/com/seibel/lod/objects/LodRegion.java @@ -12,17 +12,16 @@ import java.io.Serializable; * 0 for x, 1 for y, 2 for z in 3D */ -public class LodRegion implements Serializable { +public class LodRegion{ //x coord, private byte minLevelOfDetail; - + private static final byte POSSIBLE_LOD = 10; private int numberOfPoints; - private byte[] sizes; - private short[] widths; - //For each of the following field the first slot is for the level of detail //Important: byte have a [-128, 127] range. When converting from or to int a 128 should be added or removed + //If there is a bug with color then it's probably caused by this. + //in the future other fields like transparency and light level could be added private byte[][][][] colors; private short[][][] height; @@ -30,29 +29,28 @@ public class LodRegion implements Serializable { private short[][][] depth; //a new node will have 0 as generationType + //a node with 1 is node private byte[][][] generationType; - private final RegionPos regionPos; + private int regionPosX; + private int regionPosZ; - public LodRegion(byte levelOfDetail, RegionPos regionPos){ - this.regionPos = regionPos; - - sizes = new byte[LodUtil.REGION_DETAIL_LEVEL]; - widths = new short[LodUtil.REGION_DETAIL_LEVEL]; + public LodRegion(byte minimumLevelOfDetail, RegionPos regionPos){ + this.regionPosX = regionPos.x; + this.regionPosZ = regionPos.z; //Array of matrices of arrays - colors = new byte[LodUtil.REGION_DETAIL_LEVEL][][][]; + colors = new byte[POSSIBLE_LOD][][][]; //Arrays of matrices - height = new short[LodUtil.REGION_DETAIL_LEVEL][][]; - depth = new short[LodUtil.REGION_DETAIL_LEVEL][][]; - generationType = new byte[LodUtil.REGION_DETAIL_LEVEL][][]; + height = new short[POSSIBLE_LOD][][]; + depth = new short[POSSIBLE_LOD][][]; + generationType = new byte[POSSIBLE_LOD][][]; //Initialize all the different matrices - for(int lod = levelOfDetail; lod <= LodUtil.REGION_DETAIL_LEVEL; lod ++){ + for(byte lod = minimumLevelOfDetail; lod <= LodUtil.REGION_DETAIL_LEVEL; lod ++){ int size = (short) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - lod); - int width = (short) Math.pow(2, lod); colors[lod] = new byte[size][size][3]; height[lod] = new short[size][size]; depth[lod] = new short[size][size]; @@ -61,7 +59,6 @@ public class LodRegion implements Serializable { } } - /** * This method can be used to insert data into the LodRegion * @param lod @@ -91,6 +88,8 @@ public class LodRegion implements Serializable { * @return */ public boolean setData(byte lod, int posX, int posZ, byte red, byte green, byte blue, short height, short depth, byte generationType, boolean update){ + posX = Math.floorMod(posX, (int) Math.pow(2,lod)); + posZ = Math.floorMod(posZ, (int) Math.pow(2,lod)); if( (this.generationType[lod][posX][posZ] == 0) || (generationType < this.generationType[lod][posX][posZ]) ) { //update the number of node present @@ -110,10 +109,10 @@ public class LodRegion implements Serializable { //update could be stopped and a single big update could be done at the end if(update) { - for (int tempLod = lod + 1; tempLod <= LodUtil.REGION_DETAIL_LEVEL; tempLod++) { + for (byte tempLod = (byte) (lod + 1); tempLod <= LodUtil.REGION_DETAIL_LEVEL; tempLod++) { tempPosX = Math.floorDiv(tempPosX, 2); tempPosZ = Math.floorDiv(tempPosZ, 2); - update((byte) tempLod, tempPosX, tempPosZ); + update(tempLod, tempPosX, tempPosZ); } } return true; //added @@ -144,8 +143,11 @@ public class LodRegion implements Serializable { } */ private void update(byte lod, int posX, int posZ){ + posX = Math.floorMod(posX, (int) Math.pow(2,lod)); + posZ = Math.floorMod(posZ, (int) Math.pow(2,lod)); boolean[][] children = getChildren(lod, posX, posZ); int numberOfChild = 0; + for(int x = 0; x <= 1; x++) { for (int z = 0; z <= 1; z++) { if(children[x][z]){ @@ -153,9 +155,14 @@ public class LodRegion implements Serializable { } } } + if(numberOfChild>0) { + + //int minDepth = Integer.MAX_VALUE; //int maxDepth = Integer.MIN_VALUE; //int minHeight = Integer.MAX_VALUE; + //int maxHeight = Integer.MIN_VALUE; + byte minGenerationType = 0; for (int x = 0; x <= 1; x++) { for (int z = 0; z <= 1; z++) { @@ -163,21 +170,32 @@ public class LodRegion implements Serializable { int newPosX = 2 * posX + x; int newPosZ = 2 * posZ + z; for (int col = 0; col <= 2; col++) { - colors[lod][posX][posZ][col] = (byte) (colors[lod - 1][newPosX][newPosZ][col] / numberOfChild); + colors[lod][posX][posZ][col] += (byte) (colors[lod - 1][newPosX][newPosZ][col] / numberOfChild); } - depth[lod][posX][posZ] = (short) (depth[lod - 1][newPosX][newPosZ] / numberOfChild); + //TODO ability to change between mean, max and min. - height[lod][posX][posZ] = (short) (height[lod - 1][newPosX][newPosZ] / numberOfChild); - minGenerationType = (byte) Math.min(minGenerationType, generationType[lod][posX][posZ]); + height[lod][posX][posZ] += (short) (height[lod - 1][newPosX][newPosZ] / numberOfChild); + //minHeight = Math.min( height[lod - 1][newPosX][newPosZ] , maxHeight); + //maxHeight = Math.max( height[lod - 1][newPosX][newPosZ] , minHeight); + + depth[lod][posX][posZ] += (short) (depth[lod - 1][newPosX][newPosZ] / numberOfChild); + //minDepth = Math.min( depth[lod - 1][newPosX][newPosZ] , maxDepth); + //maxDepth = Math.max( depth[lod - 1][newPosX][newPosZ] , minDepth); + + minGenerationType = (byte) Math.max(minGenerationType, generationType[lod - 1][newPosX][newPosZ]); } } } + //height[lod][posX][posZ] = minHeight; + //depth[lod][posX][posZ] = maxDepth; generationType[lod][posX][posZ] = minGenerationType; } } private boolean[][] getChildren(byte lod, int posX, int posZ){ + posX = Math.floorMod(posX, (int) Math.pow(2,lod)); + posZ = Math.floorMod(posZ, (int) Math.pow(2,lod)); boolean[][] children = new boolean[2][2]; int numberOfChild=0; if(minLevelOfDetail == lod){ @@ -190,4 +208,10 @@ public class LodRegion implements Serializable { } return children; } + + private void removeDetailLevel(byte lod, byte[][][] colors, short[][] height, short[][] depth, byte[][] generationType){ + } + + private void addDetailLevel(byte lod, int posX, int posZ){ + } }