Created
January 24, 2017 20:43
-
-
Save busti/3e272c26699c85bf2a89e089a3865060 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChunkUniform { | |
private FloatBuffer vertexBuffer; | |
private ShortBuffer indexBuffer; | |
private FloatBuffer colorBuffer; | |
private final int vertCount; | |
/** | |
* Creates a n*n chunk of uniform-terrain. | |
* This implies, that the chunk consists of a closed plane without any cave openings. | |
* @param heights An array containing the height map of the chunk. Scaled in Meters. 64 in size. | |
*/ | |
public ChunkUniform(float[] heights) { | |
//The size of the Chunks sides in Fields. (n) | |
final int sizeFields = ((int) Math.sqrt(heights.length)) - 1; | |
vertCount = sizeFields * sizeFields * 2 * 3; | |
ByteBuffer bb = ByteBuffer.allocateDirect(vertCount * 3 * 4); | |
bb.order(ByteOrder.nativeOrder()); | |
vertexBuffer = bb.asFloatBuffer(); | |
bb = ByteBuffer.allocateDirect(vertCount * 2); | |
bb.order(ByteOrder.nativeOrder()); | |
indexBuffer = bb.asShortBuffer(); | |
bb = ByteBuffer.allocateDirect(vertCount * 4 * 4); | |
bb.order(ByteOrder.nativeOrder()); | |
colorBuffer = bb.asFloatBuffer(); | |
for (int z = 0; z < sizeFields; z++) { | |
for (int x = 0; x < sizeFields; x++) { | |
//Adds the first face to the square | |
putVertex(x, heights[x + z * sizeFields], z ); | |
putVertex(x+1, heights[(x+1) + z * sizeFields], z ); | |
putVertex(x, heights[x + (z+1) * sizeFields], z+1); | |
//Adds the second face to the square | |
putVertex(x+1, heights[(x+1) + z * sizeFields], z ); | |
putVertex(x, heights[x + (z+1) * sizeFields], z+1); | |
putVertex(x+1, heights[(x+1) + (z+1) * sizeFields], z+1); | |
} | |
} | |
for (short i = 0; i < vertCount; i++) | |
indexBuffer.put(i); | |
//Todo: Fill a color buffer dynamically or according to the chunks surroundings. | |
Random r = new Random(0); | |
for (int i = 0; i < vertCount / 3; i++) { | |
float color[] = { | |
r.nextFloat(), | |
r.nextFloat(), | |
r.nextFloat(), | |
1.0f | |
}; | |
colorBuffer.put(color); | |
colorBuffer.put(color); | |
colorBuffer.put(color); | |
} | |
Log.d("Bellwether", "ChunkUniform: " + vertCount); | |
} | |
/** | |
* Adds a vertex to the vertex buffer. | |
* @param x - coordinate in Meters | |
* @param y - coordinate in Meters | |
* @param z - coordinate in Meters | |
*/ | |
private void putVertex(float x, float y, float z) { | |
vertexBuffer.put(new float[]{x, y, z}); | |
} | |
/** | |
* Draws the chunk. Todo doc this better... | |
* @param programHandle The openGL handle of the current rendering program. | |
*/ | |
public void draw(final int programHandle) { | |
final int positionHandle = GLES20.glGetAttribLocation(programHandle, "vertPos"); | |
final int colorHandle = GLES20.glGetAttribLocation(programHandle, "vertColor"); | |
GLES20.glEnableVertexAttribArray(positionHandle); | |
vertexBuffer.rewind(); | |
GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexBuffer); | |
GLES20.glEnableVertexAttribArray(colorHandle); | |
colorBuffer.rewind(); | |
GLES20.glVertexAttribPointer(colorHandle, 4, GLES20.GL_FLOAT, false, 16, colorBuffer); | |
indexBuffer.rewind(); | |
GLES20.glDrawElements(GLES20.GL_TRIANGLES, vertCount, GLES20.GL_UNSIGNED_SHORT, indexBuffer); | |
GLES20.glDisableVertexAttribArray(positionHandle); | |
GLES20.glDisableVertexAttribArray(colorHandle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment