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