Android Opengl ES2, цвет вместо растрового изображения в текстуре - PullRequest
1 голос
/ 02 августа 2020

Мне нужен светодиодный куб 8x8x8 в приложении Android. Я нашел руководство по OpenGl, в котором есть этот светодиодный куб, но он использует растровое изображение на текстуре. Могу я изменить его на простой цвет? Помощник по текстурам выглядит так:

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]
}

1 Ответ

1 голос
/ 02 августа 2020
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]
}
...