From 8a61a5f0e7fe430d572a662b373838f079f6184f Mon Sep 17 00:00:00 2001 From: Leonardo Date: Sun, 15 Aug 2021 16:40:13 +0200 Subject: [PATCH] Slightly optimize addLodToBuffer --- .../lod/builders/LodNodeBufferBuilder.java | 41 ++-- .../CubicLodNodeTemplate.java | 212 +++++++++--------- 2 files changed, 123 insertions(+), 130 deletions(-) diff --git a/src/main/java/com/seibel/lod/builders/LodNodeBufferBuilder.java b/src/main/java/com/seibel/lod/builders/LodNodeBufferBuilder.java index 49be8a5b7..f796c6158 100644 --- a/src/main/java/com/seibel/lod/builders/LodNodeBufferBuilder.java +++ b/src/main/java/com/seibel/lod/builders/LodNodeBufferBuilder.java @@ -21,6 +21,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; +import com.seibel.lod.objects.LodQuadTree; import org.lwjgl.opengl.GL11; import com.seibel.lod.builders.worldGeneration.LodNodeGenWorker; @@ -153,7 +154,6 @@ public class LodNodeBufferBuilder // used when determining which chunks are closer when queuing distance generation int minChunkDist = Integer.MAX_VALUE; - // x axis for (int i = 0; i < numbChunksWide; i++) { @@ -279,30 +279,29 @@ public class LodNodeBufferBuilder // get the desired LodTemplate and // add this LOD to the buffer - LodConfig.CLIENT.lodTemplate.get(). - template.addLodToBuffer(currentBuffer, lodDim, lod, - xOffset , yOffset, zOffset, renderer.debugging, detail); - /* - LodDetail detail = LodConfig.CLIENT.lodDetail.get(); - for(int x = 0; x < detail.dataPointLengthCount; x++){ - for(int z = 0; z < detail.dataPointLengthCount; z++) { - int posX = LodUtil.convertLevelPos(lod.startBlockPos.getX() + (x*detail.dataPointWidth), 0, detail.detailLevel); - int posZ = LodUtil.convertLevelPos(lod.startBlockPos.getZ() + (z*detail.dataPointWidth), 0, detail.detailLevel); - LodQuadTreeNode newLod = lodDim.getLodFromCoordinates(posX, posZ, detail.detailLevel); - System.out.print("printing "); - System.out.println(newLod); - if(newLod != null) { - LodConfig.CLIENT.lodTemplate.get(). - template.addLodToBuffer(currentBuffer, lodDim, newLod, - xOffset + (x*detail.dataPointWidth), yOffset, zOffset + (z*detail.dataPointWidth), renderer.debugging); - } + + for (int k = 0; k < detail.dataPointLengthCount * detail.dataPointLengthCount; k++) { + // how much to offset this LOD by + int startX = detail.startX[k]; + int startZ = detail.startZ[k]; + + // get the QuadTree location of this + LodQuadTree lodTree = lodDim.getLevelFromPos( + LodUtil.convertLevelPos((int) xOffset + startX, 0, LodUtil.CHUNK_DETAIL_LEVEL), + LodUtil.convertLevelPos((int) zOffset + startZ, 0, LodUtil.CHUNK_DETAIL_LEVEL), + LodUtil.CHUNK_DETAIL_LEVEL); + LodQuadTreeNode newLod = lodTree.getNodeAtPos( + LodUtil.convertLevelPos((int) xOffset + startX, 0, detail.detailLevel), + LodUtil.convertLevelPos((int) zOffset + startZ, 0, detail.detailLevel), + detail.detailLevel); + if (newLod != null) { + LodConfig.CLIENT.lodTemplate.get(). + template.addLodToBuffer(currentBuffer, lodDim, newLod, + xOffset + startX, yOffset, zOffset + startZ, renderer.debugging, detail); } } - */ } } - - // issue #19 // TODO add a way for a server side mod to generate chunks requested here if(mc.hasSingleplayerServer()) diff --git a/src/main/java/com/seibel/lod/builders/lodNodeTemplates/CubicLodNodeTemplate.java b/src/main/java/com/seibel/lod/builders/lodNodeTemplates/CubicLodNodeTemplate.java index 9d9804ec9..f40afe214 100644 --- a/src/main/java/com/seibel/lod/builders/lodNodeTemplates/CubicLodNodeTemplate.java +++ b/src/main/java/com/seibel/lod/builders/lodNodeTemplates/CubicLodNodeTemplate.java @@ -16,11 +16,13 @@ * along with this program. If not, see . */ package com.seibel.lod.builders.lodNodeTemplates; + import java.awt.Color; import com.seibel.lod.enums.LodDetail; import com.seibel.lod.enums.ShadingMode; import com.seibel.lod.handlers.LodConfig; +import com.seibel.lod.objects.LodQuadTree; import com.seibel.lod.objects.LodQuadTreeDimension; import com.seibel.lod.objects.LodQuadTreeNode; import com.seibel.lod.util.LodUtil; @@ -35,71 +37,69 @@ import net.minecraft.util.math.AxisAlignedBB; * @author James Seibel * @version 8-10-2021 */ -public class CubicLodNodeTemplate extends AbstractLodNodeTemplate -{ - public CubicLodNodeTemplate() - { +public class CubicLodNodeTemplate extends AbstractLodNodeTemplate { + public CubicLodNodeTemplate() { } - + @Override public void addLodToBuffer(BufferBuilder buffer, LodQuadTreeDimension lodDim, LodQuadTreeNode lod, double xOffset, double yOffset, double zOffset, - boolean debugging, LodDetail detail) - { + boolean debugging, LodDetail detail) { AxisAlignedBB bbox; - + // add each LOD for the detail level - for(int i = 0; i < detail.dataPointLengthCount * detail.dataPointLengthCount; i++) - { - // how much to offset this LOD by - int startX = detail.startX[i]; - int startZ = detail.startZ[i]; - - // get the QuadTree location of this LOD - int posX = LodUtil.convertLevelPos((int) xOffset + startX, 0, detail.detailLevel); - int posZ = LodUtil.convertLevelPos((int) zOffset + startZ, 0, detail.detailLevel); - - LodQuadTreeNode newLod = lodDim.getLodFromCoordinates(posX, posZ, detail.detailLevel); - if(newLod != null) - { - bbox = generateBoundingBox( - newLod.getLodDataPoint().height, - newLod.getLodDataPoint().depth, - newLod.width, - xOffset + startX, - yOffset, - zOffset + startZ); - - - Color color = newLod.getLodDataPoint().color; - if (LodConfig.CLIENT.debugMode.get()) - { - color = LodUtil.DEBUG_DETAIL_LEVEL_COLORS[detail.detailLevel]; - } - - if (bbox != null) - { - addBoundingBoxToBuffer(buffer, bbox, color); - } - } - } + bbox = generateBoundingBox( + lod.getLodDataPoint().height, + lod.getLodDataPoint().depth, + lod.width, + xOffset, + yOffset, + zOffset); + + + Color color = lod.getLodDataPoint().color; + if (LodConfig.CLIENT.debugMode.get()) { + color = LodUtil.DEBUG_DETAIL_LEVEL_COLORS[detail.detailLevel]; + } + + if (bbox != null) { + addBoundingBoxToBuffer(buffer, bbox, color); + } } +/* + @Override + public void addLodToBuffer(BufferBuilder buffer, + LodQuadTreeDimension lodDim, LodQuadTreeNode lod, + double xOffset, double yOffset, double zOffset, + boolean debugging) { + AxisAlignedBB bbox; - - - - private AxisAlignedBB generateBoundingBox(int height, int depth, int width, double xOffset, double yOffset, double zOffset) - { + bbox = generateBoundingBox( + lod.getLodDataPoint().height, + lod.getLodDataPoint().depth, + lod.width, + xOffset, + yOffset, + zOffset); + + Color color = lod.getLodDataPoint().color; + + if (bbox != null) { + addBoundingBoxToBuffer(buffer, bbox, color); + } + + }*/ + + private AxisAlignedBB generateBoundingBox(int height, int depth, int width, double xOffset, double yOffset, double zOffset) { // don't add an LOD if it is empty if (height == -1 && depth == -1) return null; - if (depth == height) - { + if (depth == height) { // if the top and bottom points are at the same height // render this LOD as 1 block thick height++; @@ -109,81 +109,75 @@ public class CubicLodNodeTemplate extends AbstractLodNodeTemplate } + private void addBoundingBoxToBuffer(BufferBuilder buffer, AxisAlignedBB bb, Color c) { + Color topColor = c; + Color northSouthColor = c; + Color eastWestColor = c; + Color bottomColor = c; - private void addBoundingBoxToBuffer(BufferBuilder buffer, AxisAlignedBB bb, Color c) - { - Color topColor = c; - Color northSouthColor = c; - Color eastWestColor = c; - Color bottomColor = c; + // darken the bottom and side colors if requested + if (LodConfig.CLIENT.shadingMode.get() == ShadingMode.DARKEN_SIDES) { + // the side colors are different because + // when using fast lighting in Minecraft the north/south + // and east/west sides are different in a similar way + int northSouthDarkenAmount = 25; + int eastWestDarkenAmount = 50; + int bottomDarkenAmount = 75; - // darken the bottom and side colors if requested - if (LodConfig.CLIENT.shadingMode.get() == ShadingMode.DARKEN_SIDES) - { - // the side colors are different because - // when using fast lighting in Minecraft the north/south - // and east/west sides are different in a similar way - int northSouthDarkenAmount = 25; - int eastWestDarkenAmount = 50; - int bottomDarkenAmount = 75; - - northSouthColor = new Color(Math.max(0, c.getRed() - northSouthDarkenAmount), Math.max(0, c.getGreen() - northSouthDarkenAmount), Math.max(0, c.getBlue() - northSouthDarkenAmount), c.getAlpha()); - eastWestColor = new Color(Math.max(0, c.getRed() - eastWestDarkenAmount), Math.max(0, c.getGreen() - eastWestDarkenAmount), Math.max(0, c.getBlue() - eastWestDarkenAmount), c.getAlpha()); - bottomColor = new Color(Math.max(0, c.getRed() - bottomDarkenAmount), Math.max(0, c.getGreen() - bottomDarkenAmount), Math.max(0, c.getBlue() - bottomDarkenAmount), c.getAlpha()); - } + northSouthColor = new Color(Math.max(0, c.getRed() - northSouthDarkenAmount), Math.max(0, c.getGreen() - northSouthDarkenAmount), Math.max(0, c.getBlue() - northSouthDarkenAmount), c.getAlpha()); + eastWestColor = new Color(Math.max(0, c.getRed() - eastWestDarkenAmount), Math.max(0, c.getGreen() - eastWestDarkenAmount), Math.max(0, c.getBlue() - eastWestDarkenAmount), c.getAlpha()); + bottomColor = new Color(Math.max(0, c.getRed() - bottomDarkenAmount), Math.max(0, c.getGreen() - bottomDarkenAmount), Math.max(0, c.getBlue() - bottomDarkenAmount), c.getAlpha()); + } + + + // apply the user specified saturation and brightness + float saturationMultiplier = LodConfig.CLIENT.saturationMultiplier.get().floatValue(); + float brightnessMultiplier = LodConfig.CLIENT.brightnessMultiplier.get().floatValue(); + + topColor = applySaturationAndBrightnessMultipliers(topColor, saturationMultiplier, brightnessMultiplier); + northSouthColor = applySaturationAndBrightnessMultipliers(northSouthColor, saturationMultiplier, brightnessMultiplier); + bottomColor = applySaturationAndBrightnessMultipliers(bottomColor, saturationMultiplier, brightnessMultiplier); - // apply the user specified saturation and brightness - float saturationMultiplier = LodConfig.CLIENT.saturationMultiplier.get().floatValue(); - float brightnessMultiplier = LodConfig.CLIENT.brightnessMultiplier.get().floatValue(); - - topColor = applySaturationAndBrightnessMultipliers(topColor, saturationMultiplier, brightnessMultiplier); - northSouthColor = applySaturationAndBrightnessMultipliers(northSouthColor, saturationMultiplier, brightnessMultiplier); - bottomColor = applySaturationAndBrightnessMultipliers(bottomColor, saturationMultiplier, brightnessMultiplier); - - - // top (facing up) - addPosAndColor(buffer, bb.minX, bb.maxY, bb.minZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.maxY, bb.maxZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.maxY, bb.maxZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.maxY, bb.minZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.maxY, bb.minZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.maxY, bb.maxZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.maxY, bb.maxZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.maxY, bb.minZ, topColor.getRed(), topColor.getGreen(), topColor.getBlue(), topColor.getAlpha()); // bottom (facing down) - addPosAndColor(buffer, bb.maxX, bb.minY, bb.minZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.minY, bb.maxZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.minY, bb.maxZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.minY, bb.minZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.minY, bb.minZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.minY, bb.maxZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.minY, bb.maxZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.minY, bb.minZ, bottomColor.getRed(), bottomColor.getGreen(), bottomColor.getBlue(), bottomColor.getAlpha()); // south (facing -Z) - addPosAndColor(buffer, bb.maxX, bb.minY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.maxY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.maxY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.minY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.minY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.maxY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.maxY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.minY, bb.maxZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); // north (facing +Z) - addPosAndColor(buffer, bb.minX, bb.minY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.maxY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.maxY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.minY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.minY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.maxY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.maxY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.minY, bb.minZ, northSouthColor.getRed(), northSouthColor.getGreen(), northSouthColor.getBlue(), northSouthColor.getAlpha()); // west (facing -X) - addPosAndColor(buffer, bb.minX, bb.minY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.minY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.maxY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); - addPosAndColor(buffer, bb.minX, bb.maxY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.minY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.minY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.maxY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.minX, bb.maxY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); // east (facing +X) - addPosAndColor(buffer, bb.maxX, bb.maxY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.maxY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.minY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); - addPosAndColor(buffer, bb.maxX, bb.minY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.maxY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.maxY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.minY, bb.maxZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); + addPosAndColor(buffer, bb.maxX, bb.minY, bb.minZ, eastWestColor.getRed(), eastWestColor.getGreen(), eastWestColor.getBlue(), eastWestColor.getAlpha()); } - - - + + @Override - public int getBufferMemoryForSingleNode(int detailLevel) - { + public int getBufferMemoryForSingleNode(int detailLevel) { // (sidesOnACube * pointsInASquare * (positionPoints + colorPoints))) * howManyPointsPerLodChunk return (6 * 4 * (3 + 4)); } - + }