• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ganoosh_

Taking screenshots using glReadPixels()

11 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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");

glReadPixels(0, 0, m_WindowWidth, m_WindowHeight, GL_RGB,
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

i += 3; //skip to next blue byte
}

unsigned char TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
unsigned char header[6] = {m_WindowWidth%256,m_WindowWidth/256,
m_WindowHeight%256,m_WindowHeight/256,24,0};

fwrite(TGAheader, sizeof(unsigned char), 12, fScreenshot);
fwrite(header, sizeof(unsigned char), 6, fScreenshot);
fwrite(pixels, sizeof(GLubyte), nSize, fScreenshot);
fclose(fScreenshot);

delete [] pixels;

return;
}

0

Share this post


Link to post
Share on other sites
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));
glReadPixels(... , pixels);
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Vampyre_Dark
Ripped 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.

For glReadPixels and any GL function that downloads data:

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)

0

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
When 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.

0

Share this post


Link to post
Share on other sites
Quote:
Original post by Vampyre_Dark
Quote:
Original post by V-man
When 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.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Yann L
Quote:
Original post by Vampyre_Dark
Quote:
Original post by V-man
When 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.
0

Share this post


Link to post
Share on other sites
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?

0

Share this post


Link to post
Share on other sites
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");

glReadPixels(0, 0, m_WindowWidth, m_WindowHeight, GL_RGB,
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

i += 3; //skip to next blue byte
}

unsigned char TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
unsigned char header[6] = {m_WindowWidth%256,m_WindowWidth/256,
m_WindowHeight%256,m_WindowHeight/256,24,0};

fwrite(TGAheader, sizeof(unsigned char), 12, fScreenshot);
fwrite(header, sizeof(unsigned char), 6, fScreenshot);
fwrite(pixels, sizeof(GLubyte), nSize, fScreenshot);
fclose(fScreenshot);

delete [] pixels;

return;
}


0

Share this post


Link to post
Share on other sites
Quote:
Original post by Vampyre_Dark
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]

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_Dark
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?

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_Dark
Okay, 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.
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
"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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0