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;
30 vector<TextureRes*> TextureRes::list;
32 void TextureRes::load()
34 //load image into 'texture' global var
35 upload_image(ConvertFileName(filename.c_str()));
37 skinsize = texture.sizeX;
38 GLuint type = GL_RGBA;
39 if (texture.bpp == 24)
42 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
44 glDeleteTextures(1, &id);
45 glGenTextures(1, &id);
46 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
48 glBindTexture(GL_TEXTURE_2D, id);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
52 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
59 const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
60 data = (GLubyte*)malloc(nb * sizeof(GLubyte));
62 for (int i = 0; i < nb; i++)
63 if ((i + 1) % 4 || type == GL_RGB)
64 data[datalen++] = texture.data[i];
65 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
67 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
71 void TextureRes::bind()
73 glBindTexture(GL_TEXTURE_2D, id);
76 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha):
77 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(_hasAlpha), isSkin(false),
78 skinsize(0), data(NULL), datalen(0)
84 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
85 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(false), isSkin(true),
86 skinsize(0), data(NULL), datalen(0)
89 *skinsizep = skinsize;
90 for (int i = 0; i < datalen; i++)
95 TextureRes::~TextureRes()
98 glDeleteTextures(1, &id);
99 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
106 void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha)
109 tex = new TextureRes(filename, hasMipmap, hasAlpha);
112 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
115 tex = new TextureRes(filename, hasMipmap, array, skinsizep);
118 void Texture::destroy()
131 glBindTexture(GL_TEXTURE_2D, 0);