【问题标题】:glReadPixels GL_DEPTH_COMPONENT and colorglReadPixels GL_DEPTH_COMPONENT 和颜色
【发布时间】:2010-12-06 18:06:45
【问题描述】:

如何从任何 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);

但看起来只有深度数组被填充了?

【问题讨论】:

  • 你的“网格”顶点颜色中是否有蓝色?
  • 是的,他们做到了..!但还是同样的问题.. 试过这个: IplImage* toSave = cvCreateImage(cvSize((int)width,(int)height), IPL_DEPTH_32F, 3); glReadPixels(0, 0, (int)width, (int)height, GL_BGR, GL_FLOAT, toSave->imageData); toSave->原点 = 1; cvSaveImage("test.png", toSave);
  • 如果我在 qglviewer 中使用相同的,它的工作原理.. 但我不知道为什么..

标签: opengl framebuffer depth-buffer


【解决方案1】:

为了将渲染结果保存在图像中,您必须保存颜色缓冲区信息(不是直接来自深度缓冲区)。

您可以为颜色(到颜色缓冲区)和深度提供单独的通道到相同的颜色缓冲区。并且简单地使用 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

【讨论】:

    【解决方案2】:

    试试这个:

    #include <GL/freeglut.h>
    #include <vector>
    #include <sstream>
    
    int mx = 0, my = 0;
    void passiveMotion( int x, int y )
    {
        mx = x;
        my = glutGet( GLUT_WINDOW_HEIGHT ) - y;
        glutPostRedisplay();
    }
    
    void display()
    {
        glEnable(GL_DEPTH_TEST);
        glClearColor( 0, 0, 0, 1 );
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        const int w = glutGet( GLUT_WINDOW_WIDTH );
        const int h = glutGet( GLUT_WINDOW_HEIGHT );
        const double ar = (double)w / (double)h;
        glOrtho( -10 * ar, 10 * ar, -10, 10, -10, 10 );
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glColor3ub(0,255,0);
        glPushMatrix();
            glTranslated(2,2,-5);
            glScalef(5,5,5);
            glBegin(GL_QUADS);
                glVertex2f(-1,-1);
                glVertex2f(1,-1);
                glVertex2f(1,1);
                glVertex2f(-1,1);
            glEnd();
        glPopMatrix();
    
        glColor3ub(255,0,0);
        glPushMatrix();
            glTranslated(0,0,0);
            glScalef(5,5,5);
            glBegin(GL_QUADS);
                glVertex2f(-1,-1);
                glVertex2f(1,-1);
                glVertex2f(1,1);
                glVertex2f(-1,1);
            glEnd();
        glPopMatrix();
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho( 0, w, 0, h, -1, 1 );
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        // print depth
        {
            GLfloat depth = 0.0f;
            glReadPixels( mx, my, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth ); 
            std::ostringstream oss;
            oss << "Depth: " << depth;
            glColor3ub( 255, 255, 255 );
            glRasterPos2i( 10, 10 );
            glutBitmapString( GLUT_BITMAP_9_BY_15, (const unsigned char*)oss.str().c_str() );
        }
    
        // print color
        {
            GLubyte color[4];
            glReadPixels( mx, my, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color ); 
            std::ostringstream oss;
            oss << "Color:"
                << " " << (unsigned int)color[0]
                << " " << (unsigned int)color[1]
                << " " << (unsigned int)color[2]
                << " " << (unsigned int)color[3];
            glColor3ub( 255, 255, 255 );
            glRasterPos2i( 10, 25 );
            glutBitmapString( GLUT_BITMAP_9_BY_15, (const unsigned char*)oss.str().c_str() );
        }
    
        glutSwapBuffers();
    }
    
    int main( int argc, char** argv )
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
        glutInitWindowSize(400,400);
        glutCreateWindow("GLUT");
        glutDisplayFunc( display );
        glutPassiveMotionFunc( passiveMotion );
        glutMainLoop();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多