OpenGL ES纹理都是黑色的

时间:2015-10-01 11:10:44

标签: ios opengl-es

我通过以下方式创建了8个纹理:

GLuint textures[8];
glGenTextures(8, textures);
for (int i=0; i<num; i++) {
    NSString *fileName = [NSString stringWithFormat:@"image0%d", i + 1];

    CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;
    if (!spriteImage) {
        NSLog(@"Failed to load image %@", fileName);
        exit(1);
    }

    size_t kwidth = CGImageGetWidth(spriteImage);
    size_t kheight = CGImageGetHeight(spriteImage);

    GLubyte * spriteData = (GLubyte *) calloc(kwidth*kheight*4, sizeof(GLubyte));

    glBindTexture(GL_TEXTURE_2D, textures[i]);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kwidth, kheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glGenerateMipmap(GL_TEXTURE_2D);
}

但是要获得所有黑色纹理:

enter image description here

我怎么解决这个问题?

1 个答案:

答案 0 :(得分:2)

GLubyte * spriteData = (GLubyte *) calloc(kwidth*kheight*4, sizeof(GLubyte));

创建一个用零填充的GLubyte数组。由于您从不在任何地方修改spriteData,因此纹理会填充为黑色的零。