我直接从书中复制了这段代码,我不知道问题是什么。我在调用glTexImage2d的最后一行得到一个EXC BAD ACCESS运行时错误。我认为它来自pixelData指针。但我不知道问题是什么。如果有任何天才可以帮助我会非常感激。我正在使用Xcode 6。
#import "TextureHelper.h"
@implementation TextureHelper
-(void) createTextureFromImage: (NSString *) picName{
UIImage *pic = [UIImage imageNamed:picName];
int scaleFactor = 1;
float iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if(iOSVersion >= 4.0){
if(pic.scale >= 2){
scaleFactor = pic.scale;
}
}
if(pic){
//set texture dimentions
width = pic.size.width * scaleFactor;
height = pic.size.height * scaleFactor;
/*
if ((width & (width-1)) !=0 || (height & height-1)) != 0 || width> 2048 || height > 2048) {
NSLog(@"ERROR:%@ width and/or height is not power of 2 or is > 2048!", picName);
}
*/
GLubyte *pixelData = [self generatePixelDataFromImage:pic];
[self generateTexture:pixelData];
int memory = width*height*4;
NSLog(@"%@, Size:%i KB, ID:%i", picName, memory/1024, textureID);
free(pixelData);
}else{
NSLog(@"ERROR:%@ not found, texture not created.",picName);
}
}
-(GLubyte *) generatePixelDataFromImage: (UIImage *) pic{
GLubyte *pixelData = (GLubyte *) calloc(width * height *4, sizeof(GLubyte));
CGColorSpaceRef imageCS = CGImageGetColorSpace(pic.CGImage);
CGBitmapInfo bitmapInfo = (CGBitmapInfo) kCGImageAlphaPremultipliedLast;
CGContextRef gc = CGBitmapContextCreate(pixelData, width, height, 8, width*4, imageCS, bitmapInfo);
CGContextDrawImage(gc, CGRectMake(0, 0, width, height), pic.CGImage);
CGContextRelease(gc);
return pixelData;
}
-(void) generateTexture: (GLubyte * ) pixelData{
//Create GL Texture
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_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, &pixelData);
}
@end
答案 0 :(得分:0)
我解决了这个问题,因为我愚蠢地包括了& pixelData中的符号。