翻转OpenGL纹理 [英] Flipping OpenGL texture
问题描述
当我正常加载图像中的纹理时,由于OpenGL的坐标系统,它们是颠倒的。翻转它们的最佳方法是什么?
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);
- 反向映射纹理的y坐标
- 手动垂直翻转图像文件(在Photoshop中)
- 以编程方式翻转它们加载后(我不知道怎么做)
- 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)
这是我用来加载png纹理的方法,在我的工具中.m文件(Objective-C):
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);
}
其中TextureImage是具有height,width和void *数据的结构。
Where TextureImage is a struct that has a height, width and void *data.
现在我只是玩OpenGL,但后来我想尝试制作一个简单的2D游戏。我正在使用Cocoa进行所有窗口并使用Objective-C作为语言。
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)?
谢谢。
推荐答案
Jordan Lewis指出 CGContextDrawImage在传递UIImage.CGImage时将图像颠倒绘制。在那里我找到了一个快速简单的解决方案:在调用CGContextDrawImage之前,
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);
这项工作是否完美。
这篇关于翻转OpenGL纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!