为了测试从opengl层读取像素是否正确,写了一个函数把读取的像素写入到bmp图片中。
读取opengl绘制到窗口中的像素,在vxtPLWindowImpl.cpp的vEndDraw函数中,
在SwapBuffers后面,添加读取像素数据并写入bmp图片的函数。
#pragma pack(push) #pragma pack(1) typedef struct { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; }BIT_MAP_FILE_HEADER; typedef struct { DWORD biSize; DWORD biWidth; DWORD biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; DWORD biXPelsPerMeter; DWORD biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; }BIT_MAP_INFO_HEADER; void grabBmp(bool alpha) { int ByteCount = alpha ? 4 : 3; int PixelLength = 0; if(alpha) { PixelLength = WindowWidth * WindowHeight * ByteCount; } else { int AlignmentWidth = WindowWidth * ByteCount; while( AlignmentWidth % 4 != 0 ) ++AlignmentWidth; PixelLength = AlignmentWidth * WindowHeight; } GLubyte* pPixelData = (GLubyte*)malloc(PixelLength); if( pPixelData == 0 ) exit(0); FILE* pWritingFile = fopen("D:\\grab.bmp", "wb+"); if( pWritingFile == 0 ) exit(0); BIT_MAP_FILE_HEADER bmp_map_file; memset(&bmp_map_file, 0, sizeof(BIT_MAP_FILE_HEADER)); bmp_map_file.bfType = 0x4d42; bmp_map_file.bfSize = sizeof(BIT_MAP_FILE_HEADER) + sizeof(BIT_MAP_INFO_HEADER) + PixelLength; bmp_map_file.bfOffBits = sizeof(BIT_MAP_FILE_HEADER) + sizeof(BITMAPINFOHEADER); fwrite(&bmp_map_file, sizeof(BIT_MAP_FILE_HEADER), 1, pWritingFile); BIT_MAP_INFO_HEADER bmp_map_info; memset(&bmp_map_info, 0, sizeof(BITMAPINFOHEADER)); bmp_map_info.biSize = sizeof(BITMAPINFOHEADER); bmp_map_info.biHeight = WindowHeight; bmp_map_info.biWidth = WindowWidth; bmp_map_info.biSizeImage = PixelLength; bmp_map_info.biCompression = BI_RGB; bmp_map_info.biBitCount = ByteCount * 8; bmp_map_info.biPlanes = 1; fwrite(&bmp_map_info, sizeof(BIT_MAP_INFO_HEADER), 1, pWritingFile); glReadBuffer(GL_FRONT); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); if(alpha) glReadPixels(0, 0, WindowWidth, WindowHeight, GL_RGBA, GL_UNSIGNED_BYTE, pPixelData); else glReadPixels(0, 0, WindowWidth, WindowHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, pPixelData); fwrite(pPixelData, PixelLength, 1, pWritingFile); fclose(pWritingFile); free(pPixelData); }