[英]Flipping OpenGL texture
當我正常加載圖像中的紋理時,由於OpenGL的坐標系統,它們是顛倒的。 翻轉它們的最佳方法是什么?
這是我在Utilities.m文件(Objective-C)中用來加載png紋理的方法:
+ (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);
}
其中TextureImage是具有height,width和void *數據的結構。
現在我只是玩OpenGL,但后來我想嘗試制作一個簡單的2D游戲。 我使用Cocoa進行所有窗口,使用Objective-C作為語言。
另外,我想知道另一件事:如果我做了一個簡單的游戲,將像素映射到單位,是否可以設置它以使原點位於左上角(個人喜好),或者我會運行處理其他事情的問題(例如文本呈現)?
謝謝。
任何這些:
在紋理加載期間翻轉紋理,
或在模型加載期間翻轉模型紋理坐標或在渲染期間設置紋理矩陣以翻轉y(glMatrixMode(GL_TEXTURE))。
另外,我想知道另一件事:如果我做了一個簡單的游戲,將像素映射到單位,是否可以設置它以使原點位於左上角(個人喜好),或者我會運行處理其他事情的問題(例如文本呈現)?
取決於您將如何呈現文本。
Jordan Lewis指出CGContextDrawImage在傳遞UIImage.CGImage時會將圖像顛倒過來 。 在那里我找到了一個快速簡單的解決方案:在調用CGContextDrawImage之前,
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0f, -1.0f);
這項工作完美無缺。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.