2013-01-16 46 views

回答

10

我沒有測試以下,但我會分解轉化3個步驟:爲你的形象

  1. 提取信息:

    UIImage* image = [UIImage imageNamed:@"imageToApplyAsATexture.png"]; 
    CGImageRef imageRef = [image CGImage]; 
    int width = CGImageGetWidth(imageRef); 
    int height = CGImageGetHeight(imageRef); 
    
  2. 分配上述性質的textureData

    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); 
    
  3. 設置您的紋理:

    GLuint textureID;  
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    glGenTextures(1, &textureID); 
    
    glBindTexture(GL_TEXTURE_2D, textureID); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); 
    

編輯:

this tut;一切都從一個圖像轉換爲紋理並在iOS環境中應用紋理來解釋。

+0

我有兩個問題:我的圖像是1024x768,我應該設置這些值?最後是我的質感? – CrazyDev

+0

如果你知道你的圖像尺寸,你不需要提取寬度和高度直接設置它 – tiguero

+0

您的2D紋理對應於您在調用glTexImage2D時創建的GL對象,您可以通過它訪問它名稱:textureID – tiguero

0

這樣使用GLKit框架的另一種方式:

//Path to image 
NSString *path = [[NSBundle mainBundle] pathForResource:@"textureImage" ofType:@"png"]; 

//Set eaglContext 
[EAGLContext setCurrentContext:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]]; 

//Create texture 
NSError *theError;  
GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:filePath options:nil error:&theError]; 
glBindTexture(texture.target, texture.name); 

texture.name是OpenGL的背景下對紋理名。

0

這裏是獲得質地出的UIImage

func setupTexture(sourceImage: UIImage) -> GLuint { 

guard let textureImage = sourceImage.cgImage else { 
    print("Failed to load image") 
    return 0 
} 

let width = textureImage.width 
let height = textureImage.height 

/* 
it will write one byte each for red, green, blue, and alpha – so 4 bytes in total. 
*/ 

let textureData = calloc(width * height * 4, MemoryLayout<GLubyte>.size) //4 components per pixel (RGBA) 
let spriteContext = CGContext(data: textureData, 
           width: width, 
           height: height, 
           bitsPerComponent: 8, 
           bytesPerRow: width * 4, 
           space: textureImage.colorSpace!, 
           bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) 

spriteContext?.draw(textureImage, in: CGRect(x: 0, y: 0, width: width, height: height)) 

var textName = GLuint() 
glGenTextures(1, &textName) 
glBindTexture(GLenum(GL_TEXTURE_2D), textName) 
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST) 
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), 
      GLsizei(height), 0, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), textureData) 

return textName 

}的迅速版

注:需要記住的是核芯顯卡翻轉圖像時,我們在加載它們