opengl 화면을 tga 파일로 캡쳐하는 소스코드 (.net C++)

글의 원문은 여기에 있다.
http://www.gamedev.net/community/forums/topic.asp?topic_id=350104
윈도우에는 화면 캡쳐하는 기능이 있지만 그림 품질이 얼마나 차이가 날지는 모르지만 raw데이터로 tga로 보내는 거니 화면 캡쳐보다 품질이 더 좋지 않을까 생각된다(같을수도 있지만).

내 나쁜 기억력이나 다른 사람에게 도움이 되었으면 한다 ^^;;

// snapshot to tga
void glcanvas::SnapShotToFile(String^ file_name)
{
 pin_ptr filename = PtrToStringChars(file_name);

 int windowWidth = v3d->GetCamera().width();   
 int windowHeight = v3d->GetCamera().height();

 unsigned char *outputImage = 0;

 // Allocate the neccessary memory.
 outputImage = (unsigned char*)malloc(windowWidth * windowHeight * 3);

 // Clear the variable.
 memset(outputImage, 0, windowWidth * windowHeight * 3);

 glPixelStorei(GL_PACK_ALIGNMENT, 1);
 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  
 glReadPixels(0, 0, windowWidth, windowHeight, GL_RGB, GL_UNSIGNED_BYTE, outputImage);

 FILE *filePtr = _wfopen(filename, L"wb");
 if (!filePtr)
  return;

 // To save a screen shot is just like reading in a image.  All you do
 // is the opposite.  Istead of calling fread to read in data you call
 // fwrite to save it.

 FILE *pFile;               // The file pointer.
 unsigned char uselessChar; // used for useless char.
 short int uselessInt;      // used for useless int.
 unsigned char imageType;   // Type of image we are saving.
 int index;                 // used with the for loop.
 unsigned char bits;    // Bit depth.
 long Size;                 // Size of the picture.
 int colorMode;
 unsigned char tempColors;

 // Open file for output.
 pFile = _wfopen(filename, L"wb");

 // Check if the file opened or not.
 if(!pFile) { fclose(pFile); return; }

 // Set the image type, the color mode, and the bit depth.
 imageType = 2; colorMode = 3; bits = 24;

 // Set these two to 0.
 uselessChar = 0; uselessInt = 0;

 // Write useless data.
 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);

 // Now image type.
 fwrite(&imageType, sizeof(unsigned char), 1, pFile);

 // Write useless data.
 fwrite(&uselessInt, sizeof(short int), 1, pFile);
 fwrite(&uselessInt, sizeof(short int), 1, pFile);
 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
 fwrite(&uselessInt, sizeof(short int), 1, pFile);
 fwrite(&uselessInt, sizeof(short int), 1, pFile);

 // Write the size that you want.
 fwrite(&windowWidth, sizeof(short int), 1, pFile);
 fwrite(&windowHeight, sizeof(short int), 1, pFile);
 fwrite(&bits, sizeof(unsigned char), 1, pFile);

 // Write useless data.
 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);

 // Get image size.
 Size = windowWidth * windowHeight * colorMode;

 // Now switch image from RGB to BGR.
 for(index = 0; index < Size; index += colorMode)
 {
  tempColors = outputImage[index];
  outputImage[index] = outputImage[index + 2];
  outputImage[index + 2] = tempColors;
 }

 // Finally write the image.
 fwrite(outputImage, sizeof(unsigned char), Size, pFile);

 // close the file.
 fclose(pFile);
 free(outputImage);

}