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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
package makamys.lodmod.renderer;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.List;
import org.lwjgl.BufferUtils;
import makamys.lodmod.LODMod;
import net.minecraft.block.Block;
import net.minecraft.block.BlockGrass;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.IIcon;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.Chunk;
public class SimpleChunkMesh extends Mesh {
private FloatBuffer vertices;
public static int usedRAM;
public static int instances;
public static List<SimpleChunkMesh> generateSimpleMeshes(Chunk target){
int divisions = 4;
SimpleChunkMesh pass1 = new SimpleChunkMesh(target.xPosition, target.zPosition, divisions * divisions * 25, 0);
SimpleChunkMesh pass2 = new SimpleChunkMesh(target.xPosition, target.zPosition, divisions * divisions * 25, 1);
for(int divX = 0; divX < divisions; divX++) {
for(int divZ = 0; divZ < divisions; divZ++) {
IIcon icon = null;
int color = 0xFFFFFFFF;
int size = 16 / divisions;
int y = 255;
boolean foundWater = false;
int xOff = divX * size;
int zOff = divZ * size;
BiomeGenBase biome = target.getBiomeGenForWorldCoords(xOff, zOff, target.worldObj.getWorldChunkManager());
for(y = 255; y > 0; y--) {
Block block = target.getBlock(xOff, y, zOff);
int worldX = target.xPosition * 16 + divX * size;
int worldY = y;
int worldZ = target.zPosition * 16 + divZ * size;
if(!foundWater && block.getMaterial() == Material.water) {
foundWater = true;
int meta = target.getBlockMetadata(xOff, y, zOff);
IIcon waterIcon = block.getIcon(1, meta);
int waterColor = biome.getWaterColorMultiplier();
waterColor |= 0xFF000000;
pass2.addFaceYPos(worldX, worldY, worldZ, size, size, waterIcon, waterColor);
}
if(block.isBlockNormalCube() && block.isOpaqueCube() && block.renderAsNormalBlock()) {
int meta = target.getBlockMetadata(xOff, y, zOff);
icon = block.getIcon(1, meta);
if(block instanceof BlockGrass) {
color = biome.getBiomeGrassColor(worldX, y, worldZ);
} else if(block instanceof BlockLeaves) {
color = biome.getBiomeFoliageColor(worldX, y, worldZ);
} else {
color = block.colorMultiplier(target.worldObj, worldX, y, worldZ);
}
color = (0xFF << 24) | ((color >> 16 & 0xFF) << 0) | ((color >> 8 & 0xFF) << 8) | ((color >> 0 & 0xFF) << 16);
if(biome.getFloatTemperature(worldX, y, worldZ) < 0.15f) {
pass1.addCube(worldX, worldY + 0.2f, worldZ, size, size, 1f, Blocks.snow_layer.getIcon(1, 0), Blocks.snow_layer.colorMultiplier(target.worldObj, worldX, y, worldZ));
pass1.addCube(worldX, worldY - 0.8f, worldZ, size, size, worldY - 0.8f, icon, color);
} else {
pass1.addCube(worldX, worldY, worldZ, size, size, worldY, icon, color);
}
break;
}
}
}
}
pass1.finish();
pass2.finish();
return Arrays.asList(new SimpleChunkMesh[] {pass1.quadCount != 0 ? pass1 : null, pass2.quadCount != 0 ? pass2 : null});
}
public SimpleChunkMesh(int x, int z, int maxQuads, int pass) {
this.x = x;
this.y = 64;
this.z = z;
this.pass = pass;
buffer = BufferUtils.createByteBuffer(4 * 6 * 7 * maxQuads);
vertices = buffer.asFloatBuffer();
}
public void finish() {
vertices.flip();
buffer.limit(vertices.limit() * 4);
// may want to shrink the buffers to actual size to not waste memory
usedRAM += buffer.limit();
instances++;
}
private void addCube(float x, float y, float z, float sizeX, float sizeZ, float sizeY, IIcon icon, int color) {
addFaceYPos(x, y, z, sizeX, sizeZ, icon, color);
addFace(
x + 0, y - sizeY, z + 0,
x + 0, y + 0, z + 0,
x + sizeX, y + 0, z + 0,
x + sizeX, y - sizeY, z + 0,
icon, color, 200
);
addFace(
x + sizeX, y - sizeY, z + sizeZ,
x + sizeX, y + 0, z + sizeZ,
x + 0, y + 0, z + sizeZ,
x + 0, y - sizeY, z + sizeZ,
icon, color, 200
);
addFace(
x + sizeX, y - sizeY, z + 0,
x + sizeX, y + 0, z + 0,
x + sizeX, y + 0, z + sizeZ,
x + sizeX, y - sizeY, z + sizeZ,
icon, color, 160
);
addFace(
x + 0, y - sizeY, z + sizeZ,
x + 0, y + 0, z + sizeZ,
x + 0, y + 0, z + 0,
x + 0, y - sizeY, z + 0,
icon, color, 160
);
}
private void addFaceYPos(float x, float y, float z, float sizeX, float sizeZ, IIcon icon, int color) {
addFace(
x + 0, y + 0, z + 0,
x + 0, y + 0, z + sizeZ,
x + sizeX, y + 0, z + sizeZ,
x + sizeX, y + 0, z + 0,
icon, color, 240
);
}
private void addFace(float p1x, float p1y, float p1z,
float p2x, float p2y, float p2z,
float p3x, float p3y, float p3z,
float p4x, float p4y, float p4z,
IIcon icon, int color, int brightness) {
int off = vertices.position() * 4;
vertices.put(new float[] {
p1x, p1y, p1z, icon.getMinU(), icon.getMaxV(), 0, 0,
p2x, p2y, p2z, icon.getMinU(), icon.getMinV(), 0, 0,
p4x, p4y, p4z, icon.getMaxU(), icon.getMaxV(), 0, 0,
p2x, p2y, p2z, icon.getMinU(), icon.getMinV(), 0, 0,
p3x, p3y, p3z, icon.getMaxU(), icon.getMinV(), 0, 0,
p4x, p4y, p4z, icon.getMaxU(), icon.getMaxV(), 0, 0
});
buffer.putInt(off + 0 * getStride() + 6 * 4, color);
buffer.putShort(off + 0 * getStride() + 5 * 4 + 2, (short)brightness);
buffer.putInt(off + 1 * getStride() + 6 * 4, color);
buffer.putShort(off + 1 * getStride() + 5 * 4 + 2, (short)brightness);
buffer.putInt(off + 2 * getStride() + 6 * 4, color);
buffer.putShort(off + 2 * getStride() + 5 * 4 + 2, (short)brightness);
buffer.putInt(off + 3 * getStride() + 6 * 4, color);
buffer.putShort(off + 3 * getStride() + 5 * 4 + 2, (short)brightness);
buffer.putInt(off + 4 * getStride() + 6 * 4, color);
buffer.putShort(off + 4 * getStride() + 5 * 4 + 2, (short)brightness);
buffer.putInt(off + 5 * getStride() + 6 * 4, color);
buffer.putShort(off + 5 * getStride() + 5 * 4 + 2, (short)brightness);
quadCount++;
}
public int getStride() {
return (3 * 4 + 8 + 4 + 4);
}
public void destroy() {
usedRAM -= buffer.limit();
instances--;
}
}
|