OpenGL ES 2.0/3.0 中带位图的立方体贴图纹理

Cubemap texture with bitmap in OpenGL ES 2.0/3.0

当我用简单的颜色创建立方体贴图纹理时,效果很好:

@JvmStatic
fun createSimpleTextureCubemap() {
    val textureId = IntArray(1)
    val cubeFace0 = byteArrayOf(127, 127, 127) 
    val cubeFace1 = byteArrayOf(0, 127, 0) 
    ... // create other cube faces with simple color

    val cubeFaces = ByteBuffer.allocateDirect(3)
    glGenTextures(1, textureId, 0)
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureId[0])

    cubeFaces.put(cubeFace0).position(0)
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, 
        1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, cubeFaces)

    cubeFaces.put(cubeFace1).position(0)
    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, 
        1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, cubeFaces)
    ...
    return textureId[0]
}

但是当我尝试使用位图创建立方体贴图纹理时:

@JvmStatic
fun createTextureCubemap(context: Context, rowID: Int) {
    val input = context.resources.openRawResource(rowID)
    val bitmap = BitmapFactory.decodeStream(input)

    val textureId = IntArray(1)
    glGenTextures(1, textureId, 0)
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureId[0])

    GLUtils.texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, bitmap, 0)
    GLUtils.texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, bitmap, 0)
    GLUtils.texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, bitmap, 0)
    GLUtils.texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, bitmap, 0)
    GLUtils.texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, bitmap, 0)
    GLUtils.texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, bitmap, 0)

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    return textureId[0]
}

然后对象变黑了。有人可能会提出为什么带有位图的立方体贴图不起作用(黑色)?

感谢任何 comment/answer。

立方体贴图的纹理必须是方形的。如评论中所述,使用的位图不是方形的。

来自 glTexImage2D referenceGLUtils.texImage2DglTexImage2D 的便利包装)

GL_INVALID_VALUE is generated if target is one of the six cube map 2D image targets and the width and height parameters are not equal.