Skip to content

Instantly share code, notes, and snippets.

@sjhalayka
Created November 11, 2020 04:22
Show Gist options
  • Save sjhalayka/2fbca0cec01c677828e744418e2656e5 to your computer and use it in GitHub Desktop.
Save sjhalayka/2fbca0cec01c677828e744418e2656e5 to your computer and use it in GitHub Desktop.
vector<unsigned char> output_pixels(2048*2048*4);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(0, 0, 2048, 2048, GL_RGBA, GL_UNSIGNED_BYTE, &output_pixels[0]);
// Set up Targa TGA image data.
unsigned char idlength = 0;
unsigned char colourmaptype = 0;
unsigned char datatypecode = 2;
unsigned short int colourmaporigin = 0;
unsigned short int colourmaplength = 0;
unsigned char colourmapdepth = 0;
unsigned short int x_origin = 0;
unsigned short int y_origin = 0;
unsigned short int px = 2048;
unsigned short int py = 2048;
unsigned char bitsperpixel = 32;
unsigned char imagedescriptor = 0;
vector<char> idstring;
// Write Targa TGA file to disk.
ofstream out("attachment.tga", ios::binary);
if (!out.is_open())
{
cout << "Failed to open TGA file for writing: attachment.tga" << endl;
return;
}
out.write(reinterpret_cast<char*>(&idlength), 1);
out.write(reinterpret_cast<char*>(&colourmaptype), 1);
out.write(reinterpret_cast<char*>(&datatypecode), 1);
out.write(reinterpret_cast<char*>(&colourmaporigin), 2);
out.write(reinterpret_cast<char*>(&colourmaplength), 2);
out.write(reinterpret_cast<char*>(&colourmapdepth), 1);
out.write(reinterpret_cast<char*>(&x_origin), 2);
out.write(reinterpret_cast<char*>(&y_origin), 2);
out.write(reinterpret_cast<char*>(&px), 2);
out.write(reinterpret_cast<char*>(&py), 2);
out.write(reinterpret_cast<char*>(&bitsperpixel), 1);
out.write(reinterpret_cast<char*>(&imagedescriptor), 1);
out.write(reinterpret_cast<char*>(&output_pixels[0]), 2048*2048 * 4 * sizeof(unsigned char));
out.close();
exit(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment