2013-12-15 60 views
1

我有一段代码加载使用glTexImage2D这样的纹理:的OpenGL ES 2.0 - 加载PNG与GLKit代替OpenGL的API

// 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

+0

代码中的'GLKTextureLoader'电话后看起来嫌疑。如果你放弃除glBindTexture(texture.target,texture.name)之外的所有内容,会发生什么? – rickster

+0

@rickster我删除了GLKTextureLoader之后的行,并在glkView:drawInRect:方法中添加了一个glBindTexture调用,这似乎解决了问题 – rraallvv

回答

1

pathForResource:@ “Blob.png” ofType:无

应该是 pathForResource:@ “斑点” ofType:@ “PNG”

而且大多数的时候你应该为零,而不是如果无

+0

我做了更改但仍然得到黑色图像 – rraallvv

+0

这就是我可以说的范围。目前,在我的iPhone上,其余的opengl部分超出了我的视野。抱歉。 – uchuugaka

1

这是我是如何解决它使用:

这正好OpenGL的初始化代码

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

这也同样在渲染方法,在这种情况下glkView:drawInRect: