繁体   English   中英

翻转OpenGL纹理

[英]Flipping OpenGL texture

当我正常加载图像中的纹理时,由于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是具有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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM