Nel mio progetto opengl ho bisogno di convertire un UIImage in texture; qual è il modo per farlo? Potete aiutarmi?Converti un UIImage in una trama
7
A
risposta
10
non ho testare il seguente ma io si decompone la conversione in 3 fasi: informazioni
estratto per l'immagine:
UIImage* image = [UIImage imageNamed:@"imageToApplyAsATexture.png"]; CGImageRef imageRef = [image CGImage]; int width = CGImageGetWidth(imageRef); int height = CGImageGetHeight(imageRef);
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);
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
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
Problemi correlati
- 1. Converti UIImage in NSData e converti in UIImage in Swift?
- 2. Converti UIImage in CVImageBufferRef
- 3. Converti UIImage in CVPixelBufferRef
- 4. Converti UIImage in CMSampleBufferRef
- 5. Converti UIView Layer in UIImage
- 6. Converti i contenuti di UIWebview in una UIImage
- 7. Converti trama OpenGL in OpenCV Mat
- 8. Converti UIImage in array di byte in swift
- 9. Inserire una trama in miniatura all'interno di una trama
- 10. Tagliare un quadrato traslucido in una trama
- 11. Animate un UIBezierPath che ritaglia una UIImage
- 12. OpenGL - Rendering in una trama
- 13. Come copiare un UIImage?
- 14. Come visualizzare una UIImage in Swift
- 15. Converti un CFStringRef in una stringa C?
- 16. Ruby Converti una stringa in un file
- 17. disegna una trama pixelata
- 18. Come creare un UIImage nero?
- 19. come convertire un CVImageBufferRef in UIImage
- 20. Regola UIImage in UIImageView
- 21. Punti trama su una sfera in R
- 22. Creazione di una trama in modo procedurale
- 23. etichettare una trama in incrementi di pi
- 24. Get XLIM da una trama in R
- 25. Come salvare una trama Seaborn in un file
- 26. Shader pixel per proiettare una trama in un quadrilatero arbitario
- 27. Produrre una trama raster in R
- 28. Come si autocorre un UIImage?
- 29. Come posso convertire FBProfilePictureView in un UIImage?
- 30. aggiungi UIImage in CALayer
Ho due domande: la mia immagine è 1024x768 dove dovrei impostare questi valiues? e infine qual è la mia trama? – CrazyDev
se si conoscono le dimensioni delle immagini non è necessario estrarre la larghezza e l'altezza basta impostarle direttamente – tiguero
la texture 2d corrisponde all'oggetto gl che si sta creando quando si richiama glTexImage2D, è possibile accedervi tramite il nome: textureID – tiguero