繁体   English   中英

GLKit中的纹理映射不仅适用于设备

[英]Texture mapping in GLKit is not working only in devices

我使用此链接中的代码来映​​射人脸的纹理。 此代码使用GLKIT渲染图像。 代码在模拟器中工作正常,但如果我在设备中运行它无法正常工作,则代码相同。 以下是图像的屏幕截图,它在设备中工作,而不是在我的ipad中。

我用来加载纹理的代码:

- (void)loadTexture:(UIImage *)textureImage
{
    glGenTextures(1, &_texture);
    glBindTexture(GL_TEXTURE_2D, _texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    CGImageRef cgImage = textureImage.CGImage;
    float imageWidth = CGImageGetWidth(cgImage);
    float imageHeight = CGImageGetHeight(cgImage);
    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA,  
    GL_UNSIGNED_BYTE, CFDataGetBytePtr(data));
}

模拟器的图像:

在模拟器中输出

设备中的相同代码提供以下输出:

设备输出

有什么东西我不见了?

  1. 使用Apple的内置1行方法加载 - 不要编写自己的(损坏的)实现!

    https://developer.apple.com/library/mac/#documentation/GLkit/Reference/GLKTextureLoader_ClassRef/Reference/Reference.html#//apple_ref/occ/clm/GLKTextureLoader/textureWithContentsOfFile:options:error :


  1. 或者,如果你必须自己做,你有两个可疑的部分:

    2.1。 摆脱这条线:

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    2.2。 此行使用超出范围的数字,这不是很好:

    glGenTextures(1,&_texture);


  1. 如果由于某种原因你不能使用Apple的代码(例如你想从内存中加载原始数据),这里是工作代码的复制/粘贴:

     NSData* imageData = ... // fill this yourself CGSize* imageSize = ... // fill this yourself GLuint integerForOpenGLToFill; glGenTextures(1, &integerForOpenGLToFill); glBindTexture( GL_TEXTURE_2D, integerForOpenGLToFill); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); unsigned char* pixelData = (unsigned char*) malloc( [imageData length] * sizeof(unsigned char) ); [imageData getBytes:pixelData]; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageSize.width, imageSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); 

看截图...我想知道问题是你从iPhone到iPad吗?

即视网膜到非视网膜?

在我看来,你的图像加载可能会忽略Retina比例,因此iPad上的纹理贴图是“2x太大”。

使用Apple的方法(我的另一个答案)应该自动修复,但如果没有:你可以尝试制作两个图像副本,一个是当前大小(名为“image@2x.png”),另一个是一半大小(在photoshop中调整大小) / preview.app / etc)(名为“image.png”)

我终于屈服了并切换到GLKTextureLoader。 正如亚当提到的,它非常坚固。

这是一个可能适合您的实现:

- (void)loadTexture:(NSString *)fileName
{
    NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES],
                             GLKTextureLoaderOriginBottomLeft,
                             nil];

    NSError* error;
    NSString* path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    GLKTextureInfo* texture = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
    if(texture == nil)
    {
        NSLog(@"Error loading file: %@", [error localizedDescription]);
    }

    glBindTexture(GL_TEXTURE_2D, texture.name);
}

我将很快为GitHub上的mtl2opengl项目更改它...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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