【问题标题】:Opengl ES 2.0 - Load PNG with GLKit instead of OpenGL APIOpenGL ES 2.0 - 使用 GLKit 而不是 OpenGL API 加载 PNG
【发布时间】:2014-01-03 04:26:19
【问题描述】:

我有一段代码使用 glTexImage2D 加载纹理,如下所示:

// to test texturing
CGImageRef imageRef = [[UIImage imageNamed:@"Blob.png"] CGImage];
int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);
GLubyte* textureData = (GLubyte *)malloc(width * height * 4); // if 4 components per pixel (RGBA)

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(textureData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

glBindTexture(GL_TEXTURE_2D, 0);

我用这个替换了代码,代码不会抛出任何错误,而是显示黑色图像。

NSError *error;
GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Blob.png" ofType:Nil]
                                                               options:Nil
                                                                 error:&error]; assert(!error);

//bind the texture to texture unit 0
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(texture.target, texture.name);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glEnable(texture.target);

glBindTexture(GL_TEXTURE_2D, 0);

可能是什么问题?

我要移植的项目是https://github.com/glman74/simpleFBO

【问题讨论】:

  • GLKTextureLoader 调用之后的代码看起来很可疑。如果除glBindTexture(texture.target, texture.name) 之外的所有内容都删除会怎样?
  • @rickster 我删除了 GLKTextureLoader 之后的行,并在 glkView:drawInRect: 方法中添加了对 glBindTexture 的调用,这似乎解决了问题

标签: opengl-es-2.0 glkit render-to-texture texturing


【解决方案1】:

pathForResource:@"Blob.png" ofType:Nil

应该是 pathForResource:@"Blob" ofType:@"png"

如果是 Nil,大多数时候你应该使用 nil 来代替

【讨论】:

  • 我进行了更改,但仍然得到黑色图像
  • 这就是我能说的范围。目前,其余的 opengl 部分在我的 iPhone 上超出了我的范围。对不起。
【解决方案2】:

我就是这样解决的:

这在 OpenGL 初始化代码中

NSError *error;
GLKTextureInfo *texture = [GLKTextureLoader
     textureWithContentsOfFile:
        [[NSBundle mainBundle] pathForResource:@"Blob.png" ofType:Nil]
     options:Nil
     error:&error];

这在渲染方法中,在本例中为glkView:drawInRect:

glBindTexture(GL_TEXTURE_2D, 0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多