public void Cull() { vertex_count = 0; for (int x = 0; x < MineGame.chunk_size; x++) { for (int y = 0; y < MineGame.chunk_size; y++) { for (int z = 0; z < MineGame.chunk_size; z++) { Block cur = blocks[x, y, z]; if (!cur.active) { continue; } cur.render_faces[Block.YPositive] = Convert.ToInt16(y == MineGame.chunk_size - 1 || !blocks[x, y + 1, z].active); cur.render_faces[Block.XNegative] = Convert.ToInt16(x == 0 || !blocks[x - 1, y, z].active); cur.render_faces[Block.XPositive] = Convert.ToInt16(x == MineGame.chunk_size - 1 || !blocks[x + 1, y, z].active); cur.render_faces[Block.YNegative] = Convert.ToInt16((y == 0 || !blocks[x, y - 1, z].active) && chunk_y != 0); cur.render_faces[Block.ZNegative] = Convert.ToInt16(z == 0 || !blocks[x, y, z - 1].active); cur.render_faces[Block.ZPositive] = Convert.ToInt16(z == MineGame.chunk_size - 1 || !blocks[x, y, z + 1].active); vertex_count += 6 * cur.renderedVerticeCount(); } } } }
public void UpdateBuffer() { if (vertex_count != 0) { block_vertices = new VertexPositionColorTexture[vertex_count]; block_vertex_index = 0; } for (int x = 0; x < MineGame.chunk_size; x++) { for (int y = 0; y < MineGame.chunk_size; y++) { for (int z = 0; z < MineGame.chunk_size; z++) { Block cur = blocks[x, y, z]; if (cur.renderedVerticeCount() == 0) { continue; } Vector3 topLeftFront = cur.shapePosition + new Vector3(-0.5f, 0.5f, -0.5f); Vector3 bottomLeftFront = cur.shapePosition + new Vector3(-0.5f, -0.5f, -0.5f); Vector3 topRightFront = cur.shapePosition + new Vector3(0.5f, 0.5f, -0.5f); Vector3 bottomRightFront = cur.shapePosition + new Vector3(0.5f, -0.5f, -0.5f); Vector3 topLeftBack = cur.shapePosition + new Vector3(-0.5f, 0.5f, 0.5f); Vector3 topRightBack = cur.shapePosition + new Vector3(0.5f, 0.5f, 0.5f); Vector3 bottomLeftBack = cur.shapePosition + new Vector3(-0.5f, -0.5f, 0.5f); Vector3 bottomRightBack = cur.shapePosition + new Vector3(0.5f, -0.5f, 0.5f); for (int i = 0; i < 6; i++) { if (cur.render_faces[i] == 1) //render it { var color = Color.White; if (cur.type == BlockType.Grass && i == Block.YPositive) { color = Color.FromNonPremultiplied(107, 168, 64, 255); } Vector2 textureTopLeft = game.texture_coordinates[cur.type][i, Block.textureTopLeft]; Vector2 textureBottomLeft = game.texture_coordinates[cur.type][i, Block.textureBottomLeft]; Vector2 textureTopRight = game.texture_coordinates[cur.type][i, Block.textureTopRight]; Vector2 textureBottomRight = game.texture_coordinates[cur.type][i, Block.textureBottomRight]; int offset = block_vertex_index; switch (i) { case Block.ZNegative: //front block_vertices[offset + 0] = new VertexPositionColorTexture(topLeftFront, color, textureTopLeft); block_vertices[offset + 1] = new VertexPositionColorTexture(bottomLeftFront, color, textureBottomLeft); block_vertices[offset + 2] = new VertexPositionColorTexture(topRightFront, color, textureTopRight); block_vertices[offset + 3] = new VertexPositionColorTexture(bottomLeftFront, color, textureBottomLeft); block_vertices[offset + 4] = new VertexPositionColorTexture(bottomRightFront, color, textureBottomRight); block_vertices[offset + 5] = new VertexPositionColorTexture(topRightFront, color, textureTopRight); break; case Block.ZPositive: //back block_vertices[offset + 0] = new VertexPositionColorTexture(topLeftBack, color, textureTopRight); block_vertices[offset + 1] = new VertexPositionColorTexture(topRightBack, color, textureTopLeft); block_vertices[offset + 2] = new VertexPositionColorTexture(bottomLeftBack, color, textureBottomRight); block_vertices[offset + 3] = new VertexPositionColorTexture(bottomLeftBack, color, textureBottomRight); block_vertices[offset + 4] = new VertexPositionColorTexture(topRightBack, color, textureTopLeft); block_vertices[offset + 5] = new VertexPositionColorTexture(bottomRightBack, color, textureBottomLeft); break; case Block.YPositive: //top block_vertices[offset + 0] = new VertexPositionColorTexture(topLeftFront, color, textureBottomLeft); block_vertices[offset + 1] = new VertexPositionColorTexture(topRightBack, color, textureTopRight); block_vertices[offset + 2] = new VertexPositionColorTexture(topLeftBack, color, textureTopLeft); block_vertices[offset + 3] = new VertexPositionColorTexture(topLeftFront, color, textureBottomLeft); block_vertices[offset + 4] = new VertexPositionColorTexture(topRightFront, color, textureBottomRight); block_vertices[offset + 5] = new VertexPositionColorTexture(topRightBack, color, textureTopRight); break; case Block.YNegative: //Bottom block_vertices[offset + 0] = new VertexPositionColorTexture(bottomLeftFront, color, textureTopLeft); block_vertices[offset + 1] = new VertexPositionColorTexture(bottomLeftBack, color, textureBottomLeft); block_vertices[offset + 2] = new VertexPositionColorTexture(bottomRightBack, color, textureBottomRight); block_vertices[offset + 3] = new VertexPositionColorTexture(bottomLeftFront, color, textureTopLeft); block_vertices[offset + 4] = new VertexPositionColorTexture(bottomRightBack, color, textureBottomRight); block_vertices[offset + 5] = new VertexPositionColorTexture(bottomRightFront, color, textureTopRight); break; case Block.XPositive: //Right block_vertices[offset + 0] = new VertexPositionColorTexture(topRightFront, color, textureTopLeft); block_vertices[offset + 1] = new VertexPositionColorTexture(bottomRightFront, color, textureBottomLeft); block_vertices[offset + 2] = new VertexPositionColorTexture(bottomRightBack, color, textureBottomRight); block_vertices[offset + 3] = new VertexPositionColorTexture(topRightBack, color, textureTopRight); block_vertices[offset + 4] = new VertexPositionColorTexture(topRightFront, color, textureTopLeft); block_vertices[offset + 5] = new VertexPositionColorTexture(bottomRightBack, color, textureBottomRight); break; case Block.XNegative: //Left block_vertices[offset + 0] = new VertexPositionColorTexture(topLeftFront, color, textureTopRight); block_vertices[offset + 1] = new VertexPositionColorTexture(bottomLeftBack, color, textureBottomLeft); block_vertices[offset + 2] = new VertexPositionColorTexture(bottomLeftFront, color, textureBottomRight); block_vertices[offset + 3] = new VertexPositionColorTexture(topLeftBack, color, textureTopLeft); block_vertices[offset + 4] = new VertexPositionColorTexture(bottomLeftBack, color, textureBottomLeft); block_vertices[offset + 5] = new VertexPositionColorTexture(topLeftFront, color, textureTopRight); break; } block_vertex_index += 6; } } } } } if (vertex_buffer != null) { block_vertices = null; } }