forked from shedaniel/LightOverlay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.8.0 Cubic Chunk calculation, using glList during caching
Signed-off-by: shedaniel <daniel@shedaniel.me>
- Loading branch information
Showing
10 changed files
with
405 additions
and
124 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
common/src/main/java/me/shedaniel/lightoverlay/common/ChunkData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
common/src/main/java/me/shedaniel/lightoverlay/common/CubicChunkPos.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.