当我正常从图像加载纹理时,由于 OpenGL 的坐标系统,它们是颠倒的。翻转它们的最佳方法是什么?
- glScalef(1.0f, -1.0f, 1.0f);
- 反向映射纹理的 y 坐标
- 手动垂直翻转图像文件(在 Photoshop 中)
- 加载后以编程方式翻转它们(我不知道如何)
这是我在 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 是一个具有高度、宽度和 void *data 的结构。
现在我只是在玩 OpenGL,但稍后我想尝试制作一个简单的 2d 游戏。我使用 Cocoa 进行所有窗口操作,并使用 Objective-C 作为语言。
另外,我想知道的另一件事是:如果我制作了一个简单的游戏,将像素映射到单位,是否可以将其设置为原点位于左上角(个人偏好),或者我会运行遇到其他问题(例如文本渲染)?
Thanks.