我在Android应用程序中需要一个8x8x8 LED立方体。我找到了一个OpenGl教程,其中有一个led立方体,但它在纹理上使用了位图。我可以将其更改为简单的颜色吗?纹理助手是这样的:
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]
}
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]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句