Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. public class ChunkUniform {
  2. private FloatBuffer vertexBuffer;
  3. private ShortBuffer indexBuffer;
  4. private FloatBuffer colorBuffer;
  5.  
  6. private final int vertCount;
  7.  
  8. /**
  9. * Creates a n*n chunk of uniform-terrain.
  10. * This implies, that the chunk consists of a closed plane without any cave openings.
  11. * @param heights An array containing the height map of the chunk. Scaled in Meters. 64 in size.
  12. */
  13. public ChunkUniform(float[] heights) {
  14. //The size of the Chunks sides in Fields. (n)
  15. final int sizeFields = ((int) Math.sqrt(heights.length)) - 1;
  16. vertCount = sizeFields * sizeFields * 2 * 3;
  17.  
  18. ByteBuffer bb = ByteBuffer.allocateDirect(vertCount * 3 * 4);
  19. bb.order(ByteOrder.nativeOrder());
  20. vertexBuffer = bb.asFloatBuffer();
  21.  
  22. bb = ByteBuffer.allocateDirect(vertCount * 2);
  23. bb.order(ByteOrder.nativeOrder());
  24. indexBuffer = bb.asShortBuffer();
  25.  
  26. bb = ByteBuffer.allocateDirect(vertCount * 4 * 4);
  27. bb.order(ByteOrder.nativeOrder());
  28. colorBuffer = bb.asFloatBuffer();
  29.  
  30. for (int z = 0; z < sizeFields; z++) {
  31. for (int x = 0; x < sizeFields; x++) {
  32. //Adds the first face to the square
  33. putVertex(x, heights[x + z * sizeFields], z );
  34. putVertex(x+1, heights[(x+1) + z * sizeFields], z );
  35. putVertex(x, heights[x + (z+1) * sizeFields], z+1);
  36.  
  37. //Adds the second face to the square
  38. putVertex(x+1, heights[(x+1) + z * sizeFields], z );
  39. putVertex(x, heights[x + (z+1) * sizeFields], z+1);
  40. putVertex(x+1, heights[(x+1) + (z+1) * sizeFields], z+1);
  41. }
  42. }
  43.  
  44. for (short i = 0; i < vertCount; i++)
  45. indexBuffer.put(i);
  46.  
  47. //Todo: Fill a color buffer dynamically or according to the chunks surroundings.
  48. Random r = new Random(0);
  49. for (int i = 0; i < vertCount / 3; i++) {
  50. float color[] = {
  51. r.nextFloat(),
  52. r.nextFloat(),
  53. r.nextFloat(),
  54. 1.0f
  55. };
  56. colorBuffer.put(color);
  57. colorBuffer.put(color);
  58. colorBuffer.put(color);
  59.  
  60. }
  61.  
  62. Log.d("Bellwether", "ChunkUniform: " + vertCount);
  63. }
  64.  
  65. /**
  66. * Adds a vertex to the vertex buffer.
  67. * @param x - coordinate in Meters
  68. * @param y - coordinate in Meters
  69. * @param z - coordinate in Meters
  70. */
  71. private void putVertex(float x, float y, float z) {
  72. vertexBuffer.put(new float[]{x, y, z});
  73. }
  74.  
  75. /**
  76. * Draws the chunk. Todo doc this better...
  77. * @param programHandle The openGL handle of the current rendering program.
  78. */
  79. public void draw(final int programHandle) {
  80. final int positionHandle = GLES20.glGetAttribLocation(programHandle, "vertPos");
  81. final int colorHandle = GLES20.glGetAttribLocation(programHandle, "vertColor");
  82.  
  83. GLES20.glEnableVertexAttribArray(positionHandle);
  84. vertexBuffer.rewind();
  85. GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer);
  86.  
  87. GLES20.glEnableVertexAttribArray(colorHandle);
  88. colorBuffer.rewind();
  89. GLES20.glVertexAttribPointer(colorHandle, 4, GLES20.GL_FLOAT, false, 16, colorBuffer);
  90.  
  91. indexBuffer.rewind();
  92.  
  93. GLES20.glDrawElements(GLES20.GL_TRIANGLES, vertCount, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
  94.  
  95. GLES20.glDisableVertexAttribArray(positionHandle);
  96. GLES20.glDisableVertexAttribArray(colorHandle);
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement