Jump to content
  • Advertisement
Sign in to follow this  
KungFooMasta

OpenGL Screen capture to bmp orjpg?

This topic is 3798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to OpenGL programming, and I'm using OpenGL ES, trying to do a simple screen capture and save it to disk. I've seen people mention I need to use glReadPixels and flip the bytes, or restructure in BGR mode, etc. I've seen code to write a TGA, but I'm unable to view those types of files. Is there an easy way to create a bitmap or jpeg? Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Hidden
This is the code I use for saving a screenshot, but I'm using SDL. Maybe you can modify it in some way? If not, just ignore this post. :)


void SaveScreenshot(char *filename) {
SDL_Surface *temp;
unsigned char *pixels;
int i;

if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
temp = SDL_CreateRGBSurface(SDL_SWSURFACE, MainScreen->w, MainScreen->h, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
else
temp = SDL_CreateRGBSurface(SDL_SWSURFACE, MainScreen->w, MainScreen->h, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);

if (temp != NULL) {
pixels = malloc(3 * MainScreen->w * MainScreen->h);
if (pixels != NULL) {
glReadPixels(0, 0, MainScreen->w, MainScreen->h, GL_RGB, GL_UNSIGNED_BYTE, pixels);

for (i = 0; i < MainScreen->h; i++)
memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3 * MainScreen->w * (MainScreen->h-i-1), MainScreen->w * 3);
free(pixels);

SDL_SaveBMP(temp, filename);
}
SDL_FreeSurface(temp);
}

Message(LOG_GAME, "Screenshot saved as &y%s&w.", filename);
}

Share this post


Link to post
Quote:
Original post by KungFooMasta
I'm new to OpenGL programming, and I'm using OpenGL ES, trying to do a simple screen capture and save it to disk. I've seen people mention I need to use glReadPixels and flip the bytes, or restructure in BGR mode, etc.

In case you didn't know, you need to change pixel format and flip the bytes because that is how a .bmp is stored. The pixels are in reverse order and the format is BGR. There isn't a shortcut to that - it's just the file format. It really isn't that tough, though. Perhaps a different format is less "backwards".

Share this post


Link to post
Share on other sites
Quote:
Original post by KungFooMasta
I'm new to OpenGL programming, and I'm using OpenGL ES, trying to do a simple screen capture and save it to disk. I've seen people mention I need to use glReadPixels and flip the bytes, or restructure in BGR mode, etc. I've seen code to write a TGA, but I'm unable to view those types of files. Is there an easy way to create a bitmap or jpeg?

Any help is appreciated.
Irfanview is a really small, free, viewer that can view TGA files.

Share this post


Link to post
Share on other sites
I would think that BMP would be horrible for screen capture. A simple print screen pasted to "paint" creates a file that is 1.25 megabytes. Do this often and your wasting allot of space.

Share this post


Link to post
Share on other sites
I would suggest that before you look into making a jpg, you should check out the .png format. It's a bit easier to construct in memory ( a lot easier if you use libpng or something similar. ) jpgs can be a pain to construct in memory.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!