【问题标题】:Flipping OpenGL texture翻转OpenGL纹理
【发布时间】:2010-06-05 18:10:30
【问题描述】:

当我正常从图像加载纹理时,由于 OpenGL 的坐标系,它们是颠倒的。翻转它们的最佳方法是什么?

  • glScalef(1.0f, -1.0f, 1.0f);
  • 反向映射纹理的 y 坐标
  • 手动垂直翻转图像文件(在 Photoshop 中)
  • 加载后以编程方式翻转它们(我不知道如何)

这是我用来加载 png 纹理的方法,在我的 Utilities.m 文件(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 是一个具有高度、宽度和 void *data 的结构体。

现在我只是在玩 OpenGL,但稍后我想尝试制作一个简单的 2d 游戏。我将 Cocoa 用于所有窗口,并使用 Objective-C 作为语言。

另外,我想知道的另一件事是:如果我制作了一个简单的游戏,将像素映射到单位,是否可以将其设置为原点位于左上角(个人喜好),或者我会遇到其他问题(例如文本渲染)吗?

谢谢。

【问题讨论】:

标签: objective-c opengl image macos textures


【解决方案1】:

其中任何一个:

在纹理加载期间翻转纹理,
或在模型加载期间翻转模型纹理坐标 或者在渲染期间将纹理矩阵设置为翻转 y (glMatrixMode(GL_TEXTURE))。

另外,我想知道的另一件事是:如果我制作了一个简单的游戏,将像素映射到单位,是否可以将其设置为原点位于左上角(个人喜好),或者我会遇到其他问题(例如文本渲染)吗?

取决于您将如何呈现文本。

【讨论】:

    【解决方案2】:

    乔丹刘易斯指出CGContextDrawImage draws image upside down when passed UIImage.CGImage。在那里我找到了一个快速简单的解决方案:在调用 CGContextDrawImage 之前,

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

    工作做得很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多