OpenGL ES 2.0 渲染到纹理
我正在尝试使用 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);
}
};
但是当我尝试在其上绘图并使用生成的纹理时,纹理被绘制为全黑。
如果我不将绘图代码包装在 render_tex->begin();
和 render_tex->end();
中,则所有内容都会正确绘制,导致我相信问题与上面的代码无关。
I'm trying to render to a texture using OpenGL ES 2.0, but I can't seem to make it work.
This is how I proceed:
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在尝试渲染纹理之前,请确保纹理未绑定。即使根本不使用纹理,尝试渲染到当前绑定的纹理也可能会调用未定义的行为并且不起作用。
实际上,您应该在
RenderTexture
构造函数中的glTexImage2D
之后调用glBindTexture(GL_TEXTURE_2D, 0)
,或者像您一样恢复之前绑定的纹理与固定基地运营基地 (FBO) 一起。只需确保渲染到 FBO 时tex
未绑定即可。Make sure the texture is not bound before trying to render into it. Even if not using texturing at all, trying to render into a currently bound texture may invoke undefined behaviour and just not work.
You should actually call
glBindTexture(GL_TEXTURE_2D, 0)
after theglTexImage2D
in yourRenderTexture
constructor, or maybe restore the previously bound texture, like you do with the FBO. Just make sure thetex
is not bound when you render into the FBO.这已经有一段时间了,但正如我在评论中所写,请确保您正在初始化 2 的幂纹理。
This has been a while, but as I wrote in the comments, be sure you're initializing a power of 2 texture.
我遇到了和你完全相同的问题。尝试
在
glBindTexture(GL_TEXTURE_2D, tex);
之后添加,黑色方块必须消失。I had exact the same problem as u do. Try to add
right after
glBindTexture(GL_TEXTURE_2D, tex);
and black square have to disappear.您似乎没有使用
glActiveTexture
。我建议您在每个glBindTexture(tex);
之前调用glActiveTexture(GL_TEXTURE0+tex);
,这样当您使用多个纹理时,这将为您省去很多麻烦。我猜错误出在您用于在屏幕上绘制纹理的代码中。You don't seem to make use of
glActiveTexture
. I recommend you to callglActiveTexture(GL_TEXTURE0+tex);
before eachglBindTexture(tex);
, which will save you a lot of headaches when you use more than one texture. I guess the error is in the code you use for drawing the texture on the screen.