如何解决opengl es 3 glBlitFramebuffer - 将一个纹理复制到另一个
我正在尝试使用“glBlitFramebuffer”将纹理复制到另一个纹理中,我正在处理来自 android ndk-samples 的经典 open gl 示例 hello-gl2。我更改了着色器以支持纹理,并且可以正确渲染带纹理的三角形。我还更改了 java 端请求的 opengl es 版本,以便请求 opengl-es3,并且我更新了 Cmakelists 文件以链接正确的库。调用 glBlitFramebuffer(0,tw,th,GL_COLOR_BUFFER_BIT,GL_LINEAR);
后,我收到 after blitTexture glBlitFramebuffer() glError (0x502)
错误。造成这种情况的根本原因是什么?
我使用的代码:
生成纹理:
void createTexture(gluint &tex,int width,int height,int r){
int rowlen = width * 3;
unsigned char buf[width*3*height];
for (int i=0; i<rowlen; i+=3)
for (int j=0; j<height; j++) {
int col = ((int (i/(3*r))) + (int (j/r)))%2;
buf[i+j*rowlen] = buf[i+1+j*rowlen] = buf[i+2+j*rowlen] = col * 255;
}
glGenTextures(1,&tex);
glBindTexture(GL_TEXTURE_2D,tex);
glTexImage2D(GL_TEXTURE_2D,GL_RGB,width,height,GL_UNSIGNED_BYTE,buf);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
LOGI("DEBUG C++ Texture created [%d]",tex);
}
设置帧缓冲区:
bool SetupFramebuffer(gluint tex,int height)
{
if (fb == 0)
{
glGenFramebuffers(1,&fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,fb);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,tex,0);
GLenum status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
LOGE("Incomplete frame buffer object!");
return false;
}
LOGI("Created FBO %d for texture %d.",fb,tex);
}
return true;
}
在 setupGraphics 函数中构建纹理数组:
createTexture(tex,6);
for (int i = 0; i<25; i++) createTexture(aniTex[i],3+i);
SetupFramebuffer(tex,th);
最后对帧缓冲区进行 blit:
const GLenum attachment[1] = { GL_COLOR_ATTACHMENT0 };
void blitTexture() {
static int time = 0;
glBindFramebuffer(GL_FRAMEBUFFER,fb);
checkGlError("blitTexture glBindFramebuffer");
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT1,aniTex[time],0);
checkGlError("blitTexture glFramebufferTexture2D");
glreadBuffer(GL_COLOR_ATTACHMENT1);
checkGlError("blitTexture glreadBuffer");
glDrawBuffers(1,attachment);
checkGlError("blitTexture glDrawBuffers");
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) LOGE("Incomplete frame buffer object!");
glBlitFramebuffer(0,GL_LINEAR);
checkGlError("blitTexture glBlitFramebuffer");
int samples;
glGetFramebufferParameteriv(GL_READ_FRAMEBUFFER,GL_FRAMEBUFFER_DEFAULT_SAMPLES,&samples);
LOGI("SAMPLES draw buffer %d",samples);
time = ++time%25;
}
可以在此处找到完整的项目:https://github.com/AndrewBloom/BlitFramebufferExample
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。