]> git.jsancho.org Git - lugaru.git/blob - Source/Graphic/Texture.cpp
Applied clang-format on all files
[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/Texture.hpp"
22
23 #include "Utils/Folders.hpp"
24 #include "Utils/ImageIO.hpp"
25
26 using namespace std;
27
28 extern bool trilinear;
29
30 void TextureRes::load()
31 {
32     ImageRec texture;
33
34     //load image into 'texture'
35     if (!load_image(filename.c_str(), texture)) {
36         cerr << "Texture " << filename << " loading failed" << endl;
37         return;
38     }
39
40     skinsize = texture.sizeX;
41     GLuint type = GL_RGBA;
42     if (texture.bpp == 24)
43         type = GL_RGB;
44
45     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
46
47     glDeleteTextures(1, &id);
48     glGenTextures(1, &id);
49     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
50
51     glBindTexture(GL_TEXTURE_2D, id);
52     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
53     if (hasMipmap) {
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);
56     } else {
57         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
58     }
59
60     if (isSkin) {
61         free(data);
62         const int nb = texture.sizeY * texture.sizeX * (texture.bpp / 8);
63         data = (GLubyte*)malloc(nb * sizeof(GLubyte));
64         datalen = 0;
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);
69     } else {
70         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
71     }
72 }
73
74 void TextureRes::bind()
75 {
76     glBindTexture(GL_TEXTURE_2D, id);
77 }
78
79 TextureRes::TextureRes(const string& _filename, bool _hasMipmap)
80     : id(0)
81     , filename(_filename)
82     , hasMipmap(_hasMipmap)
83     , isSkin(false)
84     , skinsize(0)
85     , data(NULL)
86     , datalen(0)
87 {
88     load();
89 }
90
91 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep)
92     : id(0)
93     , filename(_filename)
94     , hasMipmap(_hasMipmap)
95     , isSkin(true)
96     , skinsize(0)
97     , data(NULL)
98     , datalen(0)
99 {
100     load();
101     *skinsizep = skinsize;
102     for (int i = 0; i < datalen; i++) {
103         array[i] = data[i];
104     }
105 }
106
107 TextureRes::~TextureRes()
108 {
109     free(data);
110     glDeleteTextures(1, &id);
111 }
112
113 void Texture::load(const string& filename, bool hasMipmap)
114 {
115     tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap));
116 }
117
118 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep)
119 {
120     tex.reset(new TextureRes(Folders::getResourcePath(filename), hasMipmap, array, skinsizep));
121 }
122
123 void Texture::bind()
124 {
125     if (tex)
126         tex->bind();
127     else
128         glBindTexture(GL_TEXTURE_2D, 0);
129 }