/
ChunkRenderer.java
executable file
·82 lines (70 loc) · 2.13 KB
/
ChunkRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package de.funky_clan.chunks;
import com.google.inject.Inject;
import de.funky_clan.coregl.renderer.BufferedRenderer;
import de.funky_clan.octree.data.OctreeNode;
import org.lwjgl.opengl.GL11;
import java.nio.ByteBuffer;
/**
* @author synopia
*/
public abstract class ChunkRenderer {
protected static int[][] NEIGHBORS = new int[][]{
{0,0,1}, {0,0,-1}, {0,1,0}, {0,-1,0}, {1,0,0}, {-1,0,0}
};
@Inject
protected BufferedRenderer renderer;
protected int glListId;
protected boolean dirty;
protected Chunk chunk;
protected ByteBuffer map;
@Inject
protected ChunkStorage storage;
@Inject
public ChunkRenderer() {
glListId = GL11.glGenLists(1);
dirty = true;
}
public void update() {
boolean totallyEmpty = true;
map = chunk.getMap();
if( map !=null ) {
GL11.glNewList(glListId, GL11.GL_COMPILE);
renderer.begin();
for( int x = 0; x < OctreeNode.CHUNK_SIZE; x++ ) {
for( int y = 0; y < OctreeNode.CHUNK_SIZE; y++ ) {
for( int z = 0; z < OctreeNode.CHUNK_SIZE; z++ ) {
boolean rendered = renderBlock(x, y, z);
if( rendered ) {
totallyEmpty = false;
}
}
}
}
renderer.render();
GL11.glEndList();
}
dirty = false;
chunk.setDirty(false);
chunk.setVisible(!totallyEmpty);
}
protected abstract boolean renderBlock( int x, int y, int z );
public boolean needsUpdate() {
return chunk.isVisible() && (chunk.isDirty() || dirty);
}
public void render() {
if( !chunk.isVisible() ) {
return;
}
GL11.glCallList(glListId);
}
public void setChunk(Chunk chunk) {
dirty = true;
this.chunk = chunk;
if( chunk==null ) {
this.map = null;
}
}
public Chunk getChunk() {
return chunk;
}
}