OpenGL glReadPixels 转成 CImage

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

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彷佛还不行,它并不会从左上角开始,真弄不懂这设计者的逻辑。spa

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

.h文件中定义:code

GLubyte *_print_image_data;
CImage _print_img;

OnPreparePrinting(CPrintInfo* pInfo)component

_print_image_data = nullptr;
    glReadBuffer(GL_BACK_LEFT);

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

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*/)xml

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

OnPrint(CDC* pDC, CPrintInfo* pInfo)htm

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了吧。blog

相关文章
相关标签/搜索