Jump to content
  • Advertisement
Sign in to follow this  
blazek

OpenGL save to file an opengl texture

This topic is 4523 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

hello! How can i save my gluint texture or screen into file (jpeg, bmp, or anything else). I use also sdl. Thanks :-)

Share this post


Link to post
Share on other sites
Advertisement
It's pretty simple: create an array with space enough to hold all the texel data (width * height * channels in internal format for a 2d texture) and use glGetTexImage. After you have the pixels, use SDL (there was something like SDL_SaveBMP, don't quite remember) or your own image data file functions to save to an image format you like.

Share this post


Link to post
Share on other sites
Draw it and read back the texture into a color array. now you can write it out with an SDL routine or your own imageToHarddisk function like this one (writes to TGA):


int awx_ScreenShot()
{
unsigned char *pixels;
FILE *image;

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

pixels = new unsigned char[viewport[2]*viewport[3]*3];

glReadPixels(0, 0, viewport[2], viewport[3], GL_BGR,
GL_UNSIGNED_BYTE, pixels);

char tempstring[50];
sprintf(tempstring,"Screenshots/screenshot_%i.tga",1);
if((image=fopen(tempstring, "wb"))==NULL) return 1;

unsigned char TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};

unsigned char header[6]={((int)(viewport[2]%256)),
((int)(viewport[2]/256)),
((int)(viewport[3]%256)),
((int)(viewport[3]/256)),24,0};

// TGA header schreiben
fwrite(TGAheader, sizeof(unsigned char), 12, image);
// Header schreiben
fwrite(header, sizeof(unsigned char), 6, image);

fwrite(pixels, sizeof(unsigned char),
viewport[2]*viewport[3]*3, image);

fclose(image);
delete [] pixels;

return 0;
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!