The advantage of using a TGA file as a texture than a BMP file is that you do not need to use the aux library for loading the TGA file, and the TGA texture can have an alpha channel. When loading a TGA file as a texture, you must understand the file structure of the TGA file. The file structure of the TGA file is shown in the following table.
Offset |
Length |
Description |
Values of each byte in a 32-bit common image file |
0 |
1 |
It indicates the length of the image information field. The value range is 0 to 255. When it is 0, it indicates that there is no image information field. |
0 |
1 |
1 |
Whether to use a color table. 0 indicates no color table. 1 indicates that the color table exists. |
0 |
2 |
1 |
This field is always 2. Image type code, TGA a total of 6 formats, 2 indicates no color table RGB image |
2 |
3 |
5 |
Color Table type, total 0. |
0 |
4 |
0 |
5 |
0 |
6 |
0 |
7 |
0 |
8 10 image specifications |
8 |
2 |
Starting position of the X coordinate of the image, usually 0 |
0 |
9 |
10 |
2 |
Starting position of the Y coordinate of the image, usually 0 |
0 |
11 |
12 |
2 |
Image Width, in pixels |
256 |
13 |
14 |
2 |
Image Height, in pixels |
256 |
15 |
16 |
1 |
Number of bit occupied by Image Storage per pixel |
32 |
17 |
1 |
Image Descriptor bytes Bits 3-0-the number of BITs corresponding to each pixel. For TGA 24, this value is 0. Bit 4-reserved, must be 0 Bit 5-indicates the start position of the screen. 0 = the origin is in the lower left corner, and 1 = the origin is in the upper left corner. Generally, set this byte to 0x00. |
00100000 (2) |
18 |
Variable |
Image data domain Here (width) x (height) pixels are stored. The RGB color value in each pixel contains an integer byte. |
... |
When reading a TGA file, the most important thing is to analyze the file header information of the TGA file. We can see that the first 12 bytes of the TGA file can be used as the feature of the TGA file. If the first 12 bytes of the file to be read are 0, 0, 2, 0... then we can determine that this file is indeed a TGA file. The last 6 bytes of a total of 18 bytes provide information about the TGA file, including the width, height, and the number of digits contained in each bit, after you confirm that the file is a TGA file and read the file, We need to extract the information about this TGA file from this part, and then construct the structure of a TGA file based on the information, the general style of this structure is as follows. Struct texture
...{
Gluint imagesize;
Gluint bitperpixel;
Gluint width;
Gluint height;
Glubyte * data;
};
After we load the TGA file into the memory, we process it like a general texture, declare the texture handle, and use glteximage2d to load it after binding, after binding in our rendering phase, specify the texture coordinates and then render the general steps.
The following is a function for loading TGA files. Bool loadtexture (const char * filename)
...{
If (! Filename)
Return false;
File * file = fopen (filename, "R ");
If (file)
...{
Glubyte fileheader [12];
Glubyte compare [12] =... {0, 2, 0 };
Glubyte info [6];
If (fread (fileheader, 1, 12, file )! = 12 | memcmp (fileheader, compare, sizeof (compare )))
Return false;
If (fread (Info, 1, 6, file )! = 6)
Return false;
Texture Tex;
Tex. width = 256 * info [1] + info [0];
Tex. Height = 256 * info [3] + info [2];
Tex. bitperpixel = info [4];
Gluint type;
If (Tex. bitperpixel = 24)
Type = gl_rgb;
Else
Type = gl_rgba;
Tex. imagesize = Tex. Width * Tex. Height * Tex. bitperpixel/8;
Tex. Data = new glubyte [Tex. imagesize];
If (fread (Tex. Data, 1, Tex. imagesize, file )! = Tex. imagesize)
...{
Fclose (File );
Delete [] Tex. Data;
Return false;
}
Fclose (File );
For (INT I = 0; I <(INT) Tex. imagesize; I ++)
...{
Glubyte temp = Tex. Data [I];
Tex. Data [I] = Tex. Data [I + 2];
Tex. Data [I + 2] = temp;
}
Glgentextures (1, & texhandle );
Glbindtexture (gl_texture_2d, texhandle );
Glteximage2d (gl_texture_2d, 0, type, Tex. Width, Tex. Height, 0, type, gl_unsigned_byte, Tex. data );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
If (Tex. Data)
Free (Tex. data );
Return true;
}
Return false;
}