Guest User

Untitled

a guest
May 15th, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. private static final float BLOCK_WIDTH = 1f;
  2. private static final float BLOCK_HEIGHT = 1f;
  3. private static final float BLOCK_LENGTH = -1f;
  4. private static final int TEXTURES_SPLIT = 256/16;
  5.  
  6. private static final int LENGTH = 16;
  7. private Vector3f pos;
  8.  
  9. public Chunk(float xPos, float yPos, float zPos) {
  10. super(createChunk());
  11. setPos(new Vector3f((xPos*LENGTH), (yPos*LENGTH)-BLOCK_HEIGHT, (zPos*LENGTH)));
  12.  
  13. }
  14.  
  15. private static Mesh createChunk() {
  16. List<Vertex> verticesArray = new ArrayList<Vertex>();
  17. List<Integer> indicesArray = new ArrayList<Integer>();
  18. List<Vector2f> textureArray = new ArrayList<Vector2f>();
  19. List<Vector3f> normalsArray = new ArrayList<Vector3f>();
  20.  
  21. for (int x = 0; x < LENGTH; x++) {
  22. for (int y = 0; y < LENGTH; y++) {
  23. for (int z = 0; z < LENGTH; z++) {
  24. createCube(verticesArray, indicesArray, textureArray, normalsArray, x,y,z);
  25. }
  26. }
  27. }
  28. float[] vertices = new float[verticesArray.size() * 3];
  29. int[] indices = new int[indicesArray.size()];
  30. float[] textures = new float[textureArray.size() * 2];
  31. float[] normals = new float[normalsArray.size() * 3];
  32.  
  33. int pointer = 0;
  34. for (int i = 0; i < verticesArray.size(); i++) {
  35. Vertex v = verticesArray.get(i);
  36. vertices[pointer++] = v.getX();
  37. vertices[pointer++] = v.getY();
  38. vertices[pointer++] = v.getZ();
  39. }
  40. for (int i = 0; i < indices.length; i++) {
  41. indices[i] = indicesArray.get(i);
  42. }
  43. pointer = 0;
  44. for (int i = 0; i < textureArray.size(); i++) {
  45. Vector2f texCoord = textureArray.get(i);
  46. textures[pointer++] = texCoord.getX();
  47. textures[pointer++] = texCoord.getY();
  48. }
  49.  
  50. pointer = 0;
  51. for (int i = 0; i < normalsArray.size(); i++) {
  52. Vector3f n = normalsArray.get(i);
  53. normals[pointer++] = n.getX();
  54. normals[pointer++] = n.getY();
  55. normals[pointer++] = n.getZ();
  56. }
  57. return new Mesh(vertices, indices, textures, normals);
  58. }
  59.  
  60. private static void createCube(List<Vertex> verticesArray, List<Integer> indicesArray, List<Vector2f> textureArray, List<Vector3f> normalsArray, float xPos, float yPos, float zPos) {
  61.  
  62. // Face
  63. indicesArray.add(verticesArray.size() + 0);
  64. indicesArray.add(verticesArray.size() + 1);
  65. indicesArray.add(verticesArray.size() + 2);
  66.  
  67. indicesArray.add(verticesArray.size() + 2);
  68. indicesArray.add(verticesArray.size() + 3);
  69. indicesArray.add(verticesArray.size() + 0);
  70.  
  71. textureArray.add(new Vector2f(0, 0));
  72. textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
  73. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
  74. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
  75.  
  76. normalsArray.add(new Vector3f(0, 0, 1));
  77. normalsArray.add(new Vector3f(0, 0, 1));
  78. normalsArray.add(new Vector3f(0, 0, 1));
  79. normalsArray.add(new Vector3f(0, 0, 1));
  80.  
  81. verticesArray.add(new Vertex(xPos, yPos, zPos));
  82. verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
  83. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
  84. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
  85.  
  86. // Back
  87. indicesArray.add(verticesArray.size() + 0);
  88. indicesArray.add(verticesArray.size() + 3);
  89. indicesArray.add(verticesArray.size() + 2);
  90.  
  91. indicesArray.add(verticesArray.size() + 2);
  92. indicesArray.add(verticesArray.size() + 1);
  93. indicesArray.add(verticesArray.size() + 0);
  94.  
  95. textureArray.add(new Vector2f(0, 0));
  96. textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
  97. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
  98. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
  99.  
  100. normalsArray.add(new Vector3f(0, 0, -1));
  101. normalsArray.add(new Vector3f(0, 0, -1));
  102. normalsArray.add(new Vector3f(0, 0, -1));
  103. normalsArray.add(new Vector3f(0, 0, -1));
  104.  
  105. verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
  106. verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
  107. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
  108. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
  109.  
  110. // Left
  111. indicesArray.add(verticesArray.size() + 0);
  112. indicesArray.add(verticesArray.size() + 1);
  113. indicesArray.add(verticesArray.size() + 2);
  114.  
  115. indicesArray.add(verticesArray.size() + 2);
  116. indicesArray.add(verticesArray.size() + 3);
  117. indicesArray.add(verticesArray.size() + 0);
  118.  
  119. textureArray.add(new Vector2f(0, 0));
  120. textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
  121. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
  122. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
  123.  
  124. normalsArray.add(new Vector3f(-1, 0, 0));
  125. normalsArray.add(new Vector3f(-1, 0, 0));
  126. normalsArray.add(new Vector3f(-1, 0, 0));
  127. normalsArray.add(new Vector3f(-1, 0, 0));
  128.  
  129. verticesArray.add(new Vertex(xPos, yPos, zPos));
  130. verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
  131. verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
  132. verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
  133.  
  134. // Right
  135. indicesArray.add(verticesArray.size() + 0);
  136. indicesArray.add(verticesArray.size() + 3);
  137. indicesArray.add(verticesArray.size() + 2);
  138.  
  139. indicesArray.add(verticesArray.size() + 2);
  140. indicesArray.add(verticesArray.size() + 1);
  141. indicesArray.add(verticesArray.size() + 0);
  142.  
  143. textureArray.add(new Vector2f(0, 0));
  144. textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
  145. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
  146. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
  147.  
  148. normalsArray.add(new Vector3f(1, 0, 0));
  149. normalsArray.add(new Vector3f(1, 0, 0));
  150. normalsArray.add(new Vector3f(1, 0, 0));
  151. normalsArray.add(new Vector3f(1, 0, 0));
  152.  
  153. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
  154. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
  155. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
  156. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
  157.  
  158. // Bottom
  159. indicesArray.add(verticesArray.size() + 0);
  160. indicesArray.add(verticesArray.size() + 3);
  161. indicesArray.add(verticesArray.size() + 2);
  162.  
  163. indicesArray.add(verticesArray.size() + 2);
  164. indicesArray.add(verticesArray.size() + 1);
  165. indicesArray.add(verticesArray.size() + 0);
  166.  
  167. textureArray.add(new Vector2f(0, 0));
  168. textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
  169. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
  170. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
  171.  
  172. normalsArray.add(new Vector3f(0, -1, 0));
  173. normalsArray.add(new Vector3f(0, -1, 0));
  174. normalsArray.add(new Vector3f(0, -1, 0));
  175. normalsArray.add(new Vector3f(0, -1, 0));
  176.  
  177. verticesArray.add(new Vertex(xPos, yPos, zPos));
  178. verticesArray.add(new Vertex(xPos, yPos, zPos + BLOCK_LENGTH));
  179. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos + BLOCK_LENGTH));
  180. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos, zPos));
  181.  
  182. // Top
  183. indicesArray.add(verticesArray.size() + 0);
  184. indicesArray.add(verticesArray.size() + 1);
  185. indicesArray.add(verticesArray.size() + 2);
  186.  
  187. indicesArray.add(verticesArray.size() + 2);
  188. indicesArray.add(verticesArray.size() + 3);
  189. indicesArray.add(verticesArray.size() + 0);
  190.  
  191. textureArray.add(new Vector2f(0, 0));
  192. textureArray.add(new Vector2f(0, 1/TEXTURES_SPLIT));
  193. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 1/TEXTURES_SPLIT));
  194. textureArray.add(new Vector2f(1/TEXTURES_SPLIT, 0));
  195.  
  196. normalsArray.add(new Vector3f(0, 1, 0));
  197. normalsArray.add(new Vector3f(0, 1, 0));
  198. normalsArray.add(new Vector3f(0, 1, 0));
  199. normalsArray.add(new Vector3f(0, 1, 0));
  200.  
  201. verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos));
  202. verticesArray.add(new Vertex(xPos, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
  203. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos + BLOCK_LENGTH));
  204. verticesArray.add(new Vertex(xPos + BLOCK_WIDTH, yPos + BLOCK_HEIGHT, zPos));
  205. }
  206.  
  207. public void render(StaticShader shader) {
  208. shader.start();
  209. {
  210. GL30.glBindVertexArray(getMesh().getVaoID());
  211. {
  212. GL20.glEnableVertexAttribArray(0);
  213. GL20.glEnableVertexAttribArray(1);
  214. GL20.glEnableVertexAttribArray(2);
  215. {
  216. shader.loadTransformationMatrix(Utils.createTransformationMatrix3D(pos, new Vector3f(), new Vector3f(1, 1, 1)));
  217. GL11.glDrawElements(GL11.GL_TRIANGLES, getMesh().getCount(), GL11.GL_UNSIGNED_INT, 0);
  218. }
  219. GL20.glDisableVertexAttribArray(2);
  220. GL20.glDisableVertexAttribArray(1);
  221. GL20.glDisableVertexAttribArray(0);
  222. }
  223. GL30.glBindVertexArray(0);
  224. }
  225. shader.stop();
  226.  
  227. }
  228.  
  229. public Vector3f getPos() {
  230. return pos;
  231. }
  232.  
  233. public void setPos(Vector3f pos) {
  234. this.pos = pos;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment