Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final float BLOCK_WIDTH = 1f;
- private static final float BLOCK_HEIGHT = 1f;
- private static final float BLOCK_LENGTH = -1f;
- private static final int TEXTURES_SPLIT = 256/16;
- private static final int LENGTH = 16;
- private Vector3f pos;
- public Chunk(float xPos, float yPos, float zPos) {
- super(createChunk());
- setPos(new Vector3f((xPos*LENGTH), (yPos*LENGTH)-BLOCK_HEIGHT, (zPos*LENGTH)));
- }
- private static Mesh createChunk() {
- List<Vertex> verticesArray = new ArrayList<Vertex>();
- List<Integer> indicesArray = new ArrayList<Integer>();
- List<Vector2f> textureArray = new ArrayList<Vector2f>();
- List<Vector3f> normalsArray = new ArrayList<Vector3f>();
- for (int x = 0; x < LENGTH; x++) {
- for (int y = 0; y < LENGTH; y++) {
- for (int z = 0; z < LENGTH; z++) {
- createCube(verticesArray, indicesArray, textureArray, normalsArray, x,y,z);
- }
- }
- }
- float[] vertices = new float[verticesArray.size() * 3];
- int[] indices = new int[indicesArray.size()];
- float[] textures = new float[textureArray.size() * 2];
- float[] normals = new float[normalsArray.size() * 3];
- int pointer = 0;
- for (int i = 0; i < verticesArray.size(); i++) {
- Vertex v = verticesArray.get(i);
- vertices[pointer++] = v.getX();
- vertices[pointer++] = v.getY();
- vertices[pointer++] = v.getZ();
- }
- for (int i = 0; i < indices.length; i++) {
- indices[i] = indicesArray.get(i);
- }
- pointer = 0;
- for (int i = 0; i < textureArray.size(); i++) {
- Vector2f texCoord = textureArray.get(i);
- textures[pointer++] = texCoord.getX();
- textures[pointer++] = texCoord.getY();
- }
- pointer = 0;
- for (int i = 0; i < normalsArray.size(); i++) {
- Vector3f n = normalsArray.get(i);
- normals[pointer++] = n.getX();
- normals[pointer++] = n.getY();
- normals[pointer++] = n.getZ();
- }
- return new Mesh(vertices, indices, textures, normals);
- }
- private static void createCube(List<Vertex> verticesArray, List<Integer> indicesArray, List<Vector2f> textureArray, List<Vector3f> normalsArray, float xPos, float yPos, float zPos) {
- // Face
- indicesArray.add(verticesArray.size() + 0);
- indicesArray.add(verticesArray.size() + 1);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 3);
- indicesArray.add(verticesArray.size() + 0);
- textureArray.add(new Vector2f(0, 0));
- textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
- normalsArray.add(new Vector3f(0, 0, 1));
- normalsArray.add(new Vector3f(0, 0, 1));
- normalsArray.add(new Vector3f(0, 0, 1));
- normalsArray.add(new Vector3f(0, 0, 1));
- verticesArray.add(new Vertex(xPos, yPos, zPos));
- verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
- // Back
- indicesArray.add(verticesArray.size() + 0);
- indicesArray.add(verticesArray.size() + 3);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 1);
- indicesArray.add(verticesArray.size() + 0);
- textureArray.add(new Vector2f(0, 0));
- textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
- normalsArray.add(new Vector3f(0, 0, -1));
- normalsArray.add(new Vector3f(0, 0, -1));
- normalsArray.add(new Vector3f(0, 0, -1));
- normalsArray.add(new Vector3f(0, 0, -1));
- verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
- // Left
- indicesArray.add(verticesArray.size() + 0);
- indicesArray.add(verticesArray.size() + 1);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 3);
- indicesArray.add(verticesArray.size() + 0);
- textureArray.add(new Vector2f(0, 0));
- textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
- normalsArray.add(new Vector3f(-1, 0, 0));
- normalsArray.add(new Vector3f(-1, 0, 0));
- normalsArray.add(new Vector3f(-1, 0, 0));
- normalsArray.add(new Vector3f(-1, 0, 0));
- verticesArray.add(new Vertex(xPos, yPos, zPos));
- verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
- // Right
- indicesArray.add(verticesArray.size() + 0);
- indicesArray.add(verticesArray.size() + 3);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 1);
- indicesArray.add(verticesArray.size() + 0);
- textureArray.add(new Vector2f(0, 0));
- textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
- normalsArray.add(new Vector3f(1, 0, 0));
- normalsArray.add(new Vector3f(1, 0, 0));
- normalsArray.add(new Vector3f(1, 0, 0));
- normalsArray.add(new Vector3f(1, 0, 0));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
- // Bottom
- indicesArray.add(verticesArray.size() + 0);
- indicesArray.add(verticesArray.size() + 3);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 1);
- indicesArray.add(verticesArray.size() + 0);
- textureArray.add(new Vector2f(0, 0));
- textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
- normalsArray.add(new Vector3f(0, -1, 0));
- normalsArray.add(new Vector3f(0, -1, 0));
- normalsArray.add(new Vector3f(0, -1, 0));
- normalsArray.add(new Vector3f(0, -1, 0));
- verticesArray.add(new Vertex(xPos, yPos, zPos));
- verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
- // Top
- indicesArray.add(verticesArray.size() + 0);
- indicesArray.add(verticesArray.size() + 1);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 2);
- indicesArray.add(verticesArray.size() + 3);
- indicesArray.add(verticesArray.size() + 0);
- textureArray.add(new Vector2f(0, 0));
- textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
- textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
- normalsArray.add(new Vector3f(0, 1, 0));
- normalsArray.add(new Vector3f(0, 1, 0));
- normalsArray.add(new Vector3f(0, 1, 0));
- normalsArray.add(new Vector3f(0, 1, 0));
- verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
- verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
- verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
- }
- public void render(StaticShader shader) {
- shader.start();
- {
- GL30.glBindVertexArray(getMesh().getVaoID());
- {
- GL20.glEnableVertexAttribArray(0);
- GL20.glEnableVertexAttribArray(1);
- GL20.glEnableVertexAttribArray(2);
- {
- shader.loadTransformationMatrix(Utils.createTransformationMatrix3D(pos, new Vector3f(), new Vector3f(1, 1, 1)));
- GL11.glDrawElements(GL11.GL_TRIANGLES, getMesh().getCount(), GL11.GL_UNSIGNED_INT, 0);
- }
- GL20.glDisableVertexAttribArray(2);
- GL20.glDisableVertexAttribArray(1);
- GL20.glDisableVertexAttribArray(0);
- }
- GL30.glBindVertexArray(0);
- }
- shader.stop();
- }
- public Vector3f getPos() {
- return pos;
- }
- public void setPos(Vector3f pos) {
- this.pos = pos;
- }
Advertisement
Add Comment
Please, Sign In to add comment