2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
22 #include "TGALoader.h"
26 extern ImageRec texture;
27 extern bool trilinear;
33 static vector<TextureRes*> list;
48 TextureRes(const string& filename, bool hasMipmap, bool hasAlpha);
49 TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize);
53 static void reloadAll();
57 vector<TextureRes*> TextureRes::list;
59 void TextureRes::load()
61 //load image into 'texture' global var
63 upload_image(ConvertFileName(filename.c_str()));
66 skinsize = texture.sizeX;
67 GLuint type = GL_RGBA;
68 if (texture.bpp == 24)
71 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
73 glDeleteTextures(1, &id);
74 glGenTextures(1, &id);
75 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
77 glBindTexture(GL_TEXTURE_2D, id);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
81 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
88 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skinsize, skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, skindata);
91 const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
92 data = (GLubyte*)malloc(nb * sizeof(GLubyte));
94 for (int i = 0; i < nb; i++)
95 if ((i + 1) % 4 || type == GL_RGB)
96 data[datalen++] = texture.data[i];
97 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
100 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
104 void TextureRes::bind()
106 glBindTexture(GL_TEXTURE_2D, id);
109 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha):
110 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(_hasAlpha), isSkin(false),
111 skinsize(0), data(NULL), datalen(0), skindata(NULL)
114 list.push_back(this);
117 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
118 id(0), filename(_filename), hasMipmap(_hasMipmap), hasAlpha(false), isSkin(true),
119 skinsize(0), data(NULL), datalen(0), skindata(NULL)
122 *skinsizep = skinsize;
123 for (int i = 0; i < datalen; i++)
126 list.push_back(this);
129 TextureRes::~TextureRes()
132 glDeleteTextures(1, &id);
133 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
140 void TextureRes::reloadAll()
142 for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++) {
149 void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha)
152 tex = new TextureRes(filename, hasMipmap, hasAlpha);
155 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
158 tex = new TextureRes(filename, hasMipmap, array, skinsizep);
161 void Texture::destroy()
174 glBindTexture(GL_TEXTURE_2D, 0);
177 void Texture::reloadAll()
179 TextureRes::reloadAll();