这个问题是对
the original question推荐的Matic Oblak

设置并渲染FBO后,如何将渲染的图像提供回默认渲染缓冲区并以缩放版本显示。

-(void)setupFBO {
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
}

首先渲染到FBO:
-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{

glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport (0, 0, 160, 144);
glClearColor (1.0f, 0.0f, 0.0f, 0.5f);
glClear (GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glViewport(0, 0, 160, 144);

glBindVertexArrayOES(_vertexArray);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

如果我取消fbo绑定的注释,则正方形将正确显示-因此,我认为VBO和VAO不会出现问题。

尝试将结果显示回放大到适合屏幕的主视图:
[((GLKView *) self.view) bindDrawable];
glViewport (0, 0, view.frame.size.width, view.frame.size.height);
glClearColor (0.0f, 1.0f, 0.0f, 0.5f);
glClear (GL_COLOR_BUFFER_BIT);

//vb2 has the scaled vertex coordinates
glBindBuffer(GL_ARRAY_BUFFER, vb2);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);

//make sure we are not redrawing the same thing? the texture should be
//a colored cube based on what the fbo rendered.
glDisableVertexAttribArray(GLKVertexAttribColor);

//create a buffer for the texture coordinates
//should be moved in setup phase, but left in for testing
glGenBuffers(1, &texBuffer);
glBindBuffer(GL_ARRAY_BUFFER, texBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*8, squareTex, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, 0);
//bind to the fbo texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

我已阅读并遵循的一些教程。
  • OpenGL FBO Example
  • Songho OGL FBO
  • RTT

  • 最终,我希望在模拟器的整个宽度(保持高度比)中显示(160x144像素)的模拟GB屏幕。

    我先从一个小正方形开始,然后尝试将其扩展以实现复古正方形像素效果。

    最佳答案

    要获得拉伸效果,您要做的就是绘制一个带有纹理的全屏矩形。如果不使用矩阵,则只需将顶点位置数据设置在[-1,1]范围内,因此对于三角带:

    {-1, -1,
      1, -1,
     -1,  1,
      1,  1}
    

    纹理坐标应取决于原始纹理视口与纹理尺寸的比率。对于160x144的示例,最近的足够大的纹理大小数据将是256x256,这将导致ratio = (160/256, 144/256),因此坐标为
    {.0,       .0,
    ratio.x,   .0,
    .0,        ratio.y,
    ratio.x,   ratio.y}
    

    如果未显示结果,则将需要查找可能随时存在的问题。首先,我将首先尝试以纯色绘制此坐标,以查看矩形是否全屏绘制。如果是这样,则问题最有可能是来自FBO的纹理。如果我假设您正在使用Xcode,则最好的工具是创建一个帧快照(该工具位于左下角),它将向您显示帧的任何点的所有调用和所有纹理。有了它,您可能会看到纹理是否正确绘制。

    不要忘记在附件glGetError之后检查openGL错误(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)并检查FBO缓冲区的状态。

    这是我的一个项目中创建FBO的一种方法。这可能对您有用。
        glGenFramebuffers(1, &_frameBuffer);
        glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
    
        glGenRenderbuffers(1, &_renderBuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer);
    
        glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, texture.textureWidth, texture.textureHeight);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderBuffer);
    
        [texture bind];
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.textureID, 0);
    
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_bufferWidth);
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_bufferHeight);
    
        [GLGlobalTools checkFramebufferStatus];
        [GLGlobalTools checkError];
    
        _bufferWidthf = _bufferWidth;
        _bufferHeightf = _bufferHeight;
    
        _surfaceWidth = (GLint)texture.imageWidth;
        _surfaceHeight = (GLint)texture.imageHeight;
    

    关于ios - OpenGL ES FBO缩放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38071480/

    10-12 01:38