]> git.jsancho.org Git - lugaru.git/blob - Source/Texture.cpp
ef20536da7b37f7268607d0a52ba0e44df69569e
[lugaru.git] / Source / Texture.cpp
1 #include "gamegl.h"
2 #include "Texture.h"
3 #include "TGALoader.h"
4
5 using namespace std;
6
7 extern TGAImageRec texture;
8 extern bool trilinear;
9
10
11 class TextureRes {
12 private:
13     static vector<TextureRes*> list;
14
15     GLuint id;
16     string filename;
17     bool hasMipmap;
18     bool hasAlpha;
19     bool isSkin;
20     int skinsize;
21     GLubyte* data;
22     int datalen;
23     GLubyte* skindata;
24
25     void load();
26
27 public:
28     TextureRes(const string& filename, bool hasMipmap, bool hasAlpha);
29     TextureRes(const string& filename, bool hasMipmap, GLubyte* array, int* skinsize);
30     ~TextureRes();
31     void bind();
32
33     static void reloadAll();
34 };
35
36
37 vector<TextureRes*> TextureRes::list;
38
39 void TextureRes::load(){
40     //load image into 'texture' global var
41     if(!skindata){
42         unsigned char filenamep[256];
43         CopyCStringToPascal(ConvertFileName(filename.c_str()),filenamep);
44         upload_image(filenamep,hasAlpha);
45     }
46         
47         skinsize=texture.sizeX;
48         GLuint type=GL_RGBA;
49         if(texture.bpp==24)
50                 type=GL_RGB;
51         
52         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
53
54     glDeleteTextures(1,&id);
55     glGenTextures(1,&id);
56         glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
57
58         glBindTexture(GL_TEXTURE_2D, id);
59         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
60         if(hasMipmap){
61                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,(trilinear?GL_LINEAR_MIPMAP_LINEAR:GL_LINEAR_MIPMAP_NEAREST));
62         glTexParameteri(GL_TEXTURE_2D,GL_GENERATE_MIPMAP,GL_TRUE);
63     }else{
64                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
65     }
66         
67         if(isSkin){
68         if(skindata){
69             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, skinsize, skinsize, 0, GL_RGB, GL_UNSIGNED_BYTE, skindata);
70         }else{
71             free(data);
72             const int nb=texture.sizeY*texture.sizeX*(texture.bpp/8);
73             data=(GLubyte*)malloc(nb*sizeof(GLubyte));
74             datalen=0;
75             for(int i=0;i<nb;i++)
76                 if((i+1)%4||type==GL_RGB)
77                     data[datalen++]=texture.data[i];
78             glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
79         }
80         }else{
81         glTexImage2D(GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data);
82         }
83 }
84
85 void TextureRes::bind(){
86     glBindTexture(GL_TEXTURE_2D,id);
87 }
88
89 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, bool _hasAlpha):
90     id(0),filename(_filename),hasMipmap(_hasMipmap),hasAlpha(_hasAlpha),isSkin(false),
91     skinsize(0),data(NULL),datalen(0),skindata(NULL) {
92     load();
93     list.push_back(this);
94 }
95
96 TextureRes::TextureRes(const string& _filename, bool _hasMipmap, GLubyte* array, int* skinsizep):
97     id(0),filename(_filename),hasMipmap(_hasMipmap),hasAlpha(false),isSkin(true),
98     skinsize(0),data(NULL),datalen(0),skindata(NULL) {
99     load();
100     *skinsizep=skinsize;
101     for(int i=0;i<datalen;i++)
102         array[i]=data[i];
103     skindata=array;
104     list.push_back(this);
105 }
106
107 TextureRes::~TextureRes(){
108     free(data);
109     glDeleteTextures(1,&id);
110     for(vector<TextureRes*>::iterator it=list.begin();it!=list.end();it++)
111         if(*it==this){
112             list.erase(it);
113             break;
114         }
115 }
116
117 void TextureRes::reloadAll(){
118     for(vector<TextureRes*>::iterator it=list.begin();it!=list.end();it++){
119         (*it)->id=0;
120         (*it)->load();
121     }
122 }
123
124
125
126
127 void Texture::load(const string& filename, bool hasMipmap, bool hasAlpha){
128     destroy();
129     tex=new TextureRes(filename,hasMipmap,hasAlpha);
130 }
131
132 void Texture::load(const string& filename, bool hasMipmap, GLubyte* array, int* skinsizep){
133     destroy();
134     tex=new TextureRes(filename,hasMipmap,array,skinsizep);
135 }
136
137 void Texture::destroy(){
138     if(tex){
139         delete tex;
140         tex=NULL;
141     }
142 }
143
144 void Texture::bind(){
145     if(tex)
146         tex->bind();
147     else
148         glBindTexture(GL_TEXTURE_2D,0);
149 }
150
151 void Texture::reloadAll(){
152     TextureRes::reloadAll();
153 }
154