aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/olacraft/render/jogl/ChunkRenderer.java
blob: e24e8e99d2af7ab80300bb2e433e27b1bab21e03 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package ru.olamedia.olacraft.render.jogl;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLContext;

import ru.olamedia.math.Box;
import ru.olamedia.math.Classifier;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.world.chunk.BlockSlice;
import ru.olamedia.olacraft.world.chunk.Chunk;
import ru.olamedia.olacraft.world.chunk.ChunkMeshBulder;
import ru.olamedia.olacraft.world.chunk.ChunkSlice;

public class ChunkRenderer {
	private BlockSlice slice;

	public ChunkRenderer(BlockSlice slice) {
		this.slice = slice;
	}

	public int visibleTop = 0;
	public int visibleBottom = 0;
	public int visibleLeft = 0;
	public int visibleRight = 0;
	public int visibleFront = 0;
	public int visibleBack = 0;

	public int frustumCulledChunks = 0;
	public int frustumIntersectChunks = 0;

	public boolean renderChunk(Chunk chunk, boolean skipnew) {
		GL gl = GLContext.getCurrentGL();
		if (!chunk.isAvailable()) {
			// System.out.println("not available");
			chunk.request();
			return skipnew;
		}
		/*
		 * if (!chunk.isNeighborsAvailable()) {
		 * System.out.println("no neighbors");
		 * chunk.requestNeighbors();
		 * return;
		 * }
		 */
		// System.out.println("available");
		Box box = new Box(chunk.getX(), chunk.getY(), chunk.getZ(), chunk.getX() + chunk.getWidth(), chunk.getY()
				+ chunk.getHeight(), chunk.getZ() + chunk.getDepth());
		if (Game.instance.camera.frustum.quickClassify(box) == Classifier.OUTSIDE) {
			frustumCulledChunks++;
			return skipnew;
		}

		// // boolean inside = true;
		// if (Game.camera.frustum != null) {
		// if (Game.camera.frustum.quickClassify(box) ==
		// Classifier.Classification.OUTSIDE) {
		// frustumCulledChunks++;
		// return;
		// }
		// }
		// } else if (Game.camera.frustum.test(box) == Classifier.INTERSECT) {
		// inside = false;
		// frustumIntersectChunks++;
		// } else {
		// frustumCulledChunks++;
		// return;
		// }
		if (!chunk.isMeshCostructed) {
			if (skipnew) {
				return skipnew;
			}
		}
		if (!chunk.isMeshCostructed) {
			ChunkMeshBulder.instance.add(chunk);
			if (ChunkMeshBulder.instance.isFull()) {
				skipnew = true;
			}
			return skipnew;
		}
		if (null == chunk.getMesh()) {
		} else {
			chunk.getMesh().joglRender(gl);
		}
		return skipnew;
	}

	public void render(GLAutoDrawable drawable) {

		if (!ChunkMeshBulder.instance.isAlive()) {
			ChunkMeshBulder.instance.start();
		}

		visibleTop = 0;
		visibleBottom = 0;
		visibleLeft = 0;
		visibleRight = 0;
		visibleFront = 0;
		visibleBack = 0;
		frustumCulledChunks = 0;
		boolean skipnew = false;
		ChunkSlice cs = slice.getChunkSlice();
		// rendering from center
		int x, y, z;
		int dw = cs.getWidth() / 2;
		int dd = cs.getDepth() / 2;
		int dh = cs.getHeight() / 2;
		for (int dx = 0; dx < dw; dx++) {
			x = cs.getX() + dw + dx;
			for (int dz = 0; dz < dd; dz++) {
				z = cs.getZ() + dd + dz;
				for (int dy = 0; dy < dh; dy++) {
					y = cs.getY() + dh + dy;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
					y = cs.getY() + dh - dy - 1;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
				}
				z = cs.getZ() + dd - dz - 1;
				for (int dy = 0; dy < dh; dy++) {
					y = cs.getY() + dh + dy;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
					y = cs.getY() + dh - dy - 1;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
				}
			}
			x = cs.getX() + dw - dx - 1;
			for (int dz = 0; dz < dd; dz++) {
				z = cs.getZ() + dd + dz;
				for (int dy = 0; dy < dh; dy++) {
					y = cs.getY() + dh + dy;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
					y = cs.getY() + dh - dy - 1;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
				}
				z = cs.getZ() + dd - dz - 1;
				for (int dy = 0; dy < dh; dy++) {
					y = cs.getY() + dh + dy;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
					y = cs.getY() + dh - dy - 1;
					skipnew = renderChunk(cs.getChunk(x, y, z), skipnew);
				}
			}
		}
		// System.out.println("visible top " + visibleTop);
	}
}