2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
27 extern ImageRec texture;
28 extern bool trilinear;
34 static vector<TextureRes*> list;
49 TextureRes(const string& filename, bool hasMipmap, bool hasAlpha);
50 TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize);
54 static void reloadAll();
58 vector<TextureRes*> TextureRes::list;
60 void TextureRes::load()
62 //load image into 'texture' global var
64 upload_image(ConvertFileName(filename.c_str()));
67 skinsize = texture.sizeX;
68 GLuint type = GL_RGBA;
69 if (texture.bpp == 24)
72 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
74 glDeleteTextures(1, &id);
75 glGenTextures(1, &id);
76 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
78 glBindTexture(GL_TEXTURE_2D, id);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
82 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
89 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skinsize, skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, skindata);
92 const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
93 data = (GLubyte*)malloc(nb * sizeof(GLubyte));
95 for (int i = 0; i < nb; i++)
96 if ((i + 1) % 4 || type == GL_RGB)
97 data[datalen++] = texture.data[i];
98 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
101 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
105 void TextureRes::bind()
107 glBindTexture(GL_TEXTURE_2D, id);
110 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha):
111 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(_hasAlpha), isSkin(false),
112 skinsize(0), data(NULL), datalen(0), skindata(NULL)
115 list.push_back(this);
118 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
119 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(false), isSkin(true),
120 skinsize(0), data(NULL), datalen(0), skindata(NULL)
123 *skinsizep = skinsize;
124 for (int i = 0; i < datalen; i++)
127 list.push_back(this);
130 TextureRes::~TextureRes()
133 glDeleteTextures(1, &id);
134 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
141 void TextureRes::reloadAll()
143 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++) {
150 void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha)
153 tex = new TextureRes(filename, hasMipmap, hasAlpha);
156 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
159 tex = new TextureRes(filename, hasMipmap, array, skinsizep);
162 void Texture::destroy()
175 glBindTexture(GL_TEXTURE_2D, 0);
178 void Texture::reloadAll()
180 TextureRes::reloadAll();