Preparing the BufferBuilder for new system

This commit is contained in:
Leonardo
2021-08-27 11:28:29 +02:00
parent 3dfa2d778a
commit 783c0c97a1
5 changed files with 210 additions and 110 deletions
@@ -3,12 +3,10 @@ package com.seibel.lod.objects.LevelPos;
import com.seibel.lod.objects.RegionPos;
import com.seibel.lod.util.LodUtil;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.Chunk;
import java.util.Comparator;
import java.util.Map;
public class LevelPos implements Cloneable, ImmutableLevelPos, MutableLevelPos
public class LevelPos implements Cloneable, ImmutableLevelPos, MutableLevelPos, Comparable<LevelPos>
{
public byte detailLevel;
public int posX;
@@ -117,6 +115,18 @@ public class LevelPos implements Cloneable, ImmutableLevelPos, MutableLevelPos
Math.floorDiv(posZ, width));
}
public int getRegionPosX()
{
int width = 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel);
return Math.floorDiv(posX, width);
}
public int getRegionPosZ()
{
int width = 1 << (LodUtil.REGION_DETAIL_LEVEL - detailLevel);
return Math.floorDiv(posZ, width);
}
public ChunkPos getChunkPos()
{
if (LodUtil.CHUNK_DETAIL_LEVEL >= detailLevel)
@@ -244,21 +254,23 @@ public class LevelPos implements Cloneable, ImmutableLevelPos, MutableLevelPos
}
}
public static LevelPosComparator getPosComparator(int playerPosX, int playerPosZ)
public static LevelPosDistanceComparator getPosComparator(int playerPosX, int playerPosZ)
{
return new LevelPosComparator(playerPosX,playerPosZ);
return new LevelPosDistanceComparator(playerPosX, playerPosZ);
}
public static LevelPosDetailComparator getPosAndDetailComparator(int playerPosX, int playerPosZ)
{
return new LevelPosDetailComparator(playerPosX,playerPosZ);
return new LevelPosDetailComparator(playerPosX, playerPosZ);
}
public static class LevelPosComparator implements Comparator<LevelPos>
public static class LevelPosDistanceComparator implements Comparator<LevelPos>
{
int playerPosX;
int playerPosZ;
public LevelPosComparator(int playerPosX, int playerPosZ){
public LevelPosDistanceComparator(int playerPosX, int playerPosZ)
{
this.playerPosX = playerPosX;
this.playerPosZ = playerPosZ;
}
@@ -267,8 +279,8 @@ public class LevelPos implements Cloneable, ImmutableLevelPos, MutableLevelPos
public int compare(LevelPos first, LevelPos second)
{
return Integer.compare(
first.minDistance(playerPosX,playerPosZ),
second.minDistance(playerPosX,playerPosZ));
first.minDistance(playerPosX, playerPosZ),
second.minDistance(playerPosX, playerPosZ));
}
}
@@ -276,7 +288,9 @@ public class LevelPos implements Cloneable, ImmutableLevelPos, MutableLevelPos
{
int playerPosX;
int playerPosZ;
public LevelPosDetailComparator(int playerPosX, int playerPosZ){
public LevelPosDetailComparator(int playerPosX, int playerPosZ)
{
this.playerPosX = playerPosX;
this.playerPosZ = playerPosZ;
}
@@ -285,16 +299,62 @@ public class LevelPos implements Cloneable, ImmutableLevelPos, MutableLevelPos
public int compare(LevelPos first, LevelPos second)
{
int compareResult = Integer.compare(first.detailLevel, second.detailLevel);
if (compareResult != 0)
if (compareResult == 0)
{
compareResult = Integer.compare(
first.minDistance(playerPosX,playerPosZ),
second.minDistance(playerPosX,playerPosZ));
first.minDistance(playerPosX, playerPosZ),
second.minDistance(playerPosX, playerPosZ));
}
return compareResult;
}
}
public static LevelPosComparator getComparator()
{
return new LevelPosComparator();
}
public static class LevelPosComparator implements Comparator<LevelPos>
{
@Override
public int compare(LevelPos first, LevelPos second)
{
int compareResult = Integer.compare(first.detailLevel, second.detailLevel);
if (compareResult == 0)
{
compareResult = Integer.compare(
first.posX,
second.posX);
}
if (compareResult == 0)
{
compareResult = Integer.compare(
first.posZ,
second.posZ);
}
return compareResult;
}
}
@Override
public int compareTo(LevelPos other)
{
int compareResult = Integer.compare(this.detailLevel, other.detailLevel);
if (compareResult == 0)
{
compareResult = Integer.compare(
this.posX,
other.posX);
}
if (compareResult == 0)
{
compareResult = Integer.compare(
this.posZ,
other.posZ);
}
return compareResult;
}
@Override
public LevelPos clone()
@@ -20,10 +20,8 @@ package com.seibel.lod.objects;
import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.stream.Collectors;
import com.seibel.lod.enums.DistanceGenerationMode;
@@ -311,7 +309,7 @@ public class LodDimension
{
int regionX;
int regionZ;
LevelPos levelPos;
LevelPos levelPos = new LevelPos();
for (int x = 0; x < regions.length; x++)
{
@@ -319,7 +317,7 @@ public class LodDimension
{
regionX = (x + center.x) - halfWidth;
regionZ = (z + center.z) - halfWidth;
levelPos = new LevelPos(LodUtil.REGION_DETAIL_LEVEL, regionX, regionZ);
levelPos.changeParameters(LodUtil.REGION_DETAIL_LEVEL, regionX, regionZ);
//we start checking from the first circle. If the whole region is in the circle
//we proceed to cut all the level lower than the level of circle 1 and we break
//if this is not the case w
@@ -348,7 +346,7 @@ public class LodDimension
{
int regionX;
int regionZ;
LevelPos levelPos;
LevelPos levelPos = new LevelPos();
RegionPos regionPos;
LodRegion region;
byte targetDetailLevel;
@@ -358,7 +356,7 @@ public class LodDimension
{
regionX = (x + center.x) - halfWidth;
regionZ = (z + center.z) - halfWidth;
levelPos = new LevelPos(LodUtil.REGION_DETAIL_LEVEL, regionX, regionZ);
levelPos.changeParameters(LodUtil.REGION_DETAIL_LEVEL, regionX, regionZ);
regionPos = new RegionPos(regionX, regionZ);
for(byte index = LodUtil.BLOCK_DETAIL_LEVEL; index <= LodUtil.REGION_DETAIL_LEVEL; index++){
@@ -445,7 +443,7 @@ public class LodDimension
int zIndex;
LodRegion region;
RegionPos regionPos;
LevelPos regionLevelPos;
LevelPos regionLevelPos = new LevelPos();
List<LevelPos> listOfData = new ArrayList<>();
for (int xRegion = 0; xRegion < n; xRegion++)
{
@@ -456,7 +454,7 @@ public class LodDimension
xIndex = (xRegion + center.x) - halfWidth;
zIndex = (zRegion + center.z) - halfWidth;
regionPos = new RegionPos(xIndex, zIndex);
regionLevelPos = new LevelPos(LodUtil.REGION_DETAIL_LEVEL, regionPos.x, regionPos.z);
regionLevelPos.changeParameters(LodUtil.REGION_DETAIL_LEVEL, regionPos.x, regionPos.z);
if (end >= regionLevelPos.minDistance(playerPosX, playerPosZ) &&
start <= regionLevelPos.maxDistance(playerPosX, playerPosZ))
{
@@ -481,15 +479,13 @@ public class LodDimension
return levelMinPosList;
}
/**
* method to get all the nodes that have to be rendered based on the position of the player
*
* @return list of nodes
*/
public List<LevelPos> getDataToRender(RegionPos regionPos, int playerPosX, int playerPosZ, int start, int end, byte detailLevel, boolean zFix)
public void getDataToRender(ConcurrentNavigableMap<LevelPos,List<Integer>> dataToRender, RegionPos regionPos, int playerPosX, int playerPosZ, int start, int end, byte detailLevel, boolean zFix)
{
List<LevelPos> listOfData = new ArrayList<>();
LevelPos regionLevelPos = new LevelPos(LodUtil.REGION_DETAIL_LEVEL, regionPos.x, regionPos.z);
try
{
@@ -497,14 +493,10 @@ public class LodDimension
start <= regionLevelPos.maxDistance(playerPosX, playerPosZ))
{
LodRegion region = getRegion(new LevelPos(LodUtil.REGION_DETAIL_LEVEL, regionPos.x, regionPos.z).getConvertedLevelPos(detailLevel));
listOfData.addAll(region.getDataToRender(playerPosX, playerPosZ, start, end, detailLevel,zFix));
region.getDataToRender(dataToRender,playerPosX, playerPosZ, start, end, detailLevel,zFix);
}
}catch (Exception e){
//e.printStackTrace();
}finally
{
return listOfData;
e.printStackTrace();
}
}
@@ -1,9 +1,8 @@
package com.seibel.lod.objects;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.concurrent.ConcurrentNavigableMap;
import com.seibel.lod.builders.LodBuilder;
import com.seibel.lod.enums.DistanceGenerationMode;
@@ -298,76 +297,81 @@ public class LodRegion implements Serializable
/**
* @return
*/
public List<LevelPos> getDataToRender(int playerPosX, int playerPosZ, int start, int end, byte detailLevel, boolean zFix)
public void getDataToRender(ConcurrentNavigableMap<LevelPos,List<Integer>> dataToRender, int playerPosX, int playerPosZ, int start, int end, byte detailLevel, boolean zFix)
{
LevelPos levelPos = new LevelPos(LodUtil.REGION_DETAIL_LEVEL, 0, 0);
return getDataToRender(levelPos, playerPosX, playerPosZ, start, end, detailLevel, zFix);
getDataToRender(dataToRender, levelPos, playerPosX, playerPosZ, start, end, detailLevel, zFix);
}
/**
* @return
*/
private List<LevelPos> getDataToRender(LevelPos levelPos, int playerPosX, int playerPosZ, int start, int end, byte detailLevel, boolean zFix)
private void getDataToRender(ConcurrentNavigableMap<LevelPos,List<Integer>> dataToRender, LevelPos levelPos, int playerPosX, int playerPosZ, int start, int end, byte detailLevel, boolean zFix)
{
List<LevelPos> levelPosList = new ArrayList<>();
int size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - levelPos.detailLevel);
int width = (int) Math.pow(2, levelPos.detailLevel);
//here i calculate the the LevelPos is in range
//This is important to avoid any kind of hole in the rendering
int maxDistance = levelPos.maxDistance(playerPosX, playerPosZ, regionPosX, regionPosZ);
int minDistance = levelPos.minDistance(playerPosX, playerPosZ, regionPosX, regionPosZ);
//To avoid z fighting: if the pos is touching the end radius at detailLevel + 1 then we stop
//cause this area will be occupied by bigger block
if (levelPos.detailLevel == detailLevel + 1 && end <= maxDistance && minDistance <= end && zFix)
try
{
return levelPosList;
}
int size = (int) Math.pow(2, LodUtil.REGION_DETAIL_LEVEL - levelPos.detailLevel);
int width = (int) Math.pow(2, levelPos.detailLevel);
if (!(start <= maxDistance && minDistance < end) || levelPos.detailLevel < detailLevel)
{
return levelPosList;
}
//here i calculate the the LevelPos is in range
//This is important to avoid any kind of hole in the rendering
int maxDistance = levelPos.maxDistance(playerPosX, playerPosZ, regionPosX, regionPosZ);
int minDistance = levelPos.minDistance(playerPosX, playerPosZ, regionPosX, regionPosZ);
//we have reached the target detail level
if (detailLevel == levelPos.detailLevel)
{
levelPosList.add(new LevelPos(levelPos.detailLevel, levelPos.posX + regionPosX * size, levelPos.posZ + regionPosZ * size));
} else
{
int childPosX = levelPos.posX * 2;
int childPosZ = levelPos.posZ * 2;
LevelPos childPos;
int childrenCount = 0;
for (int x = 0; x <= 1; x++)
//To avoid z fighting: if the pos is touching the end radius at detailLevel + 1 then we stop
//cause this area will be occupied by bigger block
if (levelPos.detailLevel == detailLevel + 1 && end <= maxDistance && minDistance <= end && zFix)
{
for (int z = 0; z <= 1; z++)
{
childPos = new LevelPos((byte) (levelPos.detailLevel - 1), childPosX + x, childPosZ + z);
if (doesDataExist(childPos)) childrenCount++;
}
return;
}
//If all the four children exist we go deeper
if (childrenCount == 4)
if (!(start <= maxDistance && minDistance < end) || levelPos.detailLevel < detailLevel)
{
levelPosList.clear();
return;
}
//we have reached the target detail level
if (detailLevel == levelPos.detailLevel)
{
dataToRender.put(new LevelPos(levelPos.detailLevel, levelPos.posX + regionPosX * size, levelPos.posZ + regionPosZ * size), new ArrayList<>());
} else
{
int childPosX = levelPos.posX * 2;
int childPosZ = levelPos.posZ * 2;
LevelPos childPos;
int childrenCount = 0;
for (int x = 0; x <= 1; x++)
{
for (int z = 0; z <= 1; z++)
{
childPos = new LevelPos((byte) (levelPos.detailLevel - 1), childPosX + x, childPosZ + z);
levelPosList.addAll(getDataToRender(childPos, playerPosX, playerPosZ, start, end, detailLevel, zFix));
if (doesDataExist(childPos)) childrenCount++;
}
}
} else
{
levelPosList.add(new LevelPos(levelPos.detailLevel, levelPos.posX + regionPosX * size, levelPos.posZ + regionPosZ * size));
//If all the four children exist we go deeper
if (childrenCount == 4)
{
for (int x = 0; x <= 1; x++)
{
for (int z = 0; z <= 1; z++)
{
childPos = new LevelPos((byte) (levelPos.detailLevel - 1), childPosX + x, childPosZ + z);
getDataToRender(dataToRender, childPos, playerPosX, playerPosZ, start, end, detailLevel, zFix);
}
}
} else
{
dataToRender.put(new LevelPos(levelPos.detailLevel, levelPos.posX + regionPosX * size, levelPos.posZ + regionPosZ * size), new ArrayList<>());
}
}
return;
}catch(ClassCastException e){
System.out.println(dataToRender);
e.printStackTrace();
throw e;
}
return levelPosList;
}
/**