PDA

View Full Version : screenshot routine



McBain
11-12-2001, 01:42 PM
i have a screenshot routine with glreadpixels.
strange behaviour:
- crash if window is resized horizontally
- no crash if window is not resized
- no crash if window is resized vertically
- no crash in any fullscreen mode

the routine crashes at the glReadPixels call, if (0, 0 changed to (1, 1 then everything works fine
so buffer overflow ? I made the buffer bigger, no change.

any1 ?


// write screenshot to disk
void GLTools::screenShot(int width, int height) {
printf("GLTools::screenShot(): writing screenshot.tga (%dx%d) to disk...\n", width, height);
// allocate buffer and calculate buffer size
unsigned char *buffer;
int bufsize = width * height * 3 + 18;
buffer = (unsigned char *) malloc(bufsize);
if (buffer != NULL) {
// write tga header
memset(buffer, 0, 18);
buffer[2] = 2;
buffer[12] = width & 255;
buffer[13] = width >> 8;
buffer[14] = height & 255;
buffer[15] = height >> 8;
buffer[16] = 24;
// read pixels
glReadPixels(0, 0, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, buffer + 18);
// write to file
FILE * pFile;
pFile = fopen("screenshot.tga" , "w");
if (pFile != NULL) {
fwrite (buffer, 1, bufsize, pFile);
fclose (pFile);
}
else {
// fopen() failed
printf(" *** Error: GLTools::screenShot(): fopen() failed");
exit(1);
}
free (buffer);
}
else {
// malloc() failed
printf(" *** Error: GLTools::screenShot(): malloc() failed");
exit(1);
}
}

Tron
11-12-2001, 08:00 PM
do you set glPixelStorei(GL_PACK_ALIGNMENT, 1)? if not every line is word-aligned!

for comparison check out my screenshot code, which just generates a 24bit RGB raw-file




UByte* Data = new UByte[Width() * Height() * 3];
if (Data)
{
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(
0,
0,
Width(),
Height(),
GL_RGB,
GL_UNSIGNED_BYTE,
Data
);
ofstream File("Screenshot.raw", ios::binary);
if (File.is_open())
File.write(Data, Width() * Height() * 3);
delete [] Data;
}


[This message has been edited by Tron (edited 11-12-2001).]

[This message has been edited by Tron (edited 11-12-2001).]

McBain
11-13-2001, 04:59 AM
that seems to do the trick ...
thnx