]> git.jsancho.org Git - lugaru.git/blob - Source/Texture.cpp
11f421129df60970ddbc7e5d088c21dadacbcd59
[lugaru.git] / Source / Texture.cpp
1 #include "gamegl.h"
2 #include "Texture.h"
3 #include "TGALoader.h"
4
5 using namespace std;
6
7 map<string,Texture> Texture::textures;
8
9 extern TGAImageRec texture;
10 extern bool trilinear;
11
12 Texture::~Texture()  {
13         free(array);
14     glDeleteTextures(1,&id);
15 }
16
17 void Texture::load()  {
18         GLuint type;
19
20         LOGFUNC;
21
22         LOG(std::string("Loading texture...") + fileName);
23
24         unsigned char fileNamep[256];
25         CopyCStringToPascal(ConvertFileName(fileName.c_str()), fileNamep);
26         //Load Image
27         upload_image( fileNamep ,hasalpha);
28         
29         //Alpha channel?
30         if ( texture.bpp == 24 )
31                 type = GL_RGB;
32         else
33                 type = GL_RGBA;
34
35         glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
36
37         if(!id)
38                 glGenTextures( 1, &id );
39         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
40
41         glBindTexture( GL_TEXTURE_2D, id);
42         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
43         if(mipmap)
44                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (trilinear?GL_LINEAR_MIPMAP_LINEAR:GL_LINEAR_MIPMAP_NEAREST) );
45         else
46                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
47         glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );
48         
49         skinsize=texture.sizeX;
50         
51         if(isSkin) {
52                 int tempnum=0;
53                 int nb = (texture.sizeY*texture.sizeX*(texture.bpp/8));
54                 array = (GLubyte*)malloc(nb*sizeof(GLubyte));
55                 for(int i=0;i<nb;i++) {
56                         if((i+1)%4||type==GL_RGB) {
57                                 array[tempnum]=texture.data[i];
58                                 tempnum++;
59                         }
60                 }
61                 arraySize=tempnum;
62
63         glTexImage2D( GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, array );
64         } else {
65         glTexImage2D( GL_TEXTURE_2D, 0, type, texture.sizeX, texture.sizeY, 0, type, GL_UNSIGNED_BYTE, texture.data );
66         }
67 }
68
69 GLuint Texture::Load(const string& fileName, bool mipmap, bool hasalpha) {
70         map<string,Texture>::iterator it = textures.find(fileName);
71         if(it==textures.end()) {
72                 textures.insert(make_pair(fileName,Texture(fileName,mipmap,hasalpha)));
73                 textures[fileName].load();
74                 return textures[fileName].getId();
75         } else {
76                 return it->second.getId();
77         }
78 }
79
80 GLuint Texture::Load(const string& fileName, bool mipmap, bool hasalpha, GLubyte* array, int* skinsize) {
81         map<string,Texture>::iterator it = textures.find(fileName);
82         if(it==textures.end()) {
83                 textures.insert(make_pair(fileName,Texture(fileName,mipmap,hasalpha,true)));
84                 textures[fileName].load();
85                 *skinsize = textures[fileName].skinsize;
86                 for(int i=0;i<textures[fileName].arraySize;i++) {
87                         array[i] = textures[fileName].array[i];
88                 }
89                 return textures[fileName].getId();
90         } else {
91                 *skinsize = it->second.skinsize;
92                 for(int i=0;i<it->second.arraySize;i++) {
93                         array[i] = it->second.array[i];
94                 }
95                 return it->second.getId();
96         }
97 }
98