2014-10-30 3 views
0

Я скопировал этот код прямо из книги, и я не знаю, в чем проблема. Я получаю ошибку времени выполнения EXC BAD ACCESS на последней строке, где вызывается glTexImage2d. Я думаю, что это происходит из указателя pixelData. Но я не знаю, в чем проблема. Если бы какие-то гении могли помочь, я бы очень признателен. Я использую Xcode 6.Не могу понять, что это за цель c Плохой доступ

#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

Я исправил это, потому что я тупо включил & символ в pixelData.

Смежные вопросы