Skip to content

Commit

Permalink
2.8.0 Cubic Chunk calculation, using glList during caching
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <daniel@shedaniel.me>
  • Loading branch information
shedaniel committed Feb 17, 2021
1 parent 44abdbc commit 79e1db0
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 124 deletions.
131 changes: 131 additions & 0 deletions common/src/main/java/me/shedaniel/lightoverlay/common/ChunkData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package me.shedaniel.lightoverlay.common;

import it.unimi.dsi.fastutil.longs.Long2ByteMap;
import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.lwjgl.opengl.GL11;

import java.io.Closeable;

import static me.shedaniel.lightoverlay.common.LightOverlay.*;

public class ChunkData implements Closeable {
private static final IllegalStateException WRONG_TYPE = new IllegalStateException("Wrong type accessed!");
private Long2ByteMap data;
private int glListIndex = 0;
private boolean generatedList = false;

public ChunkData() {
this(new Long2ByteOpenHashMap());
}

public ChunkData(Long2ByteMap data) {
this.data = data;
}

public Long2ByteMap data() {
return data;
}

private void compileList(Level level, CollisionContext collisionContext) {
generatedList = true;

if (data().isEmpty()) {
glListIndex = 0;
return;
}

glListIndex = GL11.glGenLists(3);
GL11.glNewList(glListIndex, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_LINES);
color(redColor);

BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
byte crossType = objectEntry.getByteValue();
mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
if (crossType == CROSS_RED) {
renderCross(level, mutable, collisionContext);
}
}

GL11.glEnd();
GL11.glEndList();

GL11.glNewList(glListIndex + 1, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_LINES);
color(yellowColor);

for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
byte crossType = objectEntry.getByteValue();
mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
if (crossType == CROSS_YELLOW) {
renderCross(level, mutable, collisionContext);
}
}

GL11.glEnd();
GL11.glEndList();

GL11.glNewList(glListIndex + 2, GL11.GL_COMPILE);
GL11.glBegin(GL11.GL_LINES);
color(secondaryColor);

for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
byte crossType = objectEntry.getByteValue();
mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
if (crossType == CROSS_SECONDARY) {
renderCross(level, mutable, collisionContext);
}
}

GL11.glEnd();
GL11.glEndList();
}

public void renderList(Level level, CollisionContext collisionContext) {
if (!generatedList) {
compileList(level, collisionContext);
}

if (glListIndex != 0) {
GL11.glCallList(glListIndex);
GL11.glCallList(glListIndex + 1);
GL11.glCallList(glListIndex + 2);
}
}

private static void color(int color) {
int red = (color >> 16) & 255;
int green = (color >> 8) & 255;
int blue = color & 255;
GL11.glColor4f(red / 255f, green / 255f, blue / 255f, 1f);
}

public static void renderCross(Level level, BlockPos pos, CollisionContext collisionContext) {
double blockOffset = 0;
VoxelShape upperOutlineShape = level.getBlockState(pos).getShape(level, pos, collisionContext);
if (!upperOutlineShape.isEmpty())
blockOffset += upperOutlineShape.max(Direction.Axis.Y);


int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
GL11.glVertex3d(x + .01, y + blockOffset, z + .01);
GL11.glVertex3d(x - .01 + 1, y + blockOffset, z - .01 + 1);
GL11.glVertex3d(x - .01 + 1, y + blockOffset, z + .01);
GL11.glVertex3d(x + .01, y + blockOffset, z - .01 + 1);
}

@Override
public void close() {
if (glListIndex != 0) {
GL11.glDeleteLists(glListIndex, 3);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static Screen getConfigScreenByCloth(Screen parent) {
general.addEntry(eb.startBooleanToggle(new TranslatableComponent("config.lightoverlay.smoothLines"), LightOverlay.smoothLines).setDefaultValue(true).setSaveConsumer(bool -> LightOverlay.smoothLines = bool).build());
general.addEntry(eb.startBooleanToggle(new TranslatableComponent("config.lightoverlay.underwater"), LightOverlay.underwater).setDefaultValue(false).setSaveConsumer(bool -> LightOverlay.underwater = bool).build());
general.addEntry(eb.startBooleanToggle(new TranslatableComponent("config.lightoverlay.mushroom"), LightOverlay.mushroom).setDefaultValue(false).setSaveConsumer(bool -> LightOverlay.mushroom = bool).build());
general.addEntry(eb.startBooleanToggle(new TranslatableComponent("config.lightoverlay.useListWhileCaching"), LightOverlay.useListWhileCaching).setDefaultValue(true).setSaveConsumer(bool -> LightOverlay.useListWhileCaching = bool).build());
general.addEntry(eb.startIntSlider(new TranslatableComponent("config.lightoverlay.lineWidth"), Mth.floor(LightOverlay.lineWidth * 100), 100, 700).setDefaultValue(100).setTextGetter(integer -> new TextComponent("Light Width: " + LightOverlay.FORMAT.format(integer / 100d))).setSaveConsumer(integer -> LightOverlay.lineWidth = integer / 100f).build());
general.addEntry(eb.startColorField(new TranslatableComponent("config.lightoverlay.yellowColor"), LightOverlay.yellowColor).setDefaultValue(0xFFFF00).setSaveConsumer(color -> LightOverlay.yellowColor = color).build());
general.addEntry(eb.startColorField(new TranslatableComponent("config.lightoverlay.redColor"), LightOverlay.redColor).setDefaultValue(0xFF0000).setSaveConsumer(color -> LightOverlay.redColor = color).build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package me.shedaniel.lightoverlay.common;

import net.minecraft.core.BlockPos;

import java.util.Objects;

public class CubicChunkPos {
public final int x;
public final int y;
public final int z;

public CubicChunkPos(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

public CubicChunkPos(long l) {
this.x = getX(l);
this.y = getY(l);
this.z = getZ(l);
}

public CubicChunkPos(BlockPos blockPos) {
this.x = blockPos.getX() >> 4;
this.y = blockPos.getY() >> 4;
this.z = blockPos.getZ() >> 4;
}

public long toLong() {
return asLong(this.x, this.y, this.z);
}

// Allocate 24 bits to x, 12 bits to y, 24 bits to z
public static long asLong(int x, int y, int z) {
return ((x & 0xffffffL) << 36) | ((y & 0xfffL) << 24) | (z & 0xffffffL);
}

public static int getX(long l) {
return (int) (l >> 36 & 0xffffffL);
}

public static int getY(long l) {
return (int) (l >> 24 & 0xfffL);
}

public static int getZ(long l) {
return (int) (l & 0xffffffL);
}

public int getMinBlockX() {
return this.x << 4;
}

public int getMinBlockY() {
return this.y << 4;
}

public int getMinBlockZ() {
return this.z << 4;
}

public int getMaxBlockX() {
return (this.x << 4) + 15;
}

public int getMaxBlockY() {
return (this.y << 4) + 15;
}

public int getMaxBlockZ() {
return (this.z << 4) + 15;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CubicChunkPos that = (CubicChunkPos) o;
return x == that.x && y == that.y && z == that.z;
}

@Override
public int hashCode() {
return Objects.hash(x, y, z);
}
}
Loading

0 comments on commit 79e1db0

Please sign in to comment.