【问题标题】:Map UIView onto a face for iPhone 5's retina display将 UIView 映射到 iPhone 5 的视网膜显示屏上
【发布时间】:2013-01-04 14:34:51
【问题描述】:

我有一个 UIView,我想将它映射到我的形状的面上。所有这些都有效,但我希望它可以在 iPhone 5 上使用视网膜图形。我将我的 openGL 层的 contentScale 属性设置为 2.0,但结果还是有点模糊。所以尺寸是正确的,就好像我在视网膜显示器上使用非视网膜图形来制作图像一样。所以我要做的“唯一”事情就是告诉 iPhone 这必须是像素的两倍。我尝试了 DarthMike 建议的解决方案,不幸的是无济于事。

我在这里初始化并将其添加到我的视图中:

    MyOpenGLView *view = [[MyOpenGLView alloc] initWithFrame:CGRectMake(0, dY, 230,230) context:context];
    view.contentScaleFactor = 2.0;
    view.layer.contentsScale = 2.0;
    view.delegate = view;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat16;
    [view setupGL];
    [self.view addSubview:view];

这里我将视图渲染到上下文中并将其添加为纹理

    MyViewToBeTheTexture *textureView = [[MyViewToBeTheTexture alloc]initWithFrame:CGRectMake(0, 0, 230, 230)];

    self.effect.texture2d0.enabled = true;

    // make space for an RGBA image of the view
    GLubyte *pixelBuffer = (GLubyte *)malloc(
                                             4 *
                                             textureView.bounds.size.width *
                                             textureView.bounds.size.height);

    // create a suitable CoreGraphics context
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context =
    CGBitmapContextCreate(pixelBuffer,
                          textureView.bounds.size.width, textureView.bounds.size.height,
                          8, 4*textureView.bounds.size.width,
                          colourSpace,
                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colourSpace);

    // draw the view to the buffer
    [textureView.layer renderInContext:context];

    // upload to OpenGL
    glTexImage2D(GL_TEXTURE_2D, 0,
                 GL_RGBA,
                 textureView.bounds.size.width, textureView.bounds.size.height, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);



    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);



    glGenBuffers(1, &texArray);
    glBindBuffer(GL_ARRAY_BUFFER, texArray);
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0,0);
    glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoords), TexCoords, GL_STATIC_DRAW);

【问题讨论】:

  • 什么不起作用?更好地描述问题所显示的内容将有助于人们回答您的问题。
  • 对不起@DarthMike。我只是想补充一些信息。图像看起来很模糊,我添加到视图中的任何 UILabel 也很模糊......
  • 这是否也适用于 iPhone 4 视网膜? (不模糊)因为 iPhone 5 的内容比例应该相同,因为屏幕在 y 中只有更多像素。
  • 在 iPhone 4 (retina) 上看起来几乎一样。
  • @DarthMike 奇怪的是,当我将 GL_TEXTURE_WRAP_S 和/或 GL_TEXTURE_WRAP_T 的参数设置为 GL_REPEAT 时,它根本不显示任何内容。

标签: opengl-es retina-display iphone-5


【解决方案1】:

我的猜测是你需要更新你的纹理以获得双倍分辨率。

在使用视网膜分辨率时,您应该完全加载不同的纹理,因为屏幕对于相同的坐标具有双倍分辨率。这就是为什么您会看到模糊的纹理(缩放 2 倍)。您必须通过阅读 [UIScreen mainScreen].scale 为视网膜创建具有两倍 x 和 y 大小的缓冲区。我给你一个暂定代码,可能因为我无法测试它而无法工作:

MyView *view = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];

self.effect.texture2d0.enabled = true;

//Pixel to coord scale
GLFloat coordToPixScale = [UIScreen mainScreen].scale;

// make space for an RGBA image of the view
GLubyte *pixelBuffer = (GLubyte *)malloc(
                                         4 *
                                         view.bounds.size.width * coordToPixScale *
                                         view.bounds.size.height * coordToPixScale);

// create a suitable CoreGraphics context
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context =
CGBitmapContextCreate(pixelBuffer,
                      view.bounds.size.width*coordToPixScale, view.bounds.size.height*coordToPixScale,
                      8, 4*view.bounds.size.width *coordToPixScale,
                      colourSpace,
                      kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colourSpace);

// draw the view to the buffer
[view.layer renderInContext:context];

// upload to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,
             GL_RGBA,
             view.bounds.size.width * coordToPixScale, view.bounds.size.height * coordToPixScale, 0,
             GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);

BOOL repeatX = NO;
BOOL repeatY = NO;

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeatX ? GL_REPEAT
                       : GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeatY ? GL_REPEAT
                       : GL_CLAMP_TO_EDGE);



glGenBuffers(1, &texArray);
glBindBuffer(GL_ARRAY_BUFFER, texArray);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0,0);
glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoords), TexCoords, GL_STATIC_DRAW);

【讨论】:

  • 我唯一的猜测是图层中的 contentScale 应该是 1.0。
  • 现在它看起来是以前的两倍......所以我认为你的答案在它看起来如何(近乎完美)方面起到了作用,但它是大小的一半......但是 contentScale 有保持 2.0,因为当我将其设置为 1.0 时,它看起来绝对可怕
【解决方案2】:

找到解决方案!

请看一下这个问题:

OpenGL ES 2.0 textures for retina display?

【讨论】:

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