在vc 6中設置好opengl開發(fā)環(huán)境,視口的大小設置成與客戶區(qū)大小_第1頁
在vc 6中設置好opengl開發(fā)環(huán)境,視口的大小設置成與客戶區(qū)大小_第2頁
在vc 6中設置好opengl開發(fā)環(huán)境,視口的大小設置成與客戶區(qū)大小_第3頁
在vc 6中設置好opengl開發(fā)環(huán)境,視口的大小設置成與客戶區(qū)大小_第4頁
在vc 6中設置好opengl開發(fā)環(huán)境,視口的大小設置成與客戶區(qū)大小_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、在vc 6中設置好opengl開發(fā)環(huán)境,視口的大小設置成與客戶區(qū)大小一樣,欲將客戶區(qū)的圖像保存為24位真彩色位圖,源代碼如下,可是存在這樣的問題:程序運行,窗口剛創(chuàng)建時(此時不拉動邊框改變客戶區(qū)大?。?,可以正常保存。當最大化窗口或將最大化窗口復原,都可以正常保存。而當人為拉動邊框改變客戶區(qū)大小后,保存的圖像出現(xiàn)問題,如下:調試信息如下:源代碼如下:void CMySDOpenGLView:OnCaptureImage() /保存位圖的消息函數(shù)/ TODO: Add your command handler code hereCString s("Windows Bitmap(*.bm

2、p)|*.bmp|");CFileDialog fd(FALSE,"bmp",0,OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,s);if(fd.DoModal()=IDOK)BeginWaitCursor();int viewport4;glGetIntegerv(GL_VIEWPORT,viewport); bool success = false; DWORD nFileType=0;if(strcmp(fd.GetFileExt(),"bmp")=0) nFileType=

3、1; if(nFileType=1)success=m_imagecapture.CaptureGLBufferToBMP(fd.GetPathName(),/調用保存位圖類的 viewport0,viewport1,/函數(shù)保存位圖 viewport2,viewport3);else / Invalid file extension specified. MessageBox("WTF? Unknown file extension specified for screen capture.n","Error",MB_OK); if (!success)

4、 char errorMsg64; if(nFileType = 0) sprintf(errorMsg, "Specified file extension is not recognized."); else sprintf(errorMsg, "Could not save image file."); MessageBox(errorMsg, "Error", MB_OK | MB_ICONERROR); EndWaitCursor();保存位圖類的定義class CImageCapturepublic:CImageCaptu

5、re();virtual CImageCapture();bool CaptureGLBufferToBMP(CString filename, int x, int y, int width, int height);int WriteBitmapFile(CString filename, int width, int height, unsigned char *imageData);CImageCapture:CImageCapture()CImageCapture:CImageCapture()bool CImageCapture:CaptureGLBufferToBMP(CStri

6、ng filename, int x, int y, int width, int height)void* imageData = malloc(width*height*3);/ allocate memory for the imageDatamemset(imageData, 0, width*height*3); / clear imageData memory contents/ read the image data from the windowglReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (unsign

7、ed char*)imageData);/ write the image data to a fileWriteBitmapFile(filename, width, height, (unsigned char*)imageData);/ free the image data memoryfree(imageData);return true;/ WriteBitmapFile()/ desc: takes image data and saves it into a 24-bit RGB .BMP file/ with width X height dimensionsint CIma

8、geCapture:WriteBitmapFile(CString filename, int width, int height, unsigned char *imageData)FILE *filePtr;/ file pointerBITMAPFILEHEADER bitmapFileHeader;/ bitmap file headerBITMAPINFOHEADER bitmapInfoHeader;/ bitmap info headerunsigned int imageIdx;/ used for swapping RGB->BGRunsigned char tempR

9、GB;/ used for swapping/ open file for writing binary modefilePtr = fopen(filename, "wb");if (!filePtr)return 0;/ define the bitmap file headerbitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+(4*(width*24+31)/32) * height);bitmapFileHeader.bfType = 0x4D42;bitmapFi

10、leHeader.bfReserved1 = 0;bitmapFileHeader.bfReserved2 = 0;bitmapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);/ define the bitmap information headerbitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);bitmapInfoHeader.biPlanes = 1;bitmapInfoHeader.biBitCount = 24;/ 24-bitb

11、itmapInfoHeader.biCompression = BI_RGB;/ no compressionbitmapInfoHeader.biSizeImage = 4*(width*24+31)/32) * height;/ width * height * (RGB bytes)bitmapInfoHeader.biXPelsPerMeter = 0;bitmapInfoHeader.biYPelsPerMeter = 0;bitmapInfoHeader.biClrUsed = 0;bitmapInfoHeader.biClrImportant = 0;bitmapInfoHead

12、er.biWidth = width;/ bitmap widthbitmapInfoHeader.biHeight = height;/ bitmap height/ switch the image data from RGB to BGRfor (imageIdx = 0; imageIdx < bitmapInfoHeader.biSizeImage; imageIdx+=3)tempRGB = imageDataimageIdx;imageDataimageIdx = imageDataimageIdx + 2;imageDataimageIdx + 2 = tempRGB;/

13、add empty Byte to every row, if this row cannot be divided exactly by 4. unsigned char *imageDataAfterAdding =(unsigned char *) malloc(4*(width*24+31)/32) * height);memset(imageDataAfterAdding, 0, 4*(width*24+31)/32) * height); for(int k=0; k<height; k+)for(int i=0; i<width*3; i+)imageDataAfterAddingk*width*3+i+k*(4*(width*24+31)/32)-width*3)= imageDatak*width*3+i;/ write the bitmap file headerfwrite(&bitmapFileHeader, 1, sizeof(BITMAPFILEHEADER), filePtr);/ write the bitmap info headerfwrite(&bitmapInfoHeader, 1, sizeof(B

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論