简体   繁体   English

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

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

I used the code in this link to map textures of human faces. 我使用此链接中的代码来映​​射人脸的纹理。 This code uses GLKIT to render the images. 此代码使用GLKIT渲染图像。 Code works fine in simulator but the same code if I run in device its not working. 代码在模拟器中工作正常,但如果我在设备中运行它无法正常工作,则代码相同。 The below are the screen shots of the images where it works in device and not in my ipad. 以下是图像的屏幕截图,它在设备中工作,而不是在我的ipad中。

Code I used to Load Texture: 我用来加载纹理的代码:

- (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));
}

Image of simulator: 模拟器的图像:

在模拟器中输出

The same code in device gives the following output: 设备中的相同代码提供以下输出:

设备输出

Is There something i`m missing? 有什么东西我不见了?

  1. Use Apple's built-in 1-line method to load -- don't write your own (broken) implementation! 使用Apple的内置1行方法加载 - 不要编写自己的(损坏的)实现!

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


  1. Or, if you must do it yourself, you have two suspicious parts: 或者,如果你必须自己做,你有两个可疑的部分:

    2.1. 2.1。 Get rid of this line: 摆脱这条线:

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    2.2. 2.2。 This line uses an out-of-scope number, which isn't great: 此行使用超出范围的数字,这不是很好:

    glGenTextures(1, &_texture); glGenTextures(1,&_texture);


  1. If for some reason you cannot use Apple's code (eg you want to load raw data from in-memory image), here's a copy/paste of working code: 如果由于某种原因你不能使用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); 

Looking at the screenshots ... I wonder if the problem is that you're going from iPhone to iPad? 看截图...我想知道问题是你从iPhone到iPad吗?

ie retina to non-retina? 即视网膜到非视网膜?

Looks to me like your image loading might be ignoring the Retina scale, so that the texture map is "2x too big" on the iPad. 在我看来,你的图像加载可能会忽略Retina比例,因此iPad上的纹理贴图是“2x太大”。

Using Apple's method (my other answer) should fix that automatically, but if not: you could try making two copies of the image, one at current size (named "image@2x.png") and one at half size (resize in photoshop / preview.app / etc) (named "image.png") 使用Apple的方法(我的另一个答案)应该自动修复,但如果没有:你可以尝试制作两个图像副本,一个是当前大小(名为“image@2x.png”),另一个是一半大小(在photoshop中调整大小) / preview.app / etc)(名为“image.png”)

I finally caved in and switched to GLKTextureLoader. 我终于屈服了并切换到GLKTextureLoader。 As Adam mentions, it's pretty sturdy. 正如亚当提到的,它非常坚固。

Here's an implementation which might work for you: 这是一个可能适合您的实现:

- (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);
}

I'll change it for the mtl2opengl project on GitHub soon... 我将很快为GitHub上的mtl2opengl项目更改它...

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

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