GLubyte *LoadTga(char *filename, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat) { int idlength = 0; int haspalette = 0; int type = 0; int palstart = 0; int pallength = 0; int bpe = 0; int xorg = 0, yorg = 0; int width = 0, height = 0; int bpp = 0; int attrib = 0; GLubyte *picture = 0; int i; FILE *tga; if ((tga = fopen(filename, "rb")) == NULL) { fprintf(stderr, "Error: could not open file: %s\n", filename); exit(2); } fread(&idlength, 1, 1, tga); fread(&haspalette, 1, 1, tga); fread(&type, 1, 1, tga); fread(&palstart, 2, 1, tga); fread(&pallength, 2, 1, tga); fread(&bpe, 1, 1, tga); fread(&xorg, 2, 1, tga); fread(&yorg, 2, 1, tga); fread(&width, 2, 1, tga); fread(&height, 2, 1, tga); fread(&bpp, 1, 1, tga); fread(&attrib, 1, 1, tga); if (type != 2 || haspalette) { fprintf(stderr, "Format is not supported\n"); exit(2); } fseek(tga, idlength, SEEK_CUR); picture = (GLubyte *) malloc(height*width*bpp/8); for (i = 0; i < width*height*bpp/8;) { picture[i+2] = getc(tga); picture[i+1] = getc(tga); picture[i] = getc(tga); i+=3; } *iComponents = bpp/8; *iWidth = width; *iHeight = height; if (bpp == 24) *eFormat = GL_RGB; else if (bpp == 32) *eFormat = GL_RGBA; else { fprintf(stderr, "Unknown size of bits per pixel\n"); exit(2); } return picture; }