2014-10-30 1 views
0

이 코드를 책에서 곧바로 복사했으며 문제가 무엇인지 알 수 없습니다. glTexImage2d가 호출 된 마지막 행에서 EXC BAD ACCESS 런타임 오류가 발생합니다. 나는 그것이 pixelData 포인터에서 오는 것이라고 생각한다. 그러나 나는 그 문제가 무엇인지 모른다. 밖에있는 천재라면 내가 그것을 크게 고맙게 여길 수있다. 나는 바보 pixelData에서 & 기호를 포함하기 때문에이 고정이 목표가 무엇인지 알 수 없음 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

엑스 코드 (6)을 사용하고 있습니다.

관련 문제