mk12
mk12

Reputation: 26354

Flipping OpenGL texture

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?

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

+ (TextureImageRef)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);

    GLuint width = CGImageGetWidth(image);
    GLuint 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);

    return TextureImageCreate(width, height, data);
}

Where TextureImage is a struct that has a height, width and void *data.

Right now I'm just playing around with OpenGL, but later I want to try making a simple 2d game. I'm using Cocoa for all the windowing and Objective-C as the language.

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

Thanks.

Upvotes: 9

Views: 11109

Answers (2)

mk12
mk12

Reputation: 26354

Jordan Lewis pointed out CGContextDrawImage draws image upside down when passed UIImage.CGImage. There I found a quick and easy solution: Before calling CGContextDrawImage,

CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0f, -1.0f);

Does the job perfectly well.

Upvotes: 0

SigTerm
SigTerm

Reputation: 26409

Any of those:

Flip texture during the texture load,
OR flip model texture coordinates during model load OR set texture matrix to flip y (glMatrixMode(GL_TEXTURE)) during render.

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

Depends on how you are going to render text.

Upvotes: 4

Related Questions