OpenGL Texture Reflection

When I usually load textures from images, they are upside down due to the OpenGL coordinate system. What would be the best way to flip them?

  • glScalef (1.0f, -1.0f, 1.0f);
  • mapping y coordinates of textures in reverse
  • manually flip image files vertically (in Photoshop)
  • reprogram them programmatically after downloading them (I don't know how)

This is the method that I use to load png textures in the Utilities.m (Objective-C) file:

+ (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 structure that has height, width, and void * data.

Now I just play with OpenGL, but later I want to try to make a 2d game. I use Cocoa for all windows and Objective-C as a language.

In addition, one more thing that I was interested in: if I made a simple game, with pixels mapped to units, it would be nice to set it up so that the origin is in the upper left corner (personal preference) or I would come across problems with other things (e.g. for rendering text)?

Thanks.

+6
objective-c image opengl textures macos
source share
2 answers

Jordan Lewis indicated CGContextDrawImage draws the image upside down when UIImage.CGImage passed . There I found a quick and easy solution: before calling CGContextDrawImage,

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

Does it work well.

0
source share

Any of them:

Flip texture while loading texture,
OR mirror coordinates of the model texture during model loading OR set the texture matrix to switch y (glMatrixMode (GL_TEXTURE)) during rendering.

In addition, one more thing that I was interested in: if I made a simple game, with pixels mapped to units, it would be nice to set it up so that the origin is in the upper left corner (personal preference) or I would come across problems with other things (e.g. for rendering text)?

Depends on how you are going to display the text.

+4
source share

All Articles