【发布时间】:2016-06-28 08:54:42
【问题描述】:
这个问题是对 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);
我阅读并遵循的一些教程。
最终,我希望模拟 GB 屏幕(160x144 像素)以模拟器的全宽显示(保持高度比)。
我从一个小正方形开始,并尝试将其拉伸以实现复古平方像素效果。
【问题讨论】:
-
您使用的是什么版本的 OpenGL ES?您的调用至少需要 ES 2.0,但也有仅在 ES 1.x 中有效的调用。
-
Es2,哪些只有1.x?我发现的大多数教程都有相同的调用。
-
glEnable(GL_TEXTURE_2D)在 ES 2.0 中无效。 -
删除并添加了 glActiveTexture。