glReadPixels GL_DEPTH_COMPONENT 和颜色
如何从 OpenGL 绘图中获取深度和颜色信息?我想将深度图像和彩色图像保存到磁盘上。我尝试的是以下内容:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glBegin(GL_POINTS);
glColor3f(1.0f,1.0f,1.0f);
for(int i=0; i<mesh->vertices.size();i++) {
if(! mesh->colors.empty()) {
glColor3f(mesh->colors[i][0],mesh->colors[i][1],mesh->colors[i][2]);
}
float x= mesh->vertices[i][0];
float y= mesh->vertices[i][1];
float z = mesh->vertices[i][2];
glVertex3f(x, y, z);
}
glEnd();
glFlush();
glFinish();
int width = 1280;
int height = 960;
GLfloat* depths;
depths = new GLfloat[ width * height ];
GLfloat * color;
color = new GLfloat[width * height];
glReadPixels (0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, depths);
glReadPixels (0, 0, width, height, GL_BLUE, GL_FLOAT, color);
但看起来只有深度数组被填充?
How to get the depth and the color information out of any OpenGL drawing? I would like to save a depth image and a color image to the disk. What i tried is the following:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glBegin(GL_POINTS);
glColor3f(1.0f,1.0f,1.0f);
for(int i=0; i<mesh->vertices.size();i++) {
if(! mesh->colors.empty()) {
glColor3f(mesh->colors[i][0],mesh->colors[i][1],mesh->colors[i][2]);
}
float x= mesh->vertices[i][0];
float y= mesh->vertices[i][1];
float z = mesh->vertices[i][2];
glVertex3f(x, y, z);
}
glEnd();
glFlush();
glFinish();
int width = 1280;
int height = 960;
GLfloat* depths;
depths = new GLfloat[ width * height ];
GLfloat * color;
color = new GLfloat[width * height];
glReadPixels (0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, depths);
glReadPixels (0, 0, width, height, GL_BLUE, GL_FLOAT, color);
But it looks like only the depths array is filled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了将渲染结果保存在图像中,您必须保存颜色缓冲区信息(不是直接来自深度缓冲区)。
您可以为颜色(到颜色缓冲区)和深度到同一颜色缓冲区提供单独的通道。简单地使用 glReadPixels 两次,第一次是在将颜色渲染到颜色缓冲区之后,第二次是在颜色缓冲区中渲染深度之后。
要通过一次在两个单独的颜色缓冲区中同时写入颜色和深度,您可以使用 MRT(多个渲染目标),教程 - http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/ 。
我会选择地铁。 :) 之后,您可以使用 glReadPixels 保存结果,就像在两次传递技术中一样。
但首先您必须使用glReadBuffer设置要从哪个颜色缓冲区读取像素,默认颜色缓冲区是GL_BACK,这意味着默认OpenGL上下文后备缓冲区。通过使用 MRT,您必须使用 GL_COLOR_ATTACHMENTi 之一来写入颜色缓冲区,它也可以是 glReadBuffer 值之一。
因此,只需使用 GL_COLOR_ATTACHMENTi 之一简单设置 glReadBuffer 并使用 glReadPixels。
For saving the render result in image, you must save colorbuffer information (not directly from depth buffer).
You can provide separate passes for color (to colorbuffer) and depth to same colorbuffer. And simple use glReadPixels two times, first after rendering color to colorbuffer and second after rendering depth in colorbuffer.
For write color and depth simultaneously in two separate color buffers by one pass you can use MRT ( Multiple Render Targets ), tutorial - http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/ .
I would choose MRT. :) After that you can save your results by using glReadPixels like in two passes technic.
But first you must setup from which colorbuffer you want read pixels by using glReadBuffer, default colorbuffer is GL_BACK, which mean default OpenGL context backbuffer. By using MRT you must use one of GL_COLOR_ATTACHMENTi for write in to colorbuffers and it also can be one of glReadBuffer value.
So, just simple setup glReadBuffer with one of GL_COLOR_ATTACHMENTi and use glReadPixels.
试试这个:
Try this: