1

我有一个 GLKView,我尝试在其中绘制几个立方体,然后从视图创建纹理并将它们映射到立方体上。但是,当我在视网膜设备上启动应用程序时,纹理大小正确,但看起来很糟糕。我试图将 GLKView 的 contentScaleFactor 设置为主屏幕的比例 - 无济于事。我还尝试将缓冲区尺寸乘以比例,这导致纹理看起来很清晰,但只有原始大小的 1/4...

事不宜迟,我可以向您展示我所做的(没有上述乘法):

GLK视图

- (void)setupGL {

    UIScreen *mainScreen = [UIScreen mainScreen];
    const CGFloat scale = mainScreen.scale;
    self.contentScaleFactor = scale;
    self.layer.contentsScale = scale;

    glGenFramebuffers(1, &defaultFrameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);

    glGenRenderbuffers(1, &depthBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);

    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, self.bounds.size.width, self.bounds.size.height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

    glGenRenderbuffers(1, &colorBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, self.bounds.size.width, self.bounds.size.height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ARRAY, GL_RENDERBUFFER, colorBuffer);

    glEnable(GL_DEPTH_TEST);

}

在这里我加载纹理

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

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

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

// upload to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,
             GL_RGBA,
             cV.bounds.size.width, cV.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);
4

1 回答 1

0

这个问题的答案可以在这里找到

如何创建适用于 Retina 显示而不浪费常规显示空间的 CGBitmapContext?

我基本上所做的是将纹理和缓冲区乘以屏幕的比例因子,因为这只产生了大小为 1/4 的纹理,所以我也必须将上下文乘以比例因子

CGContextScaleCTM(context, scaleFactor, scaleFactor);

于 2013-06-30T12:56:10.980 回答