2013-01-16 14 views

risposta

10

non ho testare il seguente ma io si decompone la conversione in 3 fasi: informazioni

  1. estratto per l'immagine:

    UIImage* image = [UIImage imageNamed:@"imageToApplyAsATexture.png"]; 
    CGImageRef imageRef = [image CGImage]; 
    int width = CGImageGetWidth(imageRef); 
    int height = CGImageGetHeight(imageRef); 
    
  2. assegnare un textureData con le proprietà di cui sopra:

    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. Imposta la tua texture:

    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); 
    

EDIT:

Leggi this tut; tutto è spiegato dalla conversione di un'immagine a una trama e l'applicazione di una texture in un ambiente iOS.

+0

Ho due domande: la mia immagine è 1024x768 dove dovrei impostare questi valiues? e infine qual è la mia trama? – CrazyDev

+0

se si conoscono le dimensioni delle immagini non è necessario estrarre la larghezza e l'altezza basta impostarle direttamente – tiguero

+0

la texture 2d corrisponde all'oggetto gl che si sta creando quando si richiama glTexImage2D, è possibile accedervi tramite il nome: textureID – tiguero

0

Un altro modo per farlo utilizzando il framework 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 è il nome del contesto OpenGL per la texture.

0

Ecco la versione veloce di ottenere tessitura fuori da un 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 

}

. Nota: Necessità di tenere a mente che Core Graphics lancia immagini quando li carichiamo in