2

我有一个 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);
4

2 回答 2

0

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

在使用视网膜分辨率时,您应该完全加载不同的纹理,因为屏幕对于相同的坐标具有双倍分辨率。这就是为什么您会看到模糊的纹理(缩放 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);
于 2013-01-04T15:28:06.583 回答
0

找到解决方案!

请看一下这个问题:

用于视网膜显示的 OpenGL ES 2.0 纹理?

于 2013-03-04T15:50:40.583 回答