Flipping OpenGL texture

Posted by Mk12 on Stack Overflow See other posts from Stack Overflow or by Mk12
Published on 2010-06-05T18:10:30Z Indexed on 2010/06/05 18:12 UTC
Read the original article Hit count: 285

Filed under:
|
|
|
|

When I load textures from images normally, they are upside down because of OpenGL's coordinate system. What would be the best way to flip them?

  • glScalef(1.0f, -1.0f, 1.0f);
  • vertically flipping the image files manually (in Photoshop)
  • flipping them programatically after loading them (I don't know how)

This is the method I'm using to load png textures, in my Utilities.m file (Objective-C):

+ (GLuint)loadPngTexture:(NSString *)name {
    CFURLRef textureURL = CFBundleCopyResourceURL(
                                                   CFBundleGetMainBundle(),
                                                   (CFStringRef)name,
                                                   CFSTR("png"),
                                                   CFSTR("Textures"));
    NSAssert(textureURL, @"Texture name invalid");

    CGImageSourceRef imageSource = CGImageSourceCreateWithURL(textureURL, NULL);
    NSAssert(imageSource, @"Invalid Image Path.");
    NSAssert((CGImageSourceGetCount(imageSource) > 0), @"No Image in Image Source.");
    CFRelease(textureURL);

    CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
    NSAssert(image, @"Image not created.");
    CFRelease(imageSource);

    NSUInteger width = CGImageGetWidth(image);
    NSUInteger height = CGImageGetHeight(image);

    void *data = malloc(width * height * 4);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSAssert(colorSpace, @"Colorspace not created.");

    CGContextRef context = CGBitmapContextCreate(
                                                 data,
                                                 width,
                                                 height,
                                                 8,
                                                 width * 4,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
    NSAssert(context, @"Context not created.");

    CGColorSpaceRelease(colorSpace);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    CGImageRelease(image);
    CGContextRelease(context);

    GLuint textureId;
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
    glTexImage2D(
                 GL_TEXTURE_2D,
                 0,
                 GL_RGBA,
                 width,
                 height,
                 0,
                 GL_BGRA,
                 GL_UNSIGNED_INT_8_8_8_8_REV,
                 data);
    glTexParameteri(
                    GL_TEXTURE_2D,
                    GL_TEXTURE_WRAP_S,
                    GL_CLAMP_TO_EDGE_SGIS);
    glTexParameteri(
                    GL_TEXTURE_2D,
                    GL_TEXTURE_WRAP_T,
                    GL_CLAMP_TO_EDGE_SGIS);
    glTexParameteri(
                    GL_TEXTURE_2D,
                    GL_TEXTURE_MAG_FILTER,
                    GL_LINEAR);
    glTexParameteri(
                    GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR);

    free(data);

    return textureId;
}

Also, another thing I was wondering about: If I made a simple 2d game, with pixels mapped to units, would it be alright to set it up so that the origin is in the top-left corner, or would I run in to problems with other things (e.g. text rendering)?

Thanks.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about opengl