Sign in to follow this  
tjisgnarly

TGA files

Recommended Posts

tjisgnarly    122
In tutorial 32 it explains how to make TGA files. I couldn't follow this expination and was wondering if anyone could explain it a little better for me?

Share this post


Link to post
Share on other sites
taby    1265
This is a good explanation, very simple:
http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/

Share this post


Link to post
Share on other sites
taby    1265
Here's some C++ code to output a sample TGA with transparency, using the information on Paul Bourke's website:


#include <fstream>
using std::ofstream;
using std::endl;

#include <ios>
using std::ios_base;

int main(void)
{
// Assumes char is 1 byte, short is 2 bytes.
char temp_c = 0;
short temp_s = 0;
ofstream out("test.tga", ios_base::binary);

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // idlength
out.write(&temp_c, sizeof(temp_c)); // colourmaptype

temp_c = 2;
out.write(&temp_c, sizeof(temp_c)); // datatypecode (2 == uncompressed RGB(A) data)

temp_s = 0;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // colourmaporigin
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // colourmaplength

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // colourmapdepth

temp_s = 0;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // x_origin
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // y_origin

temp_s = 128;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // width (128 pixels wide)

temp_s = 64;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // height (64 pixels tall)

temp_c = 32;
out.write(&temp_c, sizeof(temp_c)); // bitsperpixel (32 bits per pixel)

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // imagedescriptor

unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 128;
unsigned char a = 0;

// output 128*64*4 bytes of data
for(short y = 0; y < 64; y++)
{
// For each line:
r += 2; // Increase red by 2
g = 0; // Reset green to 0
a = 255; // Reset alpha to 255 (fully opaque)

for(short x = 0; x < 128; x++)
{
// For each pixel in each line:
g++; // Increase green by 1
a--; // Decrease alpha by 1

out.write(reinterpret_cast<const char *>(&r), sizeof(r));
out.write(reinterpret_cast<const char *>(&g), sizeof(g));
out.write(reinterpret_cast<const char *>(&b), sizeof(b));
out.write(reinterpret_cast<const char *>(&a), sizeof(a));
}
}

return 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