aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/renderer/SimpleChunkMesh.java
blob: 9cdbea80d85fe2c58258a2710223cb6155156ccf (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
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
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.List;

import org.lwjgl.BufferUtils;

import makamys.lodmod.LODMod;
import net.minecraft.block.Block;
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.nbt.NBTTagString;
import net.minecraft.util.IIcon;
import net.minecraft.world.chunk.Chunk;
import scala.actors.threadpool.Arrays;

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(divisions * divisions * 25);
		SimpleChunkMesh pass2 = new SimpleChunkMesh(divisions * divisions * 25);
		
		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;
				for(y = 255; y > 0; y--) {
					int xOff = divX * size;
					int zOff = divZ * size;
					Block block = target.getBlock(xOff, y, zOff);
					
					float offX = target.xPosition * 16 + divX * size;
					float offY = y;
					float offZ = 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 = block.colorMultiplier(Minecraft.getMinecraft().theWorld, target.xPosition * 16 + xOff, y, target.zPosition * 16 + zOff);
						waterColor |= 0xFF000000;
						pass2.addFaceYPos(offX, offY, offZ, size, size, waterIcon, waterColor);
					}
					
					if(block.isBlockNormalCube() && block.isOpaqueCube() && block.renderAsNormalBlock()) {
						int meta = target.getBlockMetadata(xOff, y, zOff);
						icon = block.getIcon(1, meta);
						color = block.colorMultiplier(Minecraft.getMinecraft().theWorld, target.xPosition * 16 + xOff, y, target.zPosition * 16 + zOff);
						color = (0xFF << 24) | ((color >> 16 & 0xFF) << 0) | ((color >> 8 & 0xFF) << 8) | ((color >> 0 & 0xFF) << 16);
						
						pass1.addCube(offX, offY, offZ, size, size, offY, 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 maxQuads) {
	    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, 180
		);
		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, 180
		);
		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, 120
			);
		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, 120
			);
	}
	
	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);
	}
	
}