我正在使用C ++和OpenGL 3.3构建一个迷你游戏引擎,除了后期处理效果外,几乎所有东西都已设置好。我开始阅读关于帧缓冲的内容,并认为这不会有问题,我有多么错误......
似乎框架缓冲区以某种方式被绑定后未被使用。查看代码示例:
....
GLuint FBO;
glViewport(0, 0, 800, 600);
glLoadIdentity();
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
GLuint texColorBuffer;
glGenTextures(1, &texColorBuffer);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0
);
GLenum drawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, drawBuffers);
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Not completed!" << std::endl;
// Clear the screen
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Read the framebuffer pixels back to check if it 'cleared' to white
unsigned char *img = new unsigned char[104857600];
glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, img);
std::cout << img << std::endl;
delete[] img;
exit(1);
....
但是一旦我读回了framebuffer像素,我就什么也得不到,只是一个很大的空博客(比如许多行结尾)。这段代码有什么问题吗?另外,我几乎可以肯定OpenGL设置正确,在这个“测试”代码块之后我有很多绘图。
修改
这些是我正在使用的inizialization标志:
// Initializes and binds VAO (vertex arrary object)
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
// Enabling stuff
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Other
glDisable(GL_DEPTH_TEST);
我也在使用SDL 2.0.1,GLEW(截止到目前为止的最新版本)和TDM-GCC(GCC 4.8.1)
更新历史记录
答案 0 :(得分:-1)
从FrameBuffer读取像素返回数据,但是您要渲染到纹理,所以请使用 glGetTexImage或TexSubImage2D可以将你的纹理绘制到你的纹理上。
如果您仍想使用readPixel,请创建一个renderBuffer并将其与glFramebufferRenderbuffer绑定,或者您也可以在创建fbo时使用GL_READ_FRAMEBUFFER或GL_DRAW_FRAMEBUFFER以允许使用readPixel。
....
GLuint FBO;
glViewport(0, 0, 800, 600);
glLoadIdentity();
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
GLuint texColorBuffer;
glGenTextures(1, &texColorBuffer);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
取代:
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL
);
由:
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, 800, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0
);
GLenum drawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, drawBuffers);
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Not completed!" << std::endl;
// Clear the screen
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Read the framebuffer pixels back to check if it 'cleared' to white
unsigned char *img = new unsigned char[104857600];
取代:
glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, img);
由:
glGetTexImage( GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
std::cout << img << std::endl;
delete[] img;
exit(1);
....