我之前问过我如何能够显示我的颜色缓冲区并将其保存到磁盘,我得到的答案是我应该这样做;
Render to a FBO and use glReadPixels() to slurp images out of that instead of the front buffer.
How can I generate a screenshot when glReadPixels is empty?
但是,我已经阅读了一些有关帧缓冲区的内容,但仍然完全感到困惑,所以我想我会问一下如何在 SO 上执行此操作。我的代码执行如下操作:
/* Callback handler for window re-paint event */
void display()
{
glClear(GL_COLOR_BUFFER_BIT); //Clear the color buffer
glMatrixMode(GL_MODELVIEW); //To operate on the model-view matrix
// do some rendering
glFlush(); // display
}
当我想随时保存图像时,我运行这个:
std::unique_ptr<RGBA2D> GrabScreen()
{
//we get the width/height of the screen into this array
GLint screen[4];
//get the width/height of the window
glGetIntegerv(GL_VIEWPORT, screen);
GLint width = screen[2];
GLint height = screen[3];
std::unique_ptr<RGBA2D> pixels(new RGBA2D(height, width * 4));
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels->data());
return std::move(pixels);
}
请注意,RGBA2D
是一个二维特征向量对象(不重要)。这一切都很好,除了它只在显示图像时才保存图像。我希望能够在没有显示器的 unix 机器上运行我的程序。我想呈现给 FBO。我该怎么做?
最佳答案
不是手 Handlebars 教,但我希望能为您指明正确的方向。
您将使用 glGenFramebuffers
和 glBindFramebuffer
来创建和绑定(bind)帧缓冲区对象 (FBO)。
然后您可以选择是渲染到纹理还是渲染缓冲区。为了您的目的,任何一个都可以。恕我直言,渲染缓冲区更容易。使用 glGenRenderbuffers
、glBindRenderbuffer
和 glRenderbufferStorage
设置颜色渲染缓冲区。
然后使用 glFramebufferRenderbuffer
将颜色渲染缓冲区附加到 FBO。
如果您需要深度缓冲区,请重复前两个步骤以创建并附加另一个渲染缓冲区用作 FBO 渲染的深度缓冲区。
然后进行渲染,并使用 glReadPixels
抓取帧。
所有这些调用都记录在 the man pages at www.opengl.org 中.如果您搜索关键字和一些函数名称,您应该也能找到一些完整的代码示例。
关于c++ - 需要一些手持来渲染、显示和保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23421235/