2013-10-17 25 views
-1

我試圖將.png文件作爲紋理加載到我的多維數據集。將.png文件作爲紋理加載到多維數據集ios

[self loadTexture:&myTexture fromFile:@"my_png.png"]; 
glEnable(GL_TEXTURE_2D); 
glActiveTexture(GL_TEXTURE); 

這是一個函數來加載它,但不幸它不起作用。

- (void)loadTexture:(GLuint *)newTextureName fromFile:(NSString *)fileName { 
// Load image from file and get reference 
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]]; 
CGImageRef imageRef = [image CGImage]; 

if(imageRef) { 
    // get width and height 
    size_t imageWidth = CGImageGetWidth(imageRef); 
    size_t imageHeight = CGImageGetHeight(imageRef); 

    GLubyte *imageData = (GLubyte *)malloc(imageWidth * imageHeight * 4); 
    memset(imageData, 0, (imageWidth * imageHeight * 4)); 

    CGContextRef imageContextRef = CGBitmapContextCreate(imageData, imageWidth, imageHeight, 8, imageWidth * 4, CGImageGetColorSpace(imageRef), kCGImageAlphaPremultipliedLast); 

    // Make CG system interpret OpenGL style texture coordinates properly by inverting Y axis 
    CGContextTranslateCTM(imageContextRef, 0, imageHeight); 
    CGContextScaleCTM(imageContextRef, 1.0, -1.0); 

    CGContextDrawImage(imageContextRef, CGRectMake(0.0, 0.0, (CGFloat)imageWidth, (CGFloat)imageHeight), imageRef); 

    CGContextRelease(imageContextRef); 

    glGenTextures(1, newTextureName); 

    glBindTexture(GL_TEXTURE_2D, *newTextureName); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); 

    free(imageData); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 
    } 
} 

你有什麼想法解決這個問題嗎?

+0

歡迎來到Stack Overflow!通過更詳細地描述您的問題,您會得到更多/更好的答案。 *如何*它不起作用?與此同時,您可能需要查看使這一切變得更容易的'GLKTextureLoader'類。 – rickster

回答

0
 Class loaderClass = (NSClassFromString(@"GLKTextureLoader")); 
     if (loaderClass != nil) 
     { 
      NSError* error = nil; 

      NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys: 
       [NSNumber numberWithBool: mipmap_levels > 0], GLKTextureLoaderGenerateMipmaps, 
       [NSNumber numberWithBool:YES], GLKTextureLoaderApplyPremultiplication, 
       nil 
      ]; 

      GLKTextureInfo* info = [loaderClass textureWithContentsOfFile: path.NSStringValue() options: options error: &error]; 
      if (info && !error) 
      {} 

如果信息不爲空且沒有錯誤,那麼信息將包含所有您需要的信息。

相關問題