sha*_*vey 13 opengl-es ios opengl-es-2.0 glkit
我正在尝试使用OpenGL ES 2.0渲染到纹理,但我似乎无法使其工作.
这就是我的进展方式:
struct RenderTexture
{
GLuint framebuffer;
GLuint tex;
GLint old_fbo;
RenderTexture(GLuint width, GLuint height)
{
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
glGenFramebuffers(1, &framebuffer);
glGenTextures(1, &tex);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
tex, 0);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
cout << status << endl; // this is not called
}
glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
}
void begin()
{
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
void end()
{
glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
}
};
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用它并使用生成的纹理时,纹理被绘制为完全黑色.
如果我只是不包含绘图代码,render_tex->begin();并且render_tex->end();所有内容都正确绘制,这让我相信问题与上面的代码隔离开来.
在尝试渲染之前,请确保未绑定纹理.即使根本不使用纹理,尝试渲染到当前绑定的纹理也可能会调用未定义的行为而无法正常工作.
你应该在你的构造函数glBindTexture(GL_TEXTURE_2D, 0)之后调用,或者恢复以前绑定的纹理,就像你对FBO一样.只需确保在渲染到FBO时未绑定.glTexImage2DRenderTexturetex
| 归档时间: |
|
| 查看次数: |
9691 次 |
| 最近记录: |