struct TextureImage GLubyte image_data GLuint bpp GLuint GLuint GLuint

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
struct TextureImage
{
GLubyte *image_data;
GLuint bpp;
GLuint w;
GLuint h;
GLuint texID;
};
TextureImage textures[1];
bool LoadTGA(TextureImage *texture, char *filename)
{
GLubyte TGAheader[12]={0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
GLubyte TGAcompare[12];
GLubyte header[6];
GLuint bytesPerPixel;
GLuint imageSize;
GLuint temp;
GLuint type=GL_RGBA;
FILE *file=fopen(filename, "rb");
if (file==NULL || fread(TGAcompare, 1, sizeof(TGAcompare), file)!=sizeof(TGAcompare) || memcmp(TGAheader, TGAcompare, sizeof(TGAheader))!=0 || fread(header, 1, sizeof(header), file)!=sizeof(header))
{
if (file=NULL)
return false;
else
{
fclose(file);
return false;
}
}
texture->w=header[1]*256+header[0];
texture->h=header[3]*256+header[2];
if (texture->w<=0 || texture->h<=0 || (header[4]!=24 && header[4]!=32))
{
fclose(file);
return false;
}
texture->bpp=header[4];
bytesPerPixel=texture->bpp/8;
imageSize=texture->w*texture->h*bytesPerPixel;
texture->image_data=(GLubyte *) malloc(imageSize);
if (texture->image_data==NULL || fread(texture->image_data, 1, imageSize, file)!=imageSize)
{
if (texture->image_data!=NULL) free(texture->image_data);
fclose(file);
return false;
}
for (GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
{
temp=texture->image_data[i];
texture->image_data[i]=texture->image_data[i+2];
texture->image_data[i+2]=temp;
}
fclose(file);
glGenTextures(1, &texture[0].texID);
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);//&&&&
if (texture[0].bpp==24) type=GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].w, texture[0].h, 0, type, GL_UNSIGNED_BYTE, texture[0].image_data);
return true;
}
....
glEnable(GL_TEXTURE_2D);
LoadTGA(&textures[0], "wall.tga");