PDA

View Full Version : From Screen to bmp



jrbcast
10-11-2002, 12:51 AM
Hi,

I think the topic explains my problem itself. I need to get data from screen (Using OpenGL of course) and store it in a file. I would prefer it to be a BMP o TIFF file and I would like not to use an extra library. Can you help me?.

Thanks in advance.

davepermen
10-11-2002, 01:08 AM
i know a friend of mine, who can help you. he's called google..

for the gl part: check glReadPixels.
for the saving to bmp part: check google, or the msdn..

Kilam Malik
10-11-2002, 09:34 AM
One Problem with using glReadPixels is that obscured areas (e.g. by a SaveAs dialog) are black.
It's better to render to an offscreen bitmap (make a new render context with a bitmap DC).

Kilam.

Diapolo
10-13-2002, 10:54 AM
Have a look at my function (perhaps not the best, but works *g*), a few changes and it should work for you, too :).




bool TakeOGLScreenshot(void)
{
String strScreenshotsDir(strAppBasePath + "Screenshots");
String strFileNameSshot;
String strTextToLog;

int iNumTakenScreenshots = 1;

GLubyte *glubScreenshot = NULL;
FILE *filePtr;
BITMAPFILEHEADER BMPFHeader;
memset(&BMPFHeader, 0, sizeof(BITMAPFILEHEADER));
BITMAPINFOHEADER BMPIHeader;
memset(&BMPIHeader, 0, sizeof(BITMAPINFOHEADER));

BMPFHeader.bfType = 0x4d42;
BMPFHeader.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (GLINIT.GetCurWidth() * GLINIT.GetCurWidth() * 3));
BMPFHeader.bfReserved1 = 0;
BMPFHeader.bfReserved2 = 0;
BMPFHeader.bfOffBits = (sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER));

BMPIHeader.biSize = sizeof(BITMAPINFOHEADER);
BMPIHeader.biWidth = GLINIT.GetCurWidth();
BMPIHeader.biHeight = GLINIT.GetCurHeight();
BMPIHeader.biPlanes = 1;
BMPIHeader.biBitCount = 24;
BMPIHeader.biCompression = BI_RGB;
BMPIHeader.biSizeImage = 0;
BMPIHeader.biXPelsPerMeter = 1;
BMPIHeader.biYPelsPerMeter = 1;
BMPIHeader.biClrUsed = 0;
BMPIHeader.biClrImportant = 0;

// erstellt das Verzeichnis "Screenshots"
CreateDir(strScreenshotsDir);

do
{
strFileNameSshot = strAppBasePath;
strFileNameSshot += "Screenshots\\PEX 3D Engine ";
strFileNameSshot += GLINIT.GetCurWidth();
strFileNameSshot += '_';
strFileNameSshot += GLINIT.GetCurHeight();
strFileNameSshot += '_';
strFileNameSshot += GLINIT.GetCurColorDepth();
strFileNameSshot += " (";
strFileNameSshot += iNumTakenScreenshots;
strFileNameSshot += ')';
strFileNameSshot += ".bmp";
iNumTakenScreenshots++;
}
while(FileExists(strFileNameSshot) == true);

filePtr = fopen(strFileNameSshot.c_str(), "wb");
if(filePtr == NULL)
{
strTextToLog = (GetCurTime() + " Uhr: " + ScreenshotMessageStart + strFileNameSshot + ScreenshotWarningMessageEnd);
AddProtocolEntry(false, false, NULL, PROTOCOL_FILE_NAME, &strTextToLog);

return false;
}
// wenn die Datei angelegt werden konnte hier weiter machen
else
{
glubScreenshot = new GLubyte[(GLINIT.GetCurWidth() * GLINIT.GetCurHeight() * 3)];
if(glubScreenshot == NULL)
{
strTextToLog = (GetCurTime() + " Uhr: " + NoMemorySShotBufferWarningMessage);
AddProtocolEntry(false, false, NULL, PROTOCOL_FILE_NAME, &strTextToLog);

return false;
}

glReadBuffer(GL_FRONT);
// eingelesene Pixel haben das Format blue-green-red nicht red-green-blue
glReadPixels(0, 0, GLINIT.GetCurWidth(), GLINIT.GetCurHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE, glubScreenshot);

fwrite(&BMPFHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
fwrite(&BMPIHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
fwrite(glubScreenshot, (GLINIT.GetCurWidth() * GLINIT.GetCurHeight() * 3), 1, filePtr);

fclose(filePtr);

delete[] glubScreenshot;

strTextToLog = (GetCurTime() + " Uhr: " + ScreenshotMessageStart + strFileNameSshot + ScreenshotWrittenMessageEnd);
AddProtocolEntry(false, false, NULL, PROTOCOL_FILE_NAME, &strTextToLog);
}
return true;
}


Diapolo