]> git.jsancho.org Git - lugaru.git/blob - Source/Graphic/Texture.cpp
Sorted all source files in folders
[lugaru.git] / Source / Graphic / Texture.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "Graphic/gamegl.h"
22 #include "Graphic/Texture.h"
23 #include "Utils/Folders.h"
24 #include "Utils/ImageIO.h"
25
26 using namespace std;
27
28 extern bool trilinear;
29
30 vector<TextureRes*> TextureRes::list;
31
32 void TextureRes::load()
33 {
34     ImageRec texture;
35
36     //load image into 'texture'
37     if (!load_image(filename.c_str(), texture)) {
38         cerr << "Texture " << filename << " loading failed" << endl;
39         return;
40     }
41
42     skinsize = texture.sizeX;
43     GLuint type = GL_RGBA;
44     if (texture.bpp == 24)
45         type = GL_RGB;
46
47     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
48
49     glDeleteTextures(1, &id);
50     glGenTextures(1, &id);
51     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
52
53     glBindTexture(GL_TEXTURE_2D, id);
54     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
55     if (hasMipmap) {
56         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST));
57         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
58     } else {
59         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60     }
61
62     if (isSkin) {
63         free(data);
64         const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
65         data = (GLubyte*)malloc(nb * sizeof(GLubyte));
66         datalen = 0;
67         for (int i = 0; i < nb; i++)
68             if ((i + 1) % 4 || type == GL_RGB)
69                 data[datalen++] = texture.data[i];
70         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
71     } else {
72         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
73     }
74 }
75
76 void TextureRes::bind()
77 {
78     glBindTexture(GL_TEXTURE_2D, id);
79 }
80
81 TextureRes::TextureRes(const string& _filename, bool _hasMipmap):
82     id(0), filename(_filename), hasMipmap(_hasMipmap), isSkin(false),
83     skinsize(0), data(NULL), datalen(0)
84 {
85     load();
86     list.push_back(this);
87 }
88
89 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
90     id(0), filename(_filename), hasMipmap(_hasMipmap), isSkin(true),
91     skinsize(0), data(NULL), datalen(0)
92 {
93     load();
94     *skinsizep = skinsize;
95     for (int i = 0; i < datalen; i++)
96         array[i] = data[i];
97     list.push_back(this);
98 }
99
100 TextureRes::~TextureRes()
101 {
102     free(data);
103     glDeleteTextures(1, &id);
104     for (vector<TextureRes*>::iterator it = list.begin(); it != list.end(); it++)
105         if (*it == this) {
106             list.erase(it);
107             break;
108         }
109 }
110
111 void Texture::load(const string& filename, bool hasMipmap)
112 {
113     destroy();
114     tex = new TextureRes(Folders::getResourcePath(filename), hasMipmap);
115 }
116
117 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
118 {
119     destroy();
120     tex = new TextureRes(Folders::getResourcePath(filename), hasMipmap, array, skinsizep);
121 }
122
123 void Texture::destroy()
124 {
125     if (tex) {
126         delete tex;
127         tex = NULL;
128     }
129 }
130
131 void Texture::bind()
132 {
133     if (tex)
134         tex->bind();
135     else
136         glBindTexture(GL_TEXTURE_2D, 0);
137 }