View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Ganoosh_  Members

Posted 15 October 2005 - 11:34 AM

Taking screenshots should be very simple, and with glReadPixels it is. Problem is for some reason it crashes the program. When it calls it, the whole program just closes. I have no idea why or what could be causing it. Maybe I need to glEnable something? Does it need to be called or after something? I'm thinking maybe it's swapping buffers too soon and when I call there's nothing to be read in? I really have no clue, if anyone could help in any way I'd appreciate it, thanks.

### #2MrDaaark  Members

Posted 15 October 2005 - 11:40 AM

Ripped from my lib, you should get the basic idea.
//this will save a TGA in the screenshots
//folder under incrementally numbered files
void gl_video::SaveTGA(void)
{

char cFileName[64];
FILE *fScreenshot;
int nSize = m_WindowWidth * m_WindowHeight * 3;

GLubyte *pixels = new GLubyte [nSize];
if (pixels == NULL) return;

int nShot = 0;
while (nShot < 64)
{
sprintf(cFileName,"screenshot_%d.tga",nShot);
fScreenshot = fopen(cFileName,"rb");
if (fScreenshot == NULL) break;
else fclose(fScreenshot);

++nShot;
if (nShot > 63)
{
MessageBox(m_WindowHandle,"Screenshot limit of 64 reached. Remove some shots if you want to take more.",
m_AppTitle,MB_OK);
return;
}
}

fScreenshot = fopen(cFileName,"wb");

GL_UNSIGNED_BYTE, pixels);

//convert to BGR format
unsigned char temp;
int i = 0;
while (i < nSize)
{
temp = pixels[i];       //grab blue
pixels[i] = pixels[i+2];//assign red to blue
pixels[i+2] = temp;     //assign blue to red

}

m_WindowHeight%256,m_WindowHeight/256,24,0};

fwrite(pixels, sizeof(GLubyte), nSize, fScreenshot);
fclose(fScreenshot);

delete [] pixels;

return;
}



### #3Promag  Members

Posted 15 October 2005 - 11:43 AM

You need to post some code.. btw it closes cause you dont allocate enough memory to save the screenshot (framebuffer), so for instance,

int width, height;
GLfloat pixels malloc(width*height*3*sizeof(GLfloat));

### #4V-man  Members

Posted 15 October 2005 - 02:25 PM

Quote:
 Original post by Vampyre_DarkRipped from my lib, you should get the basic idea.*** Source Snippet Removed ***

When you are going to be dealing with 24 bit data (RGB), you have to set the data alignment.

glPixelStorei(GL_PACK_ALIGNMENT, 1);

For glDrawPixels and any GL function that uploads data:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

If you are working with 32 bit data, leave them at 4 (default for GL)

### #5MrDaaark  Members

Posted 15 October 2005 - 05:04 PM

Quote:
 Original post by V-manWhen you are going to be dealing with 24 bit data (RGB), you have to set the data alignment.
?? What exactly is the problem? The code works, I've saved thousands of TGAs with it by now.

### #6Yann L  Members

Posted 15 October 2005 - 05:35 PM

Quote:
Original post by Vampyre_Dark
Quote:
 Original post by V-manWhen you are going to be dealing with 24 bit data (RGB), you have to set the data alignment.
?? What exactly is the problem? The code works, I've saved thousands of TGAs with it by now.

Try it on a window where the width multiplied by 3 is not a multiple of 4, and your code will fail. Always set the alignment to 1 before reading back RGB data, as V-Man showed.

And that's not the only problem with that code, btw. It will also fail, if another window overlaps a part of the render window. This region will then contain undefined data in the screenshot.

### #7V-man  Members

Posted 16 October 2005 - 01:09 AM

Quote:
Original post by Yann L
Quote:
Original post by Vampyre_Dark
Quote:
 Original post by V-manWhen you are going to be dealing with 24 bit data (RGB), you have to set the data alignment.
?? What exactly is the problem? The code works, I've saved thousands of TGAs with it by now.

Try it on a window where the width multiplied by 3 is not a multiple of 4, and your code will fail. Always set the alignment to 1 before reading back RGB data, as V-Man showed.

And that's not the only problem with that code, btw. It will also fail, if another window overlaps a part of the render window. This region will then contain undefined data in the screenshot.

Correct, glReadPixels from the back buffer is not the best solution.
p-buffer or FBO is better.
For fullscreen apps, I think it's not a problem.

Vampyre_Dark, the problem is that you get boundary overflow and weird bugs and also, the screenshot will look screwed up.

### #8Ganoosh_  Members

Posted 16 October 2005 - 04:27 AM

Thanks guys I got it working, didn't fix the packing problem though cuz
Quote:
 Correct, glReadPixels from the back buffer is not the best solution.p-buffer or FBO is better.For fullscreen apps, I think it's not a problem.Vampyre_Dark, the problem is that you get boundary overflow and weird bugs and also, the screenshot will look screwed up.

It looks very screwed up. So what would be a better way to do screenshots? What is p-buffer and FBO?

### #9MrDaaark  Members

Posted 16 October 2005 - 06:13 AM

It's impossible to take a screenshot when another window is overlapped, as it doesn't have the input focus to even get the SaveTGA call. [grin] But what exactly is the problem with that? Is that something I'm doing wrong, or just if someone set it up to have it take screenshots automatically and there happened to be in the back of another window?

Okay, I think I have fixes for both of those. What is the problem with the window size and mulitples of 4?

//this will save a TGA in the screenshots
//folder under incrementally numbered files
void gl_video::SaveTGA(void)
{

glPixelStorei(GL_PACK_ALIGNMENT,1);

if (GetForegroundWindow() != m_WindowHandle) return;

char cFileName[64];
FILE *fScreenshot;
int nSize = m_WindowWidth * m_WindowHeight * 3;

GLubyte *pixels = new GLubyte [nSize];
if (pixels == NULL) return;

int nShot = 0;
while (nShot < 64)
{
sprintf(cFileName,"screenshot_%d.tga",nShot);
fScreenshot = fopen(cFileName,"rb");
if (fScreenshot == NULL) break;
else fclose(fScreenshot);

++nShot;
if (nShot > 63)
{
MessageBox(m_WindowHandle,"Screenshot limit of 64 reached. Remove some shots if you want to take more.",
m_AppTitle,MB_OK);
return;
}
}

fScreenshot = fopen(cFileName,"wb");

GL_UNSIGNED_BYTE, pixels);

//convert to BGR format
unsigned char temp;
int i = 0;
while (i < nSize)
{
temp = pixels[i];       //grab blue
pixels[i] = pixels[i+2];//assign red to blue
pixels[i+2] = temp;     //assign blue to red

}

m_WindowHeight%256,m_WindowHeight/256,24,0};

fwrite(pixels, sizeof(GLubyte), nSize, fScreenshot);
fclose(fScreenshot);

delete [] pixels;

return;
}


### #10Yann L  Members

Posted 16 October 2005 - 06:58 AM

Quote:
 Original post by Vampyre_DarkIt's impossible to take a screenshot when another window is overlapped, as it doesn't have the input focus to even get the SaveTGA call. [grin]

Keep in mind, that under Windows a window can have the input focus, yet still be overlapped by another floating window with the WS_EX_TOPMOST style.

Quote:
 Original post by Vampyre_DarkBut what exactly is the problem with that? Is that something I'm doing wrong, or just if someone set it up to have it take screenshots automatically and there happened to be in the back of another window?

No, it's fundamentally wrong to read back from the backbuffer, unless you are guaranteed to never have any overlapping window. In a fullscreen game, this is usually true, but it fails as soon as some floating window pops up (eg. a messenger notification, the task manager, the windows start menu, etc). Taking a screenshot might not be the most vital function, so that's an acceptable risk in most cases. But don't use the backbuffer as a replacement for a render texture, eg. for fullscreen bloom effects. As convenient as it might seem, the pixel ownership test is going to bite you hard, as soon as something overlaps. IMO, this is a flaw in the OpenGL specs, but well. The only way to create a screenshot function that works in really all circumstances is by using a pbuffer or a FBO.

Quote:
 Original post by Vampyre_DarkOkay, I think I have fixes for both of those. What is the problem with the window size and mulitples of 4?

If the width multiplied by the component data size (3 for RGB) isn't aligned with the current PACK_ALIGNMENT setting, then OpenGL will add appropriate padding. And that will screw up the image strides.

### #11MrDaaark  Members

Posted 16 October 2005 - 07:18 AM

Okay, but this will should handle the first problem? Setting it to be the foreground window could work too, but IMO that would be undesirable. Anything else getting in the way for me ins't a problem.
if (GetForegroundWindow() != m_WindowHandle) return;

My window viewports were always the standard 640,800,1024 sizes so I never bumped into the error. GL_PACK_ALIGNMENT = 1 will solve it always?

[Edited by - Vampyre_Dark on October 16, 2005 1:18:53 PM]

### #12V-man  Members

Posted 17 October 2005 - 06:15 AM

"GL_PACK_ALIGNMENT = 1 will solve it always?"

Yes and you lose performance.
Most graphics cards prefer GL_BGRA format.

"//convert to BGR format"

Why do you do that?
Use GL_BGR in the first place. These are core GL features in 1.2 and above.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.