00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "ExManagerTexture.h"
00025
00026 ExManagerTexture::ExManagerTexture(void)
00027 {
00028 Guard(ExManagerTexture::ExManagerTexture(void))
00029 m_CouterTextures=0;
00030 UnGuard
00031 }
00032
00033
00034 ExManagerTexture::~ExManagerTexture(void)
00035 {
00036 Guard(ExManagerTexture::~ExManagerTexture(void))
00037 UnGuard
00038 }
00039
00040
00041 void ExManagerTexture::Reset(void)
00042 {
00043 Guard(void ExManagerTexture::Reset(void))
00044 m_VecTextures.clear();
00045 m_CouterTextures=0;
00046 UnGuard
00047 }
00048
00049
00050 void ExManagerTexture::Init(void)
00051 {
00052 Guard(void ExManagerTexture::Init(void))
00053 UnGuard
00054 }
00055
00056
00057 bool ExManagerTexture::Init(char * FileName)
00058 {
00059 Guard(bool ExManagerTexture::Init(char * FileName))
00060
00061 return true;
00062 UnGuard
00063 }
00064
00065 bool ExManagerTexture::CheckExist(const char * name)
00066 {
00067 Guard(ExManagerTexture::CheckExist(const char * name))
00068 std::string FileName(name);
00069 if(FileName.find(".")<FileName.length())
00070 {
00071 FileName.erase(FileName.find("."),FileName.length()-FileName.find("."));
00072 }
00073 if(m_CouterTextures>0)
00074 {
00075 for(m_ItVecTextures=m_VecTextures.begin();m_ItVecTextures!=m_VecTextures.end();m_ItVecTextures++)
00076 {
00077
00078 if(strcmp(m_ItVecTextures->GetFileName(),FileName.data())==0)
00079 {
00080 return true;
00081 }
00082 }
00083 }
00084 return false;
00085 UnGuard
00086 }
00087
00088
00089 bool ExManagerTexture::AddTexture(const char * name)
00090 {
00091 Guard(ExManagerTexture::AddTexture(const char * name))
00092 if(CheckExist(name)){return true;}
00093
00094 ExCTexture *texture;
00095 bool Loaded=false;
00096 tImage *image;
00097 std::string StrPath,FileName(name);
00098 image= new tImage();
00099 texture= new ExCTexture;
00100 #ifdef UNIX_SRC
00101 StrPath=PREFIX "/ExNihilo/Data/Textures/";
00102 #else
00103 StrPath="../Data/Textures/";
00104 #endif
00105
00106 if(FileName.find(".bmp")==(FileName.length()-4))
00107 {
00108 Loaded=LoadBMP(image,StrPath+FileName);
00109 if(!Loaded)return false;
00110 }
00111 if(FileName.find(".tga")==(FileName.length()-4))
00112 {
00113 Loaded=LoadTGA(image,StrPath+FileName);
00114 if(!Loaded)return false;
00115 }
00116 if(FileName.find(".jpg")==(FileName.length()-4))
00117 {
00118 Loaded=LoadJPG(image,StrPath+FileName);
00119 if(!Loaded)return false;
00120 }
00121
00122 if(!Loaded)
00123 {
00124
00125 if(FileName.find(".")<FileName.length())
00126 {
00127
00128 *Consol<<"File format not supported :"<<FileName.data()<<std::endl;
00129 }
00130 }
00131
00132 if(!Loaded)Loaded = LoadBMP(image,StrPath+FileName+".bmp");
00133 if(!Loaded)Loaded = LoadTGA(image,StrPath+FileName+".tga");
00134 if(!Loaded)Loaded = LoadJPG(image,StrPath+FileName+".jpg");
00135 if(Loaded)
00136 {
00137 texture->SetFileName(name);
00138 m_CouterTextures++;
00139
00140 glGenTextures(1,&texture->m_Texture);
00141 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00142 glBindTexture(GL_TEXTURE_2D,texture->m_Texture);
00143
00144 int textureType = GL_RGB;
00145
00146 if(image->channels == 4)textureType = GL_RGBA;
00147
00148
00149 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00150 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00151 gluBuild2DMipmaps(GL_TEXTURE_2D,4, image->sizeX, image->sizeY, textureType, GL_UNSIGNED_BYTE, image->data);
00152
00153
00154
00155
00156 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00157 m_VecTextures.push_back(*texture);
00158 *Consol<<"MannagerTexture:: New texture added "<<texture->GetFileName()<<" with Id:"<<texture->m_Texture<<std::endl;
00159 return true;
00160 }
00161 *Consol<<"MannagerTexture:: Error loading texture :"<<FileName.data()<<std::endl;
00162 return false;
00163 UnGuard
00164 }
00165
00166
00167 GLuint ExManagerTexture::GetTexture(char * name)
00168 {
00169 Guard(ExManagerTexture::GetTexture(char * name))
00170 for(m_ItVecTextures=m_VecTextures.begin();m_ItVecTextures!=m_VecTextures.end();m_ItVecTextures++)
00171 {
00172 if(strcmp(m_ItVecTextures->GetFileName(),name)==0)
00173 {
00174 return m_ItVecTextures->GetGlTextureId();
00175 }
00176 }
00177 return (GLuint) 0;
00178 UnGuard
00179 }
00180
00181 bool ExManagerTexture::SetActiveTexture(char * name)
00182 {
00183 Guard(ExManagerTexture::SetActiveTexture(char * name))
00184 std::string FileName(name);
00185 if(FileName.find(".")<FileName.length())
00186 {
00187 FileName.erase(FileName.find("."),FileName.length()-FileName.find("."));
00188 memset(name,0,strlen(name));
00189 sprintf(name,"%s",FileName.data());
00190 }
00191 for(m_ItVecTextures=m_VecTextures.begin();m_ItVecTextures!=m_VecTextures.end();m_ItVecTextures++)
00192 {
00193 if (m_ItVecTextures->GetFileName () != NULL && name != NULL)
00194 {
00195 if (strcmp(m_ItVecTextures->GetFileName(),name)==0)
00196 {
00197 m_ItVecTextures->SetActive();
00198 return true;
00199 }
00200 }
00201 }
00202 return false;
00203 UnGuard
00204 }
00205
00206 void ExManagerTexture::ListAllTexture(void)
00207 {
00208 Guard(ExManagerTexture::ListAllTexture(void))
00209 for(m_ItVecTextures=m_VecTextures.begin();m_ItVecTextures!=m_VecTextures.end();m_ItVecTextures++)
00210 {
00211 *Consol<<"Texture Id:"<<m_ItVecTextures->m_Texture<<" Material name:"<<m_ItVecTextures->GetFileName()<<std::endl;
00212 }
00213 *Consol<<"Number of texture in manager :"<<m_VecTextures.size()<<std::endl;
00214 UnGuard
00215 }
00216