]> git.lizzy.rs Git - LightOverlay.git/blob - common/src/main/java/me/shedaniel/lightoverlay/common/ChunkData.java
2.8.0 Cubic Chunk calculation, using glList during caching
[LightOverlay.git] / common / src / main / java / me / shedaniel / lightoverlay / common / ChunkData.java
1 package me.shedaniel.lightoverlay.common;
2
3 import it.unimi.dsi.fastutil.longs.Long2ByteMap;
4 import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
5 import net.minecraft.core.BlockPos;
6 import net.minecraft.core.Direction;
7 import net.minecraft.world.level.Level;
8 import net.minecraft.world.phys.shapes.CollisionContext;
9 import net.minecraft.world.phys.shapes.VoxelShape;
10 import org.lwjgl.opengl.GL11;
11
12 import java.io.Closeable;
13
14 import static me.shedaniel.lightoverlay.common.LightOverlay.*;
15
16 public class ChunkData implements Closeable {
17     private static final IllegalStateException WRONG_TYPE = new IllegalStateException("Wrong type accessed!");
18     private Long2ByteMap data;
19     private int glListIndex = 0;
20     private boolean generatedList = false;
21     
22     public ChunkData() {
23         this(new Long2ByteOpenHashMap());
24     }
25     
26     public ChunkData(Long2ByteMap data) {
27         this.data = data;
28     }
29     
30     public Long2ByteMap data() {
31         return data;
32     }
33     
34     private void compileList(Level level, CollisionContext collisionContext) {
35         generatedList = true;
36         
37         if (data().isEmpty()) {
38             glListIndex = 0;
39             return;
40         }
41         
42         glListIndex = GL11.glGenLists(3);
43         GL11.glNewList(glListIndex, GL11.GL_COMPILE);
44         GL11.glBegin(GL11.GL_LINES);
45         color(redColor);
46         
47         BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
48         for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
49             byte crossType = objectEntry.getByteValue();
50             mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
51             if (crossType == CROSS_RED) {
52                 renderCross(level, mutable, collisionContext);
53             }
54         }
55         
56         GL11.glEnd();
57         GL11.glEndList();
58         
59         GL11.glNewList(glListIndex + 1, GL11.GL_COMPILE);
60         GL11.glBegin(GL11.GL_LINES);
61         color(yellowColor);
62         
63         for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
64             byte crossType = objectEntry.getByteValue();
65             mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
66             if (crossType == CROSS_YELLOW) {
67                 renderCross(level, mutable, collisionContext);
68             }
69         }
70         
71         GL11.glEnd();
72         GL11.glEndList();
73         
74         GL11.glNewList(glListIndex + 2, GL11.GL_COMPILE);
75         GL11.glBegin(GL11.GL_LINES);
76         color(secondaryColor);
77         
78         for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
79             byte crossType = objectEntry.getByteValue();
80             mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
81             if (crossType == CROSS_SECONDARY) {
82                 renderCross(level, mutable, collisionContext);
83             }
84         }
85         
86         GL11.glEnd();
87         GL11.glEndList();
88     }
89     
90     public void renderList(Level level, CollisionContext collisionContext) {
91         if (!generatedList) {
92             compileList(level, collisionContext);
93         }
94         
95         if (glListIndex != 0) {
96             GL11.glCallList(glListIndex);
97             GL11.glCallList(glListIndex + 1);
98             GL11.glCallList(glListIndex + 2);
99         }
100     }
101     
102     private static void color(int color) {
103         int red = (color >> 16) & 255;
104         int green = (color >> 8) & 255;
105         int blue = color & 255;
106         GL11.glColor4f(red / 255f, green / 255f, blue / 255f, 1f);
107     }
108     
109     public static void renderCross(Level level, BlockPos pos, CollisionContext collisionContext) {
110         double blockOffset = 0;
111         VoxelShape upperOutlineShape = level.getBlockState(pos).getShape(level, pos, collisionContext);
112         if (!upperOutlineShape.isEmpty())
113             blockOffset += upperOutlineShape.max(Direction.Axis.Y);
114         
115         
116         int x = pos.getX();
117         int y = pos.getY();
118         int z = pos.getZ();
119         GL11.glVertex3d(x + .01, y + blockOffset, z + .01);
120         GL11.glVertex3d(x - .01 + 1, y + blockOffset, z - .01 + 1);
121         GL11.glVertex3d(x - .01 + 1, y + blockOffset, z + .01);
122         GL11.glVertex3d(x + .01, y + blockOffset, z - .01 + 1);
123     }
124     
125     @Override
126     public void close() {
127         if (glListIndex != 0) {
128             GL11.glDeleteLists(glListIndex, 3);
129         }
130     }
131 }