views:

247

answers:

2

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);
  • mapping the y coordinates of the textures in reverse
  • 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):

+ (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.

+3  A: 

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.

SigTerm
A: 

Jordan Lewis pointed out http://stackoverflow.com/questions/506622/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.

Mk12