渲染到纹理:始终是黑色纹理

时间:2013-03-14 17:59:51

标签: ios opengl-es

我正在尝试渲染纹理并随后使用纹理,但由于某种原因它不起作用。

如Apple documentation中所述,

首先我将fbo分配给纹理..

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

然后我创建纹理并将其附加到那个fbo ..

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_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 );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_OES, 64, 64, 0, GL_RGBA,
             GL_UNSIGNED_BYTE, NULL);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                       texture, 0);

然后创建用于绘图的fbo ..

glGenFramebuffers(1, &draw_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, draw_framebuffer);
glGenRenderbuffers(1, &_colorRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);

稍后在渲染循环部分..

glBindFramebuffer(GL_FRAMEBUFFER, read_framebuffer);

(...off-screen drawing the scene here...)

glBindFramebuffer(GL_FRAMEBUFFER, draw_framebuffer);

绑定应该包含我的屏幕外渲染图像的纹理..

glBindTexture(GL_TEXTURE_2D, texture);

(..draw the scene with texture applied here...)

然而,由于某种原因,纹理总是黑色的..这里有什么我不知道的吗?

1 个答案:

答案 0 :(得分:2)

glTexImage2D的{​​{1}}交换GL_RGBA8_OES。我可以看到你从苹果文档中获取这些参数..(没有评论..)无论如何关于你的问题的评论,正如@prideout所指出的,你应该总是检查帧缓冲状态。在您的情况下,它返回GL_RGBA