Search code examples
androidopengl-esandroid-12

Android12 opengles3.0 glTexImage2D 0x502 error


I have 5 textures, such as diffuse specular normal roughness ao, roughness and ao are 8bit jpg. I got glError 0x502 while loading rouggness/ao texture after call glTexImage2D, but other three texture is OK. And this issue just occured with android 12, android 11/10/9/8 is no error. How I can fix this issue?

code:

GLuint textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);

GLenum format;
if (nrComponents == 1) {
    format = GL_RED;
} else if (nrComponents == 3) {
    format = GL_RGB;
} else {
    format = GL_RGBA;
}

glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

Solution

  • According to https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml, GL_RED is not an acceptable value for internalFormat (parameter 3 in glTexImage2D).

    I think you need internalFormat=GL_R8 and format=GL_RED. Or you could use GL_LUMINANCE for both.