7 extern TGAImageRec texture;
14 static vector<TextureRes*> list;
29 TextureRes(const string& filename, bool hasMipmap, bool hasAlpha);
30 TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize);
34 static void reloadAll();
38 vector<TextureRes*> TextureRes::list;
40 void TextureRes::load()
42 //load image into 'texture' global var
44 unsigned char filenamep[256];
45 CopyCStringToPascal(ConvertFileName(filename.c_str()), filenamep);
46 upload_image(filenamep, hasAlpha);
49 skinsize = texture.sizeX;
50 GLuint type = GL_RGBA;
51 if (texture.bpp == 24)
54 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
56 glDeleteTextures(1, &id);
57 glGenTextures(1, &id);
58 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
60 glBindTexture(GL_TEXTURE_2D, id);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
64 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skinsize, skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, skindata);
74 const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
75 data = (GLubyte*)malloc(nb * sizeof(GLubyte));
77 for (int i = 0; i < nb; i++)
78 if ((i + 1) % 4 || type == GL_RGB)
79 data[datalen++] = texture.data[i];
80 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
83 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
87 void TextureRes::bind()
89 glBindTexture(GL_TEXTURE_2D, id);
92 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha):
93 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(_hasAlpha), isSkin(false),
94 skinsize(0), data(NULL), datalen(0), skindata(NULL)
100 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
101 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(false), isSkin(true),
102 skinsize(0), data(NULL), datalen(0), skindata(NULL)
105 *skinsizep = skinsize;
106 for (int i = 0; i < datalen; i++)
109 list.push_back(this);
112 TextureRes::~TextureRes()
115 glDeleteTextures(1, &id);
116 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
123 void TextureRes::reloadAll()
125 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++) {
134 void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha)
137 tex = new TextureRes(filename, hasMipmap, hasAlpha);
140 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
143 tex = new TextureRes(filename, hasMipmap, array, skinsizep);
146 void Texture::destroy()
159 glBindTexture(GL_TEXTURE_2D, 0);
162 void Texture::reloadAll()
164 TextureRes::reloadAll();