OpenGL ES 2.0 渲染到纹理 [英] OpenGL ES 2.0 render to texture
本文介绍了OpenGL ES 2.0 渲染到纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 OpenGL ES 2.0 渲染到纹理,但我似乎无法让它工作.
I'm trying to render to a texture using OpenGL ES 2.0, but I can't seem to make it work.
我是这样进行的:
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);
}
};
但是当我尝试在其上绘制并使用生成的纹理时,纹理被绘制为全黑.
But when I try drawing on it and using the resulting texture, the texture is drawn as totally black.
如果我只是不将绘图代码包装在 render_tex->begin();
和 render_tex->end();
中,一切都会正确绘制,让我相信问题与上面的代码无关.
If I just don't wrap the drawing code in render_tex->begin();
and render_tex->end();
, everything draws correctly, leading me to believe that the problem is isolated to the code above.
推荐答案
这已经有一段时间了,但正如我在评论中所写的,请确保您正在初始化 2 的幂纹理.
This has been a while, but as I wrote in the comments, be sure you're initializing a power of 2 texture.
这篇关于OpenGL ES 2.0 渲染到纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文