2014/09/05

glReadPixels to read data output by shader

I needed to read data output by one shader. So I made the shader output to a texture, but glReadPixels does not need a texture address. It seems to read data of current frame buffer.
So you have to set frame buffer with glBindFramebuffer if you need.

My code worked as follows :
{
    unsigned char *data = calloc(width*height*4, sizeof(unsigned char));
    glReadPixels(0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE,data);
    for (int ii=0;ii < width;ii++) {
        NSLog(@"%d,%d,%d,%d",data[ii*4],data[ii*4+1],data[ii*4+2],data[ii*4+3]);
    }
    free(data);
}
The format of output data by the shader is GL_RGBA. So you can set the value from 0 to 1.0 in your shader.
For example, if you set the value as 3.0/255.0 , then NSLog (in the code above) outputs 3.
Develop | Comments(0) | Trackback(0)
Comment

管理者のみに表示