Convert an UIImage in a texture
Asked Answered
S

3

7

In my opengl project I need to convert an UIImage in texture; what's the way to do it? Can you help me?

Sibbie answered 16/1, 2013 at 16:18 Comment(0)
A
12

I haven't test the following but i will decompose the conversion in 3 steps:

  1. Extract info for your image:

    UIImage* image = [UIImage imageNamed:@"imageToApplyAsATexture.png"];
    CGImageRef imageRef = [image CGImage];
    int width = CGImageGetWidth(imageRef);
    int height = CGImageGetHeight(imageRef);
    
  2. Allocate a textureData with the above properties:

    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. Set-up your 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:

Read this tut; everything is explained from the conversion of one image to a texture and applying a texture in an iOS environment.

Anett answered 16/1, 2013 at 16:44 Comment(10)
I have two question: my image is 1024x768 where I should set these valiues? and finally what's my texture?Sibbie
if you know your images dimensions you don't need to extract the width and height just set it directlyAnett
your 2d texture corresponds to the gl object you are creating when invoking glTexImage2D, you can access it through it name: textureIDAnett
why context is a variable that is unused? and you can tell me how to set this texture ad backgroung in glpaint?Sibbie
mmmm I'n not expert of openGL can you help me and tell how to set this texture in glPaint texture?Sibbie
did u check the link for the tut?Anett
texture tutorial link is broken, any info on finding a mirror?Highly
I believe you can use this one now: blog.csdn.net/opengl_es/article/details/25240061Anett
is there any way to do this in swift3? do you have any links?Blankbook
@Anett textureID always zero can you help me ?Shermy
T
1

Here is swift version of getting texture out of an 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

}

Note: Need to keep in mind that Core Graphics flips images when we load them in.

Tobey answered 16/1, 2018 at 7:34 Comment(1)
textName always zero why ?Shermy
G
0

Another way of doing this using the GLKit framework:

//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 is The OpenGL context’s name for the texture.

Goring answered 13/11, 2015 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.