前面已经记录了从显存中获取渲染结果并保存为图像的方法
OpenGL:如何从缓存中读取颜色、深度信息【转】_六月的翅膀的博客-CSDN博客
并已经验证了上述方法可以完美的保存图像,但是保存深度信息部分还是出错了。
int WINDOW_WIDTH = (int)_width;
int WINDOW_HEIGHT = (int)_height;
int tmpPixelSize = WINDOW_WIDTH * WINDOW_HEIGHT;
GLfloat* depth_value = new GLfloat[tmpPixelSize];
//从深度缓冲区读取数据
glReadPixels(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_DEPTH_COMPONENT, GL_FLOAT, depth_value);
//输出深度值,数据太多,这里只输出了部分
for (int i = WINDOW_WIDTH / 2; i < WINDOW_WIDTH / 2 + 1; i++) {
for (int j = WINDOW_HEIGHT / 2; j < WINDOW_HEIGHT / 2 + 10; j++) {
qDebug() << depth_value[i * WINDOW_HEIGHT + j];
qDebug() << "-----";
}
}
delete[] depth_value;
具体出错是所有位置的深度信息都为0。尝试过改变数据类型GLfloat为其他类型,依旧。
【OpenGL operations should be performed only when an OpenGL context is active. This is true in the paintGL() method because this is probably set by the framework for you. You can't assume the OpenGL is active in other methods, like in other event responding methods and callbacks as mousePressEvent(), because those methods can also be run by a different thread where the OpenGL context is not active. 】
就是说异步线程里使用的原因。
原因找到了,试一下吧再。