关于将OpenGL渲染的图像保存到一个位图文件中的有关问题?

关于将OpenGL渲染的图像保存到一个位图文件中的问题??急
我是在vc++环境下的。
我用来显示的窗口用的是OpenGL自带的库函数创建的,我想将我窗口渲染出来的画面在程序的文档下面以位图的格式保存。我知道应该是现有glReadPixels()函数先读取帧缓冲区的像素信息,用保存信息的数组来绘制这个位图,在绘制之后将其保存在目录下。
高手们,我该如何创建这个位图文件呢?如何些这个位图类,如何创建呢?最好有代码能让我参考下,或者介绍写入手的方法,谢谢

------解决方案--------------------
创建位图还不简单,如果用MFC的话就用CBitmap::CreateBitmap,如果没有MFC就用CreateBitmap

BOOL CBitmap::CreateBitmap(
int nWidth,
int nHeight,
UINT nPlanes,
UINT nBitcount,
const void* lpBits 
);


HBITMAP CreateBitmap(
int nWidth, // bitmap width, in pixels
int nHeight, // bitmap height, in pixels
UINT cPlanes, // number of color planes
UINT cBitsPerPel, // number of bits to identify color
CONST VOID *lpvBits // color data array
);
------解决方案--------------------
CreateBitmap就是根据参数创建出来一个空的位图,它所创建的是内存位图,要保存文件之前先生成这个.
如果要保存为文件的话需要自己写代码,Windows API只提供了读取位图的函数没提供保存位图的函数.不过Windows 的位图格式是公开的,自己写一个代码没有多困难,实在不愿意还有很多免费的库可以用
------解决方案--------------------
http://www.geocities.com/foetsch/screenshot/screenshot.htm
可以参考一下这个
------解决方案--------------------
CRect rect;
GetClientRect(&rect);
CSize size(rect.Width(),rect.Height());
ASSERT(size.cx > 0);
ASSERT(size.cy > 0);
TRACE("Start reading client...\n");
TRACE("Client : (%d,%d)\n",size.cx,size.cy);
unsigned char *pixel = new unsigned char[3*size.cx*size.cy];
ASSERT(pixel != NULL);
CRect ClientRect,MainRect;
this->GetWindowRect(&ClientRect);
CWnd *pMain = AfxGetApp()->m_pMainWnd;
CWindowDC dc(pMain);
pMain->GetWindowRect(&MainRect);
int xOffset = ClientRect.left - MainRect.left;
int yOffset = ClientRect.top - MainRect.top;
for(int j=0;j<size.cy;j++)
for(int i=0;i<size.cx;i++)
{
COLORREF color = dc.GetPixel(i+xOffset,j+yOffset);
pixel[3*(size.cx*(size.cy-1-j)+i)] = (BYTE)GetBValue(color);
pixel[3*(size.cx*(size.cy-1-j)+i)+1] = (BYTE)GetGValue(color);
pixel[3*(size.cx*(size.cy-1-j)+i)+2] = (BYTE)GetRValue(color);
}
int rest=(size.cx*3)%4;
unsigned int Width32;
if(rest != 0)
Width32=(size.cx*3 + 4-rest);
else
Width32=(size.cx*3);
unsigned int BytePerPixel = 3;

unsigned char *pData = new unsigned char [Width32 * size.cy];
for(j=0;j<size.cy;j++)
for(int i=0;i<size.cx;i++)
for(int k=0;k<BytePerPixel;k++)
{
pData[Width32*j + i*BytePerPixel+k] = 
pixel[(size.cx*j+i)*BytePerPixel+k];
//TRACE("pixel : %d\n",(int)buffer[(width*j+i)*BytePerPixel+k]);
}

// Cleanup memory
delete [] pixel;
BITMAPINFOHEADER header;
header.biWidth=size.cx;
header.biHeight=size.cy;
header.biSizeImage=Width32*size.cy;
header.biSize=40;
header.biPlanes=1;
header.biBitCount=24;
header.biCompression=0;
header.biXPelsPerMeter=0;
header.biYPelsPerMeter=0;
header.biClrUsed=0;
header.biClrImportant=0;

BITMAPFILEHEADER bfh;
bfh.bfType=0X4D42;
bfh.bfSize=54+Width32*size.cy;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
bfh.bfOffBits=54;

FILE *fA=fopen(openname,"wb");
fwrite(&bfh,sizeof(BITMAPFILEHEADER),1,fA);
fwrite(&header,sizeof(BITMAPINFOHEADER),1,fA);
fwrite(pData, Width32 * size.cy,1,fA);
fclose(fA);

TRACE("ok\n");
试试这个,直接读取帧缓存的。不过感觉不如直接生成个DIB区然后用onpaint绘制在里边来得好。
------解决方案--------------------