Created
September 15, 2015 06:40
-
-
Save defHLT/846b786b8548d2806baf 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
class CubesWithVboWithStride extends Cubes { | |
final int mCubeBufferIdx; | |
CubesWithVboWithStride(float[] cubePositions, float[] cubeNormals, float[] cubeTextureCoordinates, int generatedCubeFactor) { | |
FloatBuffer cubeBuffer = getInterleavedBuffer(cubePositions, cubeNormals, cubeTextureCoordinates, generatedCubeFactor); | |
// Second, copy these buffers into OpenGL's memory. After, we don't need to keep the client-side buffers around. | |
final int buffers[] = new int[1]; | |
GLES20.glGenBuffers(1, buffers, 0); | |
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]); | |
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, cubeBuffer.capacity() * BYTES_PER_FLOAT, cubeBuffer, GLES20.GL_STATIC_DRAW); | |
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); | |
mCubeBufferIdx = buffers[0]; | |
cubeBuffer.limit(0); | |
cubeBuffer = null; | |
} | |
@Override | |
public void render() { | |
final int stride = (POSITION_DATA_SIZE + NORMAL_DATA_SIZE + TEXTURE_COORDINATE_DATA_SIZE) * BYTES_PER_FLOAT; | |
// Pass in the position information | |
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mCubeBufferIdx); | |
GLES20.glEnableVertexAttribArray(mPositionHandle); | |
GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, stride, 0); | |
// Pass in the normal information | |
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mCubeBufferIdx); | |
GLES20.glEnableVertexAttribArray(mNormalHandle); | |
GLES20.glVertexAttribPointer(mNormalHandle, NORMAL_DATA_SIZE, GLES20.GL_FLOAT, false, stride, POSITION_DATA_SIZE * BYTES_PER_FLOAT); | |
// Pass in the texture information | |
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mCubeBufferIdx); | |
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); | |
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINATE_DATA_SIZE, GLES20.GL_FLOAT, false, | |
stride, (POSITION_DATA_SIZE + NORMAL_DATA_SIZE) * BYTES_PER_FLOAT); | |
// Clear the currently bound buffer (so future OpenGL calls do not use this buffer). | |
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); | |
// Draw the cubes. | |
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, mActualCubeFactor * mActualCubeFactor * mActualCubeFactor * 36); | |
} | |
@Override | |
public void release() { | |
// Delete buffers from OpenGL's memory | |
final int[] buffersToDelete = new int[] { mCubeBufferIdx }; | |
GLES20.glDeleteBuffers(buffersToDelete.length, buffersToDelete, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment