Reputation: 843
I need a 8x8x8 LED cube in an Android application. I found an OpenGl tutorial, which has this led cube, but it uses a bitmap on texture. Could I change it to a simple color? The texture helper is like this:
fun loadTexture(context: Context, resourceId: Int): Int {
val textureHandle = IntArray(1)
GLES20.glGenTextures(1, textureHandle, 0)
if (textureHandle[0] == 0) {
throw RuntimeException("Error generating texture name.")
}
val options = BitmapFactory.Options()
options.inScaled = false // No pre-scaling
// Read in the resource
val bitmap = BitmapFactory.decodeResource(context.resources, resourceId, options)
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0])
// Set filtering
GLES20.glTexParameteri(
GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_NEAREST
)
GLES20.glTexParameteri(
GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_NEAREST
)
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0)
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle()
return textureHandle[0]
}
Upvotes: 1
Views: 140
Reputation: 2534
fun loadTexture() {
val textureId = IntArray(1)
val color = byteArrayOf(0, 0, 127)
val bufferColor = ByteBuffer.allocateDirect(3)
bufferColor.put(color).position(0)
GLES20.glGenTextures(1, textureId, 0)
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0])
GLES20.glTexImage2D(GLES30.GL_TEXTURE_2D, 0,
GLES20.GL_RGB, 1, 1, 0, GLES30.GL_RGB,
GLES20.GL_UNSIGNED_BYTE, bufferColor)
return textureId[0]
}
Upvotes: 1