alexrnov
alexrnov

Reputation: 2534

Cubemap texture with bitmap in OpenGL ES 2.0/3.0

When I create a cubemap texture with simple colors, this works well:

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

But when I try to create a cubemap texture with bitmap:

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

Then the object turns black. Someone may suggest why the cubemap with the bitmap does not work (black color)?

Thanks for any comment/answer.

Upvotes: 1

Views: 361

Answers (1)

Columbo
Columbo

Reputation: 6766

Textures for Cubemaps need to be square. As mentioned in the comments, the bitmap used was not square.

From glTexImage2D reference (GLUtils.texImage2D is a convenience wrapper around glTexImage2D)

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.

Upvotes: 1

Related Questions