开发者

glReadPixels GL_DEPTH_COMPONENT and color

开发者 https://www.devze.com 2023-01-29 07:20 出处:网络
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:

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()) {
        开发者_如何学Go    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?


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:

#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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消