OpenGL glReadPixels 转成 CImage

本来是一件很简单的事情,就是将View上使用OpenGL画出来的数据生成图片让其实现打印的功能,我们知道MFC提供打印的接口是不支持OpenGL的(至少我不清楚),必须将它转成GDI再画在Print的pDC上。

OpenGL一直都有一个函数名字叫:glReadPixels,它能read a block of pixels from the frame buffer,但是谁都没想到它竟然转换成CImage竟然是这样的规则。

http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml中说到:Specify the window coordinates of the first pixel that is read from the frame buffer. This location is the lower left corner of a rectangular block of pixels.  是从左下角开始读,这一条是我始料未及的,一般通常都是从左上角开始的,我至今没想明白为什么它要这样设计。在调用这个函数之前还需要glReadBuffer(GL_BACK_LEFT);这句,如果你想将它设置成GL_FRONT_LEFT似乎还不行,它并不会从左上角开始,真弄不懂这设计者的逻辑。

然后接着下一步就开始往CImage里面写东西了:直接上code:

.h文件中定义:

GLubyte *_print_image_data;
CImage _print_img;

OnPreparePrinting(CPrintInfo* pInfo)

_print_image_data = nullptr;
    glReadBuffer(GL_BACK_LEFT);

OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

int format = GL_RGB ,components = 0 , width = m_width, height = m_height;
    switch(format) {
    case GL_RGB:
        components = 3; break;
    case GL_RGBA:
        components = 4; break;
    case GL_ALPHA:
    case GL_LUMINANCE:
        components = 1; break;
    }
    _print_image_data = (GLubyte*)malloc(components * width * height);
    glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, _print_image_data);
    _print_img.Create(width, height, 24);

    int nPixel = height*width*3;
    for(int j = 0; j <height ; j++)
        for(int i = width-1; i >=0; i--)
        {
            BYTE b = _print_image_data[--nPixel];
            BYTE g = _print_image_data[--nPixel];
            BYTE r = _print_image_data[--nPixel];
            _print_img.SetPixelRGB(i, j, r,g,b);
        }

OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

    free(_print_image_data);
    _print_image_data = nullptr;
    _print_img.Destroy();

OnPrint(CDC* pDC, CPrintInfo* pInfo)

if(_print_image_data != nullptr)
        _print_img.Draw(pDC->m_hDC, 0, 0, _print_img.GetWidth()*4, _print_img.GetHeight()*4);

可以看到那个x、y轴的双层循环的code真是有点怪异,一个++、一个--,但是又必须这样写才能对,希望以后有人再碰到这种情况能够Lucky的找到我这篇blog了吧。

posted @ 2013-08-12 10:03  重庆Debug  阅读(2028)  评论(0编辑  收藏  举报