axicer

Chunk.java

Jul 9th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.48 KB | None | 0 0
  1. package fr.axicer.saintscube.game.world;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4. import static org.lwjgl.opengl.GL15.*;
  5. import static org.lwjgl.opengl.GL20.*;
  6.  
  7. import java.nio.FloatBuffer;
  8.  
  9. import org.joml.Matrix4f;
  10. import org.lwjgl.BufferUtils;
  11. import org.lwjgl.opengl.Display;
  12.  
  13. import fr.axicer.saintscube.game.world.block.Block;
  14. import fr.axicer.saintscube.render.Camera;
  15. import fr.axicer.saintscube.render.Texture;
  16. import fr.axicer.saintscube.render.TextureManager;
  17. import fr.axicer.saintscube.render.shader.ChunkShader;
  18. import fr.axicer.saintscube.render.shader.Shader;
  19.  
  20. public class Chunk{
  21.    
  22.     public static final int SIZE = 16;
  23.     public static final int MAX_BUILD_HEIGHT = 256;
  24.    
  25.     private FloatBuffer vertexBuffer;
  26.     private FloatBuffer textureCoordBuffer;
  27.     private FloatBuffer colorBuffer;
  28.     private int vertexBufferId;
  29.     private int textureCoordBufferId;
  30.     private int colorBufferId;
  31.     private int bufferSize;
  32.    
  33.     public boolean loaded;
  34.     public int x,z;
  35.     public Block[][][] blocks;
  36.     public World world;
  37.    
  38.     public Chunk(int x, int z,World world) {
  39.         this.x = x;
  40.         this.z = z;
  41.         this.world = world;
  42.         blocks = new Block[SIZE][MAX_BUILD_HEIGHT][SIZE];
  43.         loaded = false;
  44.     }
  45.    
  46.     public void load(){
  47.         if(loaded){
  48.             reload();
  49.             return;
  50.         }
  51.         vertexBuffer = BufferUtils.createFloatBuffer(SIZE*MAX_BUILD_HEIGHT*SIZE*6*4*3);
  52.         textureCoordBuffer = BufferUtils.createFloatBuffer(SIZE*MAX_BUILD_HEIGHT*SIZE*6*4*2);
  53.         colorBuffer = BufferUtils.createFloatBuffer(SIZE*MAX_BUILD_HEIGHT*SIZE*6*4*4);
  54.         for(int x = 0 ; x < SIZE ; x++){
  55.             for(int y = 0 ; y < MAX_BUILD_HEIGHT ; y++){
  56.                 for(int z = 0 ; z < SIZE ; z++){
  57.                     int xx = this.x * SIZE + x;
  58.                     int yy = y;
  59.                     int zz = this.z * SIZE + z;
  60.                    
  61.                     Block upb = getBlock(x, y+1, z);
  62.                     Block downb = getBlock(x, y-1, z);
  63.                     Block leftb = getBlock(x-1, y, z);
  64.                     Block rightb = getBlock(x+1, y, z);
  65.                     Block frontb = getBlock(x, y, z-1);
  66.                     Block backb = getBlock(x, y, z+1);
  67.                     boolean up = false;
  68.                     boolean down = false;
  69.                     boolean left = false;
  70.                     boolean right = false;
  71.                     boolean front = false;
  72.                     boolean back = false;
  73.                     if(upb != null){
  74.                         up = !upb.type.isTransparent();
  75.                     }
  76.                     if(downb != null){
  77.                         down = !downb.type.isTransparent();
  78.                     }
  79.                     if(leftb != null){
  80.                         left = !leftb.type.isTransparent();
  81.                     }
  82.                     if(rightb != null){
  83.                         right = !rightb.type.isTransparent();
  84.                     }
  85.                     if(frontb != null){
  86.                         front = !frontb.type.isTransparent();
  87.                     }
  88.                     if(backb != null){
  89.                         back = !backb.type.isTransparent();
  90.                     }
  91.                    
  92.                     if(up && down && left && right && front && back)continue;
  93.                     if(blocks[x][y][z] == null)continue;
  94.                    
  95.                     Block block = blocks[x][y][z];
  96.                    
  97.                     //TODO FIX FRONT FACE BAD SHADING
  98.                    
  99.                     int size = 0;
  100.                     float off = 0.7f;
  101.                     if(!up){
  102.                         float[] shading = new float[]{1,1,1,1};
  103.                         if(getBlock(x+1, y+1, z) != null ||
  104.                                 getBlock(x+1, y+1, z-1) != null ||
  105.                                 getBlock(x, y+1, z-1) != null){
  106.                             shading[0] = off;
  107.                         }
  108.                         if(getBlock(x-1, y+1, z) != null ||
  109.                                 getBlock(x-1, y+1, z-1) != null ||
  110.                                 getBlock(x, y+1, z-1) != null){
  111.                             shading[1] = off;
  112.                         }
  113.                         if(getBlock(x-1, y+1, z) != null ||
  114.                                 getBlock(x-1, y+1, z+1) != null ||
  115.                                 getBlock(x, y+1, z+1) != null){
  116.                             shading[2] = off;
  117.                         }
  118.                         if(getBlock(x, y+1, z+1) != null ||
  119.                                 getBlock(x+1, y+1, z+1) != null ||
  120.                                 getBlock(x+1, y+1, z) != null){
  121.                             shading[3] = off;
  122.                         }
  123.                         colorBuffer.put(Block.faceColorData(shading));
  124.                         vertexBuffer.put(block.faceUpVertexData(xx, yy, zz));
  125.                         textureCoordBuffer.put(block.faceUpTextureData(xx, yy, zz));
  126.                         size++;
  127.                     }
  128.                     if(!down){
  129.                         float[] shading = new float[]{1,1,1,1};
  130.                         if(getBlock(x-1, y-1, z) != null ||
  131.                                 getBlock(x-1, y-1, z-1) != null ||
  132.                                 getBlock(x, y-1, z-1) != null){
  133.                             shading[0] = off;                          
  134.                         }
  135.                         if(getBlock(x+1, y-1, z) != null ||
  136.                                 getBlock(x+1, y-1, z-1) != null ||
  137.                                 getBlock(x, y-1, z-1) != null){
  138.                             shading[1] = off;
  139.                         }
  140.                         if(getBlock(x+1, y-1, z) != null ||
  141.                                 getBlock(x+1, y-1, z+1) != null ||
  142.                                 getBlock(x, y-1, z+1) != null){
  143.                             shading[2] = off;
  144.                         }
  145.                         if(getBlock(x-1, y-1, z) != null ||
  146.                                 getBlock(x-1, y-1, z+1) != null ||
  147.                                 getBlock(x, y-1, z+1) != null){
  148.                             shading[3] = off;
  149.                         }
  150.                         colorBuffer.put(Block.faceColorData(shading));
  151.                         vertexBuffer.put(block.faceDownVertexData(xx, yy, zz));
  152.                         textureCoordBuffer.put(block.faceDownTextureData(xx, yy, zz));
  153.                         size++;
  154.                     }
  155.                     if(!left){
  156.                         float[] shading = new float[]{1,1,1,1};
  157.                         if(getBlock(x-1, y+1, z) != null ||
  158.                                 getBlock(x-1, y+1, z-1) != null ||
  159.                                 getBlock(x-1, y, z-1) != null){
  160.                             shading[0] = off;
  161.                         }
  162.                         if(getBlock(x-1, y-1, z) != null ||
  163.                                 getBlock(x-1, y-1, z-1) != null ||
  164.                                 getBlock(x-1, y, z-1) != null){
  165.                             shading[1] = off;
  166.                         }
  167.                         if(getBlock(x-1, y-1, z) != null ||
  168.                                 getBlock(x-1, y-1, z+1) != null ||
  169.                                 getBlock(x-1, y, z+1) != null){
  170.                             shading[2] = off;
  171.                         }
  172.                         if(getBlock(x-1, y+1, z) != null ||
  173.                                 getBlock(x-1, y+1, z+1) != null ||
  174.                                 getBlock(x-1, y, z+1) != null){
  175.                             shading[3] = off;
  176.                         }
  177.                         colorBuffer.put(Block.faceColorData(shading));
  178.                         vertexBuffer.put(block.faceLeftVertexData(xx, yy, zz));
  179.                         textureCoordBuffer.put(block.faceLeftTextureData(xx, yy, zz));
  180.                         size++;
  181.                     }
  182.                     if(!right){
  183.                         float[] shading = new float[]{1,1,1,1};
  184.                         if(getBlock(x+1, y-1, z) != null ||
  185.                                 getBlock(x+1, y-1, z-1) != null ||
  186.                                 getBlock(x+1, y, z-1) != null){
  187.                             shading[0] = off;
  188.                         }
  189.                         if(getBlock(x+1, y+1, z) != null ||
  190.                                 getBlock(x+1, y+1, z-1) != null ||
  191.                                 getBlock(x+1, y, z-1) != null){
  192.                             shading[1] = off;
  193.                         }
  194.                         if(getBlock(x+1, y+1, z) != null ||
  195.                                 getBlock(x+1, y+1, z+1) != null ||
  196.                                 getBlock(x+1, y, z+1) != null){
  197.                             shading[2] = off;
  198.                         }
  199.                         if(getBlock(x+1, y-1, z) != null ||
  200.                                 getBlock(x+1, y-1, z+1) != null ||
  201.                                 getBlock(x+1, y, z+1) != null){
  202.                             shading[3] = off;
  203.                         }
  204.                         colorBuffer.put(Block.faceColorData(shading));
  205.                         vertexBuffer.put(block.faceRightVertexData(xx, yy, zz));
  206.                         textureCoordBuffer.put(block.faceRightTextureData(xx, yy, zz));
  207.                         size++;
  208.                     }
  209.                     if(!front){
  210.                         float[] shading = new float[]{1,1,1,1};
  211.                         if(getBlock(x+1, y, z-1) != null ||
  212.                                 getBlock(x+1, y-1, z-1) != null ||
  213.                                 getBlock(x, y-1, z-1) != null){
  214.                             shading[0] = off;
  215.                         }
  216.                         if(getBlock(x-1, y, z-1) != null ||
  217.                                 getBlock(x-1, y-1, z-1) != null ||
  218.                                 getBlock(x, y-1, z-1) != null){
  219.                             shading[1] = off;
  220.                         }
  221.                         if(getBlock(x-1, y, z-1) != null ||
  222.                                 getBlock(x-1, y+1, z-1) != null ||
  223.                                 getBlock(x, y+1, z-1) != null){
  224.                             shading[2] = off;
  225.                         }
  226.                         if(getBlock(x+1, y, z-1) != null ||
  227.                                 getBlock(x+1, y+1, z-1) != null ||
  228.                                 getBlock(x, y+1, z-1) != null){
  229.                             shading[3] = off;
  230.                         }
  231.                         colorBuffer.put(Block.faceColorData(shading));
  232.                         vertexBuffer.put(block.faceFrontVertexData(xx, yy, zz));
  233.                         textureCoordBuffer.put(block.faceFrontTextureData(xx, yy, zz));
  234.                         size++;
  235.                     }
  236.                     if(!back){
  237.                         float[] shading = new float[]{1,1,1,1};
  238.                         if(getBlock(x+1, y, z+1) != null ||
  239.                                 getBlock(x+1, y-1, z+1) != null ||
  240.                                 getBlock(x, y-1, z+1) != null){
  241.                             shading[0] = off;
  242.                         }
  243.                         if(getBlock(x-1, y, z+1) != null ||
  244.                                 getBlock(x-1, y-1, z+1) != null ||
  245.                                 getBlock(x, y-1, z+1) != null){
  246.                             shading[1] = off;
  247.                         }
  248.                         if(getBlock(x-1, y, z+1) != null ||
  249.                                 getBlock(x-1, y+1, z+1) != null ||
  250.                                 getBlock(x, y+1, z+1) != null){
  251.                             shading[2] = off;
  252.                         }
  253.                         if(getBlock(x+1, y, z+1) != null ||
  254.                                 getBlock(x+1, y+1, z+1) != null ||
  255.                                 getBlock(x, y+1, z+1) != null){
  256.                             shading[3] = off;
  257.                         }
  258.                         colorBuffer.put(Block.faceColorData(shading));
  259.                         vertexBuffer.put(block.faceBackVertexData(xx, yy, zz));
  260.                         textureCoordBuffer.put(block.faceBackTextureData(xx, yy, zz));
  261.                         size++;
  262.                     }
  263.  
  264.                     bufferSize += size*4;
  265.                 }
  266.             }
  267.         }
  268.         vertexBuffer.flip();
  269.         textureCoordBuffer.flip();
  270.         colorBuffer.flip();
  271.        
  272.         vertexBufferId = glGenBuffers();
  273.         glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
  274.         glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
  275.        
  276.         textureCoordBufferId = glGenBuffers();
  277.         glBindBuffer(GL_ARRAY_BUFFER, textureCoordBufferId);
  278.         glBufferData(GL_ARRAY_BUFFER, textureCoordBuffer, GL_STATIC_DRAW);
  279.        
  280.         colorBufferId = glGenBuffers();
  281.         glBindBuffer(GL_ARRAY_BUFFER, colorBufferId);
  282.         glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
  283.        
  284.         loaded = true;
  285.     }
  286.     public void reload(){
  287.         if(!loaded){
  288.             load();
  289.             return;
  290.         }
  291.         unload();
  292.         vertexBuffer.clear();
  293.         textureCoordBuffer.clear();
  294.         colorBuffer.clear();
  295.         bufferSize = 0;
  296.         for(int x = 0 ; x < SIZE ; x++){
  297.             for(int y = 0 ; y < MAX_BUILD_HEIGHT ; y++){
  298.                 for(int z = 0 ; z < SIZE ; z++){
  299.                     int xx = this.x * SIZE + x;
  300.                     int yy = y;
  301.                     int zz = this.z * SIZE + z;
  302.                    
  303.                     Block upb = getBlock(x, y+1, z);
  304.                     Block downb = getBlock(x, y-1, z);
  305.                     Block leftb = getBlock(x-1, y, z);
  306.                     Block rightb = getBlock(x+1, y, z);
  307.                     Block frontb = getBlock(x, y, z-1);
  308.                     Block backb = getBlock(x, y, z+1);
  309.                     boolean up = false;
  310.                     boolean down = false;
  311.                     boolean left = false;
  312.                     boolean right = false;
  313.                     boolean front = false;
  314.                     boolean back = false;
  315.                    
  316.                     if(upb != null){
  317.                         up = !upb.type.isTransparent();
  318.                     }
  319.                     if(downb != null){
  320.                         down = !downb.type.isTransparent();
  321.                     }
  322.                     if(leftb != null){
  323.                         left = !leftb.type.isTransparent();
  324.                     }
  325.                     if(rightb != null){
  326.                         right = !rightb.type.isTransparent();
  327.                     }
  328.                     if(frontb != null){
  329.                         front = !frontb.type.isTransparent();
  330.                     }
  331.                     if(backb != null){
  332.                         back = !backb.type.isTransparent();
  333.                     }
  334.                    
  335.                     if(up && down && left && right && front && back)continue;
  336.                     if(blocks[x][y][z] == null)continue;
  337.                    
  338.                     Block block = blocks[x][y][z];
  339.                    
  340.                     int size = 0;
  341.                     float off = 0.7f;
  342.                     if(!up){
  343.                         float[] shading = new float[]{1,1,1,1};
  344.                         if(getBlock(x+1, y+1, z) != null ||
  345.                                 getBlock(x+1, y+1, z-1) != null ||
  346.                                 getBlock(x, y+1, z-1) != null){
  347.                             shading[0] = off;
  348.                         }
  349.                         if(getBlock(x-1, y+1, z) != null ||
  350.                                 getBlock(x-1, y+1, z-1) != null ||
  351.                                 getBlock(x, y+1, z-1) != null){
  352.                             shading[1] = off;
  353.                         }
  354.                         if(getBlock(x-1, y+1, z) != null ||
  355.                                 getBlock(x-1, y+1, z+1) != null ||
  356.                                 getBlock(x, y+1, z+1) != null){
  357.                             shading[2] = off;
  358.                         }
  359.                         if(getBlock(x, y+1, z+1) != null ||
  360.                                 getBlock(x+1, y+1, z+1) != null ||
  361.                                 getBlock(x+1, y+1, z) != null){
  362.                             shading[3] = off;
  363.                         }
  364.                         colorBuffer.put(Block.faceColorData(shading));
  365.                         vertexBuffer.put(block.faceUpVertexData(xx, yy, zz));
  366.                         textureCoordBuffer.put(block.faceUpTextureData(xx, yy, zz));
  367.                         size++;
  368.                     }
  369.                     if(!down){
  370.                         float[] shading = new float[]{1,1,1,1};
  371.                         if(getBlock(x-1, y-1, z) != null ||
  372.                                 getBlock(x-1, y-1, z-1) != null ||
  373.                                 getBlock(x, y-1, z-1) != null){
  374.                             shading[0] = off;                          
  375.                         }
  376.                         if(getBlock(x+1, y-1, z) != null ||
  377.                                 getBlock(x+1, y-1, z-1) != null ||
  378.                                 getBlock(x, y-1, z-1) != null){
  379.                             shading[1] = off;
  380.                         }
  381.                         if(getBlock(x+1, y-1, z) != null ||
  382.                                 getBlock(x+1, y-1, z+1) != null ||
  383.                                 getBlock(x, y-1, z+1) != null){
  384.                             shading[2] = off;
  385.                         }
  386.                         if(getBlock(x-1, y-1, z) != null ||
  387.                                 getBlock(x-1, y-1, z+1) != null ||
  388.                                 getBlock(x, y-1, z+1) != null){
  389.                             shading[3] = off;
  390.                         }
  391.                         colorBuffer.put(Block.faceColorData(shading));
  392.                         vertexBuffer.put(block.faceDownVertexData(xx, yy, zz));
  393.                         textureCoordBuffer.put(block.faceDownTextureData(xx, yy, zz));
  394.                         size++;
  395.                     }
  396.                     if(!left){
  397.                         float[] shading = new float[]{1,1,1,1};
  398.                         if(getBlock(x-1, y+1, z) != null ||
  399.                                 getBlock(x-1, y+1, z-1) != null ||
  400.                                 getBlock(x-1, y, z-1) != null){
  401.                             shading[0] = off;
  402.                         }
  403.                         if(getBlock(x-1, y-1, z) != null ||
  404.                                 getBlock(x-1, y-1, z-1) != null ||
  405.                                 getBlock(x-1, y, z-1) != null){
  406.                             shading[1] = off;
  407.                         }
  408.                         if(getBlock(x-1, y-1, z) != null ||
  409.                                 getBlock(x-1, y-1, z+1) != null ||
  410.                                 getBlock(x-1, y, z+1) != null){
  411.                             shading[2] = off;
  412.                         }
  413.                         if(getBlock(x-1, y+1, z) != null ||
  414.                                 getBlock(x-1, y+1, z+1) != null ||
  415.                                 getBlock(x-1, y, z+1) != null){
  416.                             shading[3] = off;
  417.                         }
  418.                         colorBuffer.put(Block.faceColorData(shading));
  419.                         vertexBuffer.put(block.faceLeftVertexData(xx, yy, zz));
  420.                         textureCoordBuffer.put(block.faceLeftTextureData(xx, yy, zz));
  421.                         size++;
  422.                     }
  423.                     if(!right){
  424.                         float[] shading = new float[]{1,1,1,1};
  425.                         if(getBlock(x+1, y-1, z) != null ||
  426.                                 getBlock(x+1, y-1, z-1) != null ||
  427.                                 getBlock(x+1, y, z-1) != null){
  428.                             shading[0] = off;
  429.                         }
  430.                         if(getBlock(x+1, y+1, z) != null ||
  431.                                 getBlock(x+1, y+1, z-1) != null ||
  432.                                 getBlock(x+1, y, z-1) != null){
  433.                             shading[1] = off;
  434.                         }
  435.                         if(getBlock(x+1, y+1, z) != null ||
  436.                                 getBlock(x+1, y+1, z+1) != null ||
  437.                                 getBlock(x+1, y, z+1) != null){
  438.                             shading[2] = off;
  439.                         }
  440.                         if(getBlock(x+1, y-1, z) != null ||
  441.                                 getBlock(x+1, y-1, z+1) != null ||
  442.                                 getBlock(x+1, y, z+1) != null){
  443.                             shading[3] = off;
  444.                         }
  445.                         colorBuffer.put(Block.faceColorData(shading));
  446.                         vertexBuffer.put(block.faceRightVertexData(xx, yy, zz));
  447.                         textureCoordBuffer.put(block.faceRightTextureData(xx, yy, zz));
  448.                         size++;
  449.                     }
  450.                     if(!front){
  451.                         float[] shading = new float[]{1,1,1,1};
  452.                         if(getBlock(x+1, y, z-1) != null ||
  453.                                 getBlock(x+1, y-1, z-1) != null ||
  454.                                 getBlock(x, y-1, z-1) != null){
  455.                             shading[0] = off;
  456.                         }
  457.                         if(getBlock(x-1, y, z-1) != null ||
  458.                                 getBlock(x-1, y-1, z-1) != null ||
  459.                                 getBlock(x, y-1, z-1) != null){
  460.                             shading[1] = off;
  461.                         }
  462.                         if(getBlock(x-1, y, z-1) != null ||
  463.                                 getBlock(x-1, y+1, z-1) != null ||
  464.                                 getBlock(x, y+1, z-1) != null){
  465.                             shading[2] = off;
  466.                         }
  467.                         if(getBlock(x+1, y, z-1) != null ||
  468.                                 getBlock(x+1, y+1, z-1) != null ||
  469.                                 getBlock(x, y+1, z-1) != null){
  470.                             shading[3] = off;
  471.                         }
  472.                         colorBuffer.put(Block.faceColorData(shading));
  473.                         vertexBuffer.put(block.faceFrontVertexData(xx, yy, zz));
  474.                         textureCoordBuffer.put(block.faceFrontTextureData(xx, yy, zz));
  475.                         size++;
  476.                     }
  477.                     if(!back){
  478.                         float[] shading = new float[]{1,1,1,1};
  479.                         if(getBlock(x+1, y, z+1) != null ||
  480.                                 getBlock(x+1, y-1, z+1) != null ||
  481.                                 getBlock(x, y-1, z+1) != null){
  482.                             shading[0] = off;
  483.                         }
  484.                         if(getBlock(x-1, y, z+1) != null ||
  485.                                 getBlock(x-1, y-1, z+1) != null ||
  486.                                 getBlock(x, y-1, z+1) != null){
  487.                             shading[1] = off;
  488.                         }
  489.                         if(getBlock(x-1, y, z+1) != null ||
  490.                                 getBlock(x-1, y+1, z+1) != null ||
  491.                                 getBlock(x, y+1, z+1) != null){
  492.                             shading[2] = off;
  493.                         }
  494.                         if(getBlock(x+1, y, z+1) != null ||
  495.                                 getBlock(x+1, y+1, z+1) != null ||
  496.                                 getBlock(x, y+1, z+1) != null){
  497.                             shading[3] = off;
  498.                         }
  499.                         colorBuffer.put(Block.faceColorData(shading));
  500.                         vertexBuffer.put(block.faceBackVertexData(xx, yy, zz));
  501.                         textureCoordBuffer.put(block.faceBackTextureData(xx, yy, zz));
  502.                         size++;
  503.                     }
  504.                    
  505.                     bufferSize += size*4;
  506.                 }
  507.             }
  508.         }
  509.         vertexBuffer.flip();
  510.         textureCoordBuffer.flip();
  511.         colorBuffer.flip();
  512.        
  513.         glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
  514.         glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
  515.        
  516.         glBindBuffer(GL_ARRAY_BUFFER, textureCoordBufferId);
  517.         glBufferData(GL_ARRAY_BUFFER, textureCoordBuffer, GL_STATIC_DRAW);
  518.        
  519.         glBindBuffer(GL_ARRAY_BUFFER, colorBufferId);
  520.         glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
  521.        
  522.         loaded = true;
  523.     }
  524.     public void unload(){
  525.         glDeleteBuffers(vertexBufferId);
  526.         glDeleteBuffers(textureCoordBufferId);
  527.         glDeleteBuffers(colorBufferId);
  528.         loaded = false;
  529.     }
  530.    
  531.     public Block getBlock(int x, int y, int z){
  532.         if(y < 0 || y >= MAX_BUILD_HEIGHT)return null;
  533.         if(x < 0){
  534.             if(z < 0 ){
  535.                 if(world.getChunk(this.x-1, this.z-1) == null)return null;
  536.                 return world.getChunk(this.x-1, this.z-1).getBlock(Chunk.SIZE+x, y, Chunk.SIZE+z);
  537.             }else if(z >= Chunk.SIZE){
  538.                 if(world.getChunk(this.x-1, this.z+1) == null)return null;
  539.                 return world.getChunk(this.x-1, this.z+1).getBlock(Chunk.SIZE+x, y, z-Chunk.SIZE);
  540.             }else{
  541.                 if(world.getChunk(this.x-1, this.z) == null)return null;
  542.                 return world.getChunk(this.x-1, this.z).getBlock(Chunk.SIZE+x, y, z);
  543.             }
  544.         }else if(x >= Chunk.SIZE){
  545.             if(z < 0 ){
  546.                 if(world.getChunk(this.x+1, this.z-1) == null)return null;
  547.                 return world.getChunk(this.x+1, this.z-1).getBlock(x-Chunk.SIZE, y, Chunk.SIZE+z);
  548.             }else if(z >= Chunk.SIZE){
  549.                 if(world.getChunk(this.x+1, this.z+1) == null)return null;
  550.                 return world.getChunk(this.x+1, this.z+1).getBlock(x-Chunk.SIZE, y, z-Chunk.SIZE);
  551.             }else{
  552.                 if(world.getChunk(this.x+1, this.z) == null)return null;
  553.                 return world.getChunk(this.x+1, this.z).getBlock(x-Chunk.SIZE, y, z);
  554.             }
  555.         }else{
  556.             if(z < 0 ){
  557.                 if(world.getChunk(this.x, this.z-1) == null)return null;
  558.                 return world.getChunk(this.x, this.z-1).getBlock(x, y, Chunk.SIZE+z);
  559.             }else if(z >= Chunk.SIZE){
  560.                 if(world.getChunk(this.x, this.z+1) == null)return null;
  561.                 return world.getChunk(this.x, this.z+1).getBlock(x, y, z-Chunk.SIZE);
  562.             }else{
  563.                 return blocks[x][y][z];
  564.             }
  565.         }
  566.     }
  567.     public void addBlock(int x, int y, int z, Block b){
  568.         if(y < 0 || y >= MAX_BUILD_HEIGHT)return;
  569.         if(x < 0){
  570.             if(z < 0 ){
  571.                 if(world.getChunk(this.x-1, this.z-1) == null)return;
  572.                 world.getChunk(this.x-1, this.z-1).addBlock(Chunk.SIZE+x, y, Chunk.SIZE+z, b);
  573.             }else{
  574.                 if(world.getChunk(this.x-1, this.z) == null)return;
  575.                 world.getChunk(this.x-1, this.z).addBlock(Chunk.SIZE+x, y, z, b);
  576.             }
  577.         }else{
  578.             if(z < 0 ){
  579.                 if(world.getChunk(this.x, this.z-1) == null)return;
  580.                 world.getChunk(this.x, this.z-1).addBlock(x, y, Chunk.SIZE+z, b);
  581.             }else{
  582.                 blocks[x][y][z] = b;
  583.             }
  584.         }
  585.        
  586.         if(vertexBuffer != null){
  587.             reload();
  588.             int xx = this.x;
  589.             int zz = this.z;
  590.             if(x == 0){
  591.                 if(world.getChunk(xx-1, zz) != null){
  592.                     world.getChunk(xx-1, zz).reload();
  593.                 }
  594.             }
  595.             if(x == SIZE-1){
  596.                 if(world.getChunk(xx+1, zz) != null){
  597.                     world.getChunk(xx+1, zz).reload();
  598.                 }
  599.             }
  600.             if(z == 0){
  601.                 if(world.getChunk(xx, zz-1) != null){
  602.                     world.getChunk(xx, zz-1).reload();
  603.                 }
  604.             }
  605.             if(z == SIZE-1){
  606.                 if(world.getChunk(xx, zz+1) != null){
  607.                     world.getChunk(xx, zz+1).reload();
  608.                 }
  609.             }
  610.         }
  611.     }
  612.     public void removeBlock(int x, int y, int z){
  613.         if(y < 0 || y >= MAX_BUILD_HEIGHT)return;
  614.         if(x < 0){
  615.             if(z < 0 ){
  616.                 if(world.getChunk(this.x-1, this.z-1) == null)return;
  617.                 world.getChunk(this.x-1, this.z-1).removeBlock(Chunk.SIZE+x, y, Chunk.SIZE+z);
  618.             }else{
  619.                 if(world.getChunk(this.x-1, this.z) == null)return;
  620.                 world.getChunk(this.x-1, this.z).removeBlock(Chunk.SIZE+x, y, z);
  621.             }
  622.         }else{
  623.             if(z < 0 ){
  624.                 if(world.getChunk(this.x, this.z-1) == null)return;
  625.                 world.getChunk(this.x, this.z-1).removeBlock(x, y, Chunk.SIZE+z);
  626.             }else{
  627.                 blocks[x][y][z] = null;
  628.             }
  629.         }
  630.        
  631.         if(vertexBuffer != null){
  632.             reload();
  633.             int xx = this.x;
  634.             int zz = this.z;
  635.             if(x == 0){
  636.                 if(world.getChunk(xx-1, zz) != null){
  637.                     world.getChunk(xx-1, zz).reload();
  638.                 }
  639.             }
  640.             if(x == SIZE-1){
  641.                 if(world.getChunk(xx+1, zz) != null){
  642.                     world.getChunk(xx+1, zz).reload();
  643.                 }
  644.             }
  645.             if(z == 0){
  646.                 if(world.getChunk(xx, zz-1) != null){
  647.                     world.getChunk(xx, zz-1).reload();
  648.                 }
  649.             }
  650.             if(z == SIZE-1){
  651.                 if(world.getChunk(xx, zz+1) != null){
  652.                     world.getChunk(xx, zz+1).reload();
  653.                 }
  654.             }
  655.         }
  656.     }
  657.    
  658.     public void update(){
  659.        
  660.     }
  661.    
  662.     public void render(){
  663.         Shader.CHUNK.bind();
  664.        
  665.         TextureManager.ENV_TEXTURE.bind(0);
  666.         Shader.CHUNK.setUniform("sampler", 0);
  667.         Shader.CHUNK.setUniform("MVP", new Matrix4f().perspective(Camera.fov, (float)Display.getWidth()/(float)Display.getHeight(), 0.1f, 1000.0f));
  668.        
  669.         glEnableVertexAttribArray(ChunkShader.vertexColorLocation);
  670.         glEnableVertexAttribArray(ChunkShader.vertexPositionLocation);
  671.         glEnableVertexAttribArray(ChunkShader.vertexTexCoordLocation);
  672.        
  673.         glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
  674.         glVertexAttribPointer(ChunkShader.vertexPositionLocation, 3, GL_FLOAT, false, 0, 0);
  675.        
  676.         glBindBuffer(GL_ARRAY_BUFFER, textureCoordBufferId);
  677.         glVertexAttribPointer(ChunkShader.vertexTexCoordLocation, 2, GL_FLOAT, true, 0, 0);
  678.        
  679.         glBindBuffer(GL_ARRAY_BUFFER, colorBufferId);
  680.         glVertexAttribPointer(ChunkShader.vertexColorLocation, 4, GL_FLOAT, true, 0, 0);
  681.        
  682.         glDrawArrays(GL_QUADS, 0, bufferSize);
  683.        
  684.         glDisableVertexAttribArray(ChunkShader.vertexColorLocation);
  685.         glDisableVertexAttribArray(ChunkShader.vertexPositionLocation);
  686.         glDisableVertexAttribArray(ChunkShader.vertexTexCoordLocation);
  687.        
  688.         Texture.unbind();
  689.        
  690.         Shader.unbind();
  691.        
  692.         if(Camera.debug){
  693.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  694.             glLineWidth(1);
  695.             glDisable(GL_CULL_FACE);
  696.             glBegin(GL_QUADS);
  697.             glVertex3f(x*SIZE,          0,          z*SIZE);
  698.             glVertex3f(x*SIZE+SIZE,     0,          z*SIZE);
  699.             glVertex3f(x*SIZE+SIZE,     MAX_BUILD_HEIGHT,   z*SIZE);
  700.             glVertex3f(x*SIZE,          MAX_BUILD_HEIGHT,   z*SIZE);
  701.                
  702.             glVertex3f(x*SIZE,          0,          z*SIZE+SIZE);
  703.             glVertex3f(x*SIZE+SIZE,     0,          z*SIZE+SIZE);
  704.             glVertex3f(x*SIZE+SIZE,     MAX_BUILD_HEIGHT,   z*SIZE+SIZE);
  705.             glVertex3f(x*SIZE,          MAX_BUILD_HEIGHT,   z*SIZE+SIZE);
  706.                
  707.             glVertex3f(x*SIZE,          0,          z*SIZE);
  708.             glVertex3f(x*SIZE+SIZE,     0,          z*SIZE);
  709.             glVertex3f(x*SIZE+SIZE,     0,          z*SIZE+SIZE);
  710.             glVertex3f(x*SIZE,          0,          z*SIZE+SIZE);
  711.                
  712.             glVertex3f(x*SIZE,          MAX_BUILD_HEIGHT,   z*SIZE);
  713.             glVertex3f(x*SIZE+SIZE,     MAX_BUILD_HEIGHT,   z*SIZE);
  714.             glVertex3f(x*SIZE+SIZE,     MAX_BUILD_HEIGHT,   z*SIZE+SIZE);
  715.             glVertex3f(x*SIZE,          MAX_BUILD_HEIGHT,   z*SIZE+SIZE);
  716.             glEnd();
  717.             glEnable(GL_CULL_FACE);
  718.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  719.         }
  720.        
  721.     }
  722. }
Advertisement
Add Comment
Please, Sign In to add comment