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/>.
21 #include "Graphic/Texture.hpp"
23 #include "Utils/Folders.hpp"
24 #include "Utils/ImageIO.hpp"
28 extern bool trilinear;
30 void TextureRes::load()
34 //load image into 'texture'
35 if (!load_image(filename.c_str(), texture)) {
36 cerr << "Texture " << filename << " loading failed" << endl;
40 skinsize = texture.sizeX;
41 GLuint type = GL_RGBA;
42 if (texture.bpp == 24)
45 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
47 glDeleteTextures(1, &id);
48 glGenTextures(1, &id);
49 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
51 glBindTexture(GL_TEXTURE_2D, id);
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
55 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
62 const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
63 data = (GLubyte*)malloc(nb * sizeof(GLubyte));
65 for (int i = 0; i < nb; i++)
66 if ((i + 1) % 4 || type == GL_RGB)
67 data[datalen++] = texture.data[i];
68 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
70 glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
74 void TextureRes::bind()
76 glBindTexture(GL_TEXTURE_2D, id);
79 TextureRes::TextureRes(const string& _filename, bool _hasMipmap)
82 , hasMipmap(_hasMipmap)
91 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep)
94 , hasMipmap(_hasMipmap)
101 *skinsizep = skinsize;
102 for (int i = 0; i < datalen; i++) {
107 TextureRes::~TextureRes()
110 glDeleteTextures(1, &id);
113 void Texture::load(const string& filename, bool hasMipmap)
115 tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap));
118 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
120 tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap, array, skinsizep));
128 glBindTexture(GL_TEXTURE_2D, 0);