Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Chunk Class

By: a guest on Jan 9th, 2014  |  syntax: Java  |  size: 6.35 KB  |  views: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.voxel.engine.core.Chunk;
  2.  
  3. import com.voxel.engine.core.Block.Block;
  4. import org.lwjgl.BufferUtils;
  5. import org.lwjgl.opengl.GL11;
  6. import org.lwjgl.opengl.GL15;
  7.  
  8. import java.nio.FloatBuffer;
  9. import java.util.Random;
  10.  
  11. /**
  12.  * Created with IntelliJ IDEA.
  13.  * User: Toby's PC
  14.  * Date: 07/01/14
  15.  * Time: 19:25
  16.  */
  17. public class Chunk {
  18.  
  19.     public static final int CHUNK_SIZE = 16;
  20.     public static final int CHUNK_DEPTH = 2;
  21.     public Block[][][] blocks;
  22.  
  23.     public static final int CUBE_LENGTH = 2;
  24.  
  25.     private int StartX, StartY, StartZ;
  26.  
  27.     private Random random;
  28.  
  29.     int xx, yy, zz;
  30.  
  31.     private int VBOColorHandle;
  32.     private int VBOVertexHandle;
  33.  
  34.     public void render(){
  35.         GL11.glPushMatrix();
  36.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
  37.         GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
  38.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
  39.         GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0L);
  40.         GL11.glDrawArrays(GL11.GL_QUADS, 0, CHUNK_SIZE * CHUNK_SIZE  * CHUNK_SIZE * 24);
  41.  
  42.         GL11.glPopMatrix();
  43.     }
  44.  
  45.     public void update(){
  46.  
  47.     }
  48.  
  49.     public static void EnableLighting(boolean enabled){
  50.         GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
  51.         GL11.glEnable(GL11.GL_COLOR_MATERIAL);
  52.         GL11.glEnable(GL11.GL_LIGHTING);
  53.         GL11.glEnable(GL11.GL_LIGHT0);
  54.  
  55.     }
  56.  
  57.     public Chunk(int startX, int startY, int startZ){
  58.         random = new Random();
  59.  
  60.         int l = 10;
  61.         int h = 30;
  62.  
  63.         for(int i = 0;i < 10;i++){
  64.             xx = random.nextInt(h-l)+l;
  65.         }
  66. //       xx = random.nextInt(h-l)+l;
  67.         zz = random.nextInt(h-l)+l;
  68.  
  69.         blocks = new Block[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
  70.         for (int x = 0; x < CHUNK_SIZE; x++) {
  71.             for (int y = 0; y < CHUNK_DEPTH; y++) {
  72.                 for (int z = 0; z < CHUNK_SIZE; z++) {
  73.                     if(random.nextFloat()>0.7f){
  74.                         blocks[x][y][z] = new Block(Block.BlockType.BLOCK_TYPE_GRASS);
  75.                     }else if(random.nextFloat() > 0.4f){
  76.                         blocks[x][y][z] = new Block(Block.BlockType.BLOCK_TYPE_DIRT);
  77.                     }else if(random.nextFloat()>0.2f){
  78.                         blocks[x][y][z] = new Block(Block.BlockType.BLOCK_TYPE_WATER);
  79.                     }else{
  80.                         blocks[x][y][z] = new Block(Block.BlockType.BLOCK_TYPE_DEFAULT);
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         VBOColorHandle = GL15.glGenBuffers();
  86.         VBOVertexHandle = GL15.glGenBuffers();
  87. //        StartX = startX;
  88.  //       StartY = startY;
  89.   //      StartZ = startZ;
  90.         rebuildMesh(startX, startY, startZ);
  91.     }
  92.  
  93.     public void rebuildMesh(float startX, float startY, float startZ){
  94.         VBOColorHandle = GL15.glGenBuffers();
  95.         VBOVertexHandle = GL15.glGenBuffers();
  96.  
  97.         FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 6 * 12);
  98.         FloatBuffer VertexColorData = BufferUtils.createFloatBuffer((CHUNK_SIZE* CHUNK_SIZE * CHUNK_SIZE) * 6 * 12);
  99.         //Note that for above CHUNK_SIZE^3 was not working for me.
  100.  
  101.         for (float x = 0; x < CHUNK_SIZE; x += 1) {
  102.             for (float y = 0; y < CHUNK_DEPTH; y += 1) {
  103.                 for (float z = 0; z < CHUNK_SIZE; z += 1) {
  104.                     VertexPositionData.put(CreateCube((float) startX + x * CUBE_LENGTH, (float) startY + y * CUBE_LENGTH,(float) startZ + z * CUBE_LENGTH));
  105.                     VertexColorData.put(CreateCubeVertexCol(GetCubeColor(blocks[(int)x][(int) y][(int) z])));
  106.  
  107.  
  108.  
  109.                     VertexPositionData.put(CreateCube((float) startX, (float)xx,(float) zz));
  110.                     VertexColorData.put(CreateCubeVertexCol(GetCubeColor(blocks[(int)x][(int) y][(int) z])));
  111.                 }
  112.             }
  113.  
  114.         }
  115.  
  116.         VertexColorData.flip();
  117.         VertexPositionData.flip();
  118.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
  119.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexPositionData,GL15.GL_STATIC_DRAW);
  120.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  121.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
  122.         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData,GL15.GL_STATIC_DRAW);
  123.         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
  124.     }
  125.  
  126.     public static float[] CreateCube(float x, float y, float z) {
  127.         int offset = CUBE_LENGTH / 2;
  128.         return new float[] {
  129.  
  130.                 x + offset, y + offset,z,
  131.                 x - offset,y + offset,z,
  132.                 x - offset,y + offset,z - CUBE_LENGTH,
  133.                 x + offset,y + offset,z - CUBE_LENGTH,
  134.  
  135.                 x + offset, y - offset, z - CUBE_LENGTH,
  136.                 x - offset,y - offset,z - CUBE_LENGTH,
  137.                 x - offset,y - offset,z,
  138.                 x + offset,y - offset,z,
  139.  
  140.                 x + offset, y + offset, z - CUBE_LENGTH,
  141.                 x - offset,y + offset, z - CUBE_LENGTH,
  142.                 x - offset,y - offset,z - CUBE_LENGTH,
  143.                 x + offset,y - offset,z - CUBE_LENGTH,
  144.  
  145.                 x + offset, y - offset, z,
  146.                 x - offset, y - offset, z,
  147.                 x - offset, y + offset, z,
  148.                 x + offset,y + offset, z,
  149.  
  150.                 x - offset, y + offset, z - CUBE_LENGTH,
  151.                 x - offset,y + offset, z,
  152.                 x - offset, y - offset, z,
  153.                 x - offset,y - offset,z - CUBE_LENGTH,
  154.  
  155.                 x + offset, y + offset, z,
  156.                 x + offset, y + offset,z - CUBE_LENGTH,
  157.                 x + offset, y - offset, z - CUBE_LENGTH,
  158.                 x + offset, y - offset, z };
  159.  
  160.     }
  161.  
  162.     private float[] GetCubeColor(Block block) {
  163.         switch (block.getID()) {
  164.             case 1:
  165.                 return new float[] { 0, 1, 0 };
  166.             case 2:
  167.                 return new float[] { 1, 0.5f, 0 };
  168.             case 3:
  169.                 return new float[] { 0, 0f, 1f };
  170.         }
  171.         return new float[] { 1, 1, 1 };
  172.     }
  173.  
  174.     private float[] CreateCubeVertexCol(float[] CubeColorArray) {
  175.         float[] cubeColors = new float[CubeColorArray.length * 4 * 6];
  176.         for (int i = 0; i < cubeColors.length; i++) {
  177.             cubeColors[i] = CubeColorArray[i % CubeColorArray.length];
  178.         }
  179.         return cubeColors;
  180.     }
  181. }
clone this paste RAW Paste Data