Page principale | Liste des namespaces | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Membres des namespaces | Composants | Déclarations

# ExManagerTexture.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002  * ExNihilo 3D Engine
    00003  *
    00004  *  This program is free software; you can redistribute it and/or modify
    00005  *  it under the terms of the GNU General Public License as published by
    00006  *  the Free Software Foundation; either version 2 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  This program is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU General Public License
    00015  *  along with this program; if not, write to the Free Software
    00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  *
    00018  * Please read AUTHORS file !!!
    00019  *
    00020  * $Id: ExManagerTexture.cpp,v 1.39 2002/12/13 11:45:01 data Exp $
    00021  *
    00022  */
    00023
    00024 #include "ExManagerTexture.h"
    00025
    00026 bool ExManagerTexture::m_flag = false;
    00027 ExManagerTexture* ExManagerTexture::m_instance = NULL;
    00028
    00029 ExManagerTexture* ExManagerTexture::CreateSingleton(void){
    00030 Guard(ExManagerTexture* ExManagerTexture::CreateSingleton(void))
    00031         if(!m_flag)
    00032         {
    00033                 m_flag = true; // We are creating the error log now, so set flag to true
    00034                 m_instance = new ExManagerTexture; // Create the error log
    00035         }else
    00036         {
    00037                 std::cout<<"Error singleton already created"<<std::endl;
    00038         }
    00039         return m_instance;
    00040 UnGuard
    00041 }
    00042
    00043 ExManagerTexture::ExManagerTexture(void)
    00044 {
    00045 Guard(ExManagerTexture::ExManagerTexture(void))
    00046         m_Multitexturing_level=-1;
    00047         m_Anisotropy_level=-1;
    00048         m_Anisotropy_max_level=-1;
    00049         m_Multitexturing_max_level=-1;
    00050         glMultiTexCoord2fARB = NULL;
    00051         glActiveTextureARB = NULL;
    00052         glClientActiveTextureARB = NULL;
    00053
    00054 UnGuard
    00055 }
    00056
    00057
    00058 ExManagerTexture::~ExManagerTexture(void)
    00059 {
    00060 Guard(ExManagerTexture::~ExManagerTexture(void))
    00061 UnGuard
    00062 }
    00063
    00064 void  ExManagerTexture::Init(void)
    00065 {
    00066 Guard(void  ExManagerTexture::Init(void))
    00067         ExManagerObject<ExCTexture>::Init();
    00068         *Consol<<"****************************************"<<std::endl;
    00069         *Consol<<"*ExManagerTexture Init                 *"<<std::endl;
    00070         *Consol<<"****************************************"<<std::endl;
    00071         char *blah = (char *) glGetString(GL_EXTENSIONS);
    00072         //--------------------------------
    00073         //ANISOTROPIC EXTENSION
    00074         //--------------------------------
    00075         if (strstr( (const char *)blah, "GL_EXT_texture_filter_anisotropic" ))
    00076         {
    00077                 glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT,&m_Anisotropy_max_level);
    00078                 *Consol<<"ExManagerTexture::GL_EXT_texture_filter_anisotropic enable. Maximum level:"<<m_Anisotropy_max_level<<std::endl;
    00079                 m_Anisotropy=true;
    00080                 m_Anisotropy_level=m_Anisotropy_max_level;
    00081         }else
    00082         {
    00083                 m_Anisotropy=false;
    00084         }
    00085
    00086         //--------------------------------
    00087         //MIMAP SGIS EXTENSION
    00088         //--------------------------------
    00089         if (strstr( (const char *)blah, "GL_SGIS_generate_mipmap" ))
    00090         {
    00091                 *Consol<<"ExManagerTexture::GL_SGIS_generate_mipmap enable."<<std::endl;
    00092                 m_SGIS_Mimap=true;
    00093         }else
    00094         {
    00095                 *Consol<<"ExManagerTexture::GL_SGIS_generate_mipmap not supported."<<std::endl;
    00096                 m_SGIS_Mimap=false;
    00097         }
    00098
    00099         //--------------------------------
    00100         //TEXTURING COMPRESSION EXTENSION
    00101         //--------------------------------
    00102         if(strstr( (const char *)blah, "GL_ARB_texture_compression" ))
    00103         {
    00104                 *Consol<<"ExManagerTexture::GL_ARB_texture_compression found and enable. "<<std::endl;
    00105                 m_ARB_Texture_Compression=true;
    00106         }else
    00107         {
    00108                 *Consol<<"ExManagerTexture::GL_ARB_texture_compression extension missing. "<<std::endl;
    00109                 m_ARB_Texture_Compression=false;
    00110         }
    00111
    00112         //--------------------------------
    00113         //MULTITEXTURING EXTENSION
    00114         //--------------------------------
    00115         if (strstr( (const char *)blah, "GL_ARB_multitexture" ))
    00116         {
    00117                 glActiveTextureARB = (PFNGLCLIENTACTIVETEXTUREARBPROC)wglGetProcAddress("glActiveTextureARB");
    00118                 glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC)wglGetProcAddress("glMultiTexCoord2fARB");
    00119                 glClientActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress("glClientActiveTextureARB");
    00120                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB,&m_Multitexturing_max_level);
    00121                 *Consol<<"ExManagerTexture::GL_ARB_multitexture found and enable. Maximum textures:"<<m_Multitexturing_max_level<<std::endl;
    00122                 m_ARB_Multitexture=true;
    00123                 m_Multitexturing_level=m_Multitexturing_max_level;
    00124         }else
    00125         {
    00126                 *Consol<<"ExManagerTexture::GL_ARB_multitexture extension missing"<<std::endl;
    00127                 m_ARB_Multitexture=false;
    00128                 throw ExCExpMultitexturing();
    00129         }
    00130
    00131         //--------------------------------
    00132         //TEXTURE ENV DOT 3
    00133         //--------------------------------
    00134         if (strstr( (const char *)blah, "GL_ARB_texture_env_dot3" ))
    00135         {
    00136                 *Consol<<"ExManagerTexture::GL_ARB_texture_env_dot3 enable."<<std::endl;
    00137                 m_ARB_texture_env_dot3=true;
    00138         }else
    00139         {
    00140                 *Consol<<"ExManagerTexture::GL_ARB_texture_env_dot3 not supported."<<std::endl;
    00141                 m_ARB_texture_env_dot3=false;
    00142         }
    00143
    00144         //--------------------------------
    00145         //TEXTURE ENV COMBINE
    00146         //--------------------------------
    00147         if (strstr( (const char *)blah, "GL_ARB_texture_env_combine" ))
    00148         {
    00149                 *Consol<<"ExManagerTexture::GL_ARB_texture_env_combine enable."<<std::endl;
    00150                 m_ARB_texture_env_dot3=true;
    00151         }else
    00152         {
    00153                 *Consol<<"ExManagerTexture::GL_ARB_texture_env_combine not supported."<<std::endl;
    00154                 m_ARB_texture_env_dot3=false;
    00155         }
    00156
    00157         //--------------------------------
    00158         //TEXTURE CUBE MAP
    00159         //--------------------------------
    00160         if (strstr( (const char *)blah, "GL_ARB_texture_cube_map" ))
    00161         {
    00162                 *Consol<<"ExManagerTexture::GL_ARB_texture_cube_map enable."<<std::endl;
    00163                 m_ARB_texture_cube_map=true;
    00164         }else
    00165         {
    00166                 *Consol<<"ExManagerTexture::GL_ARB_texture_cube_map not supported."<<std::endl;
    00167                 m_ARB_texture_cube_map=false;
    00168         }
    00169 UnGuard
    00170 }
    00171
    00172
    00173
    00174 void  ExManagerTexture::Reset(void)
    00175 {
    00176 Guard(void  ExManagerTexture::Reset(void))
    00177         ExManagerObject<ExCTexture>::Reset();
    00178 UnGuard
    00179 }
    00180
    00181 bool  ExManagerTexture::Init(std::string FileName)
    00182 {
    00183 Guard(bool  ExManagerTexture::Init(std::string FileName))
    00184         //openf file and load object
    00185
    00186         return true;
    00187 UnGuard
    00188 }
    00189
    00190 bool ExManagerTexture::CheckExist(std::string  name)
    00191 {
    00192 Guard(ExManagerTexture::CheckExist(std::string  name))
    00193         if(name.find(".")<name.length())//if extension remove them
    00194         {
    00195                 name.erase(name.find("."),name.length()-name.find("."));
    00196         }
    00197         if(ManagerId->GetObjectId(name,typeid(ExCTexture).name())==-1)return true;
    00198         return false;
    00199 UnGuard
    00200 }
    00201
    00202
    00203 bool ExManagerTexture::Load(std::string FileName)
    00204 {
    00205 Guard(ExManagerTexture::Load(std::string FileName))
    00206         if(FileName.find(".")<FileName.length())//if extension remove them
    00207         {
    00208                 FileName.erase(FileName.find("."),FileName.length()-FileName.find("."));
    00209         }
    00210         if(!CheckExist(FileName)){return true;}
    00211
    00212         try
    00213         {
    00214                 return  Add(ExCTexture(FileName));
    00215         }catch(ExCExpFileNotFound)
    00216         {
    00217                 *Consol<<"Exception file not found texture not added"<<std::endl;
    00218                 return false;
    00219         }
    00220         catch(ExCExpNullPointer)
    00221         {
    00222                 *Consol<<"Exception null pointer texture not added"<<std::endl;
    00223                 return false;
    00224         }
    00225
    00226 UnGuard
    00227 }
    00228
    00229 bool ExManagerTexture::LoadCubeMap(std::string FileName)
    00230 {
    00231 Guard(ExManagerTexture::LoadCubeMap(std::string FileName))
    00232         if(FileName.find(".")<FileName.length())//if extension remove them
    00233         {
    00234                 FileName.erase(FileName.find("."),FileName.length()-FileName.find("."));
    00235         }
    00236         if(!CheckExist(FileName)){return true;}
    00237
    00238         try
    00239         {
    00240                 return  Add(ExCTexture(FileName));
    00241         }catch(ExCExpFileNotFound)
    00242         {
    00243                 *Consol<<"Exception file not found texture not added"<<std::endl;
    00244                 return false;
    00245         }
    00246         catch(ExCExpNullPointer)
    00247         {
    00248                 *Consol<<"Exception null pointer texture not added"<<std::endl;
    00249                 return false;
    00250         }
    00251
    00252 UnGuard
    00253 }
    00254
    00255 bool ExManagerTexture::Add(std::string FileName)
    00256 {
    00257 Guard(ExManagerTexture::Add(std::string FileName))
    00258         return Load(FileName);
    00259 UnGuard
    00260 }
    00261
    00262 bool ExManagerTexture::Add(ExCTexture object)
    00263 {
    00264 Guard(ExManagerTexture::Add(ExCTexture object))
    00265         if(!CheckExist(object.GetName())){return true;}
    00266         object.SetAnisotropyLevel(m_Anisotropy,m_Anisotropy_max_level);
    00267         object.SetSGISMipMap(m_SGIS_Mimap);
    00268         object.SetTextureCompression(m_ARB_Texture_Compression);
    00269         return ExManagerObject<ExCTexture>::Add(object);
    00270 UnGuard
    00271 }
    00272
    00273 bool ExManagerTexture::SetTexture(int Number,std::string Name)
    00274 {
    00275 Guard(ExManagerTexture::SetTexture(int Number,std::string Name))
    00276         //glActiveTextureARB = (PFNGLCLIENTACTIVETEXTUREARBPROC)wglGetProcAddress("glActiveTextureARB");
    00277         if(Name.size()<1)return false;
    00278         switch(Number)
    00279         {
    00280         case 0:
    00281                 glClientActiveTextureARB ( GL_TEXTURE0_ARB        );
    00282                 glActiveTextureARB(GL_TEXTURE0_ARB);
    00283                 break;
    00284         case 1:
    00285                 glClientActiveTextureARB ( GL_TEXTURE1_ARB        );
    00286                 glActiveTextureARB(GL_TEXTURE1_ARB);
    00287                 break;
    00288         case 2:
    00289                 glClientActiveTextureARB ( GL_TEXTURE2_ARB        );
    00290                 glActiveTextureARB(GL_TEXTURE2_ARB);
    00291                 break;
    00292         case 3:
    00293                 glClientActiveTextureARB ( GL_TEXTURE3_ARB        );
    00294                 glActiveTextureARB(GL_TEXTURE3_ARB);
    00295                 break;
    00296         case 4:
    00297                 glClientActiveTextureARB ( GL_TEXTURE4_ARB        );
    00298                 glActiveTextureARB(GL_TEXTURE4_ARB);
    00299                 break;
    00300         case 5:
    00301                 glClientActiveTextureARB ( GL_TEXTURE5_ARB        );
    00302                 glActiveTextureARB(GL_TEXTURE5_ARB);
    00303                 break;
    00304         case 6:
    00305                 glClientActiveTextureARB ( GL_TEXTURE6_ARB        );
    00306                 glActiveTextureARB(GL_TEXTURE6_ARB);
    00307                 break;
    00308         case 7:
    00309                 glClientActiveTextureARB ( GL_TEXTURE7_ARB        );
    00310                 glActiveTextureARB(GL_TEXTURE7_ARB);
    00311
    00312                 break;
    00313         }
    00314         glEnable(GL_TEXTURE_2D);
    00315         SetCurrentObject(Name);
    00316         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    00317         return true;
    00318 UnGuard
    00319 }
    00320
    00321 void ExManagerTexture::SetTextureCoord(int Number,float CoordU,float CoordV)
    00322 {
    00323 Guard(ExManagerTexture::SetTextureCoord(int Number,float CoordU,float CoordV))
    00324         switch(Number)
    00325         {
    00326         case 0:
    00327                 glMultiTexCoord2fARB ( GL_TEXTURE0_ARB,CoordU,CoordV);
    00328                 break;
    00329         case 1:
    00330                 glMultiTexCoord2fARB ( GL_TEXTURE1_ARB,CoordU,CoordV);
    00331                 break;
    00332         case 2:
    00333                 glMultiTexCoord2fARB ( GL_TEXTURE2_ARB,CoordU,CoordV);
    00334                 break;
    00335         case 3:
    00336                 glMultiTexCoord2fARB ( GL_TEXTURE3_ARB,CoordU,CoordV);
    00337                 break;
    00338         }
    00339 UnGuard
    00340 }
    00341
    00342 bool ExManagerTexture::ResetMultitexture(void)
    00343 {
    00344 Guard(ExManagerTexture::ResetMultitexture(void))
    00345         if(m_Multitexturing_max_level>6)
    00346         {
    00347                 glActiveTextureARB(GL_TEXTURE7_ARB);
    00348                 glDisable(GL_TEXTURE_2D);
    00349         }
    00350         if(m_Multitexturing_max_level>5)
    00351         {
    00352                 glActiveTextureARB(GL_TEXTURE6_ARB);
    00353                 glDisable(GL_TEXTURE_2D);
    00354         }
    00355         if(m_Multitexturing_max_level>4)
    00356         {
    00357                 glActiveTextureARB(GL_TEXTURE5_ARB);
    00358                 glDisable(GL_TEXTURE_2D);
    00359         }
    00360         if(m_Multitexturing_max_level>3)
    00361         {
    00362                 glActiveTextureARB(GL_TEXTURE4_ARB);
    00363                 glDisable(GL_TEXTURE_2D);
    00364         }
    00365         if(m_Multitexturing_max_level>2)
    00366         {
    00367                 glActiveTextureARB(GL_TEXTURE3_ARB);
    00368                 glDisable(GL_TEXTURE_2D);
    00369         }
    00370         if(m_Multitexturing_max_level>1)
    00371         {
    00372                 glActiveTextureARB(GL_TEXTURE2_ARB);
    00373                 glDisable(GL_TEXTURE_2D);
    00374         }
    00375         if(m_Multitexturing_max_level>0)
    00376         {
    00377                 glActiveTextureARB(GL_TEXTURE1_ARB);
    00378                 glDisable(GL_TEXTURE_2D);
    00379         }
    00380         glActiveTextureARB(GL_TEXTURE0_ARB);
    00381     glDisable(GL_TEXTURE_2D);
    00382         return true;
    00383 UnGuard
    00384 }
    00385
    00386 bool ExManagerTexture::SetCurrentObject(std::string Name)
    00387 {
    00388 Guard(ExManagerTexture::SetCurrentObject(std::string Name))
    00389         if(Name.size()<=0)return false;
    00390         if(Name.find(".")<Name.length())//if extension remove them
    00391         {
    00392                 Name.erase(Name.find("."),Name.length()-Name.find("."));
    00393         }
    00394         try
    00395         {
    00396                 m_CurrentObjectId=ManagerId->GetObjectId(Name,typeid(ExCTexture).name());
    00397                 glBindTexture(GL_TEXTURE_2D,GetObject(m_CurrentObjectId)->m_Texture);
    00398                 if(m_SGIS_Mimap)
    00399                 {
    00400                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    00401                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    00402                         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true);
    00403                 }
    00404                 return true;
    00405                 //std::cout<<"ExManagerTexture ::"<<Name<<" Current object"<<std::endl;
    00406         }catch(...)
    00407         {
    00408                 if(!Load(Name))
    00409                 {
    00410                         std::cout<<"ExManagerTexture ::Exception can make :"<<Name<<" Current object"<<std::endl;
    00411                         return true;
    00412                 }
    00413                 return false;
    00414         }
    00415 UnGuard
    00416 }
    00417
    00418 bool ExManagerTexture::SetCurrentObject(ExNihilo::ExId id)
    00419 {
    00420 Guard(ExManagerTexture::SetCurrentObject(ExNihilo::ExId id))
    00421         if(GetObject(id)!=NULL)
    00422         {
    00423                 m_CurrentObjectId=id;
    00424                 try
    00425                 {
    00426                         glBindTexture(GL_TEXTURE_2D,GetObject(m_CurrentObjectId)->m_Texture);
    00427                         if(m_SGIS_Mimap)
    00428                         {
    00429                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    00430                                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    00431                                 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true);
    00432
    00433                         }
    00434                         return true;
    00435                         //std::cout<<"ExManagerTexture ::"<<Name<<" Current object"<<std::endl;
    00436                         }catch(...)
    00437                         {
    00438                                 std::cout<<"ExManagerTexture ::Exception can make :"<<m_CurrentObjectId<<" Current object"<<std::endl;
    00439                                 return false;
    00440                         }
    00441         }
    00442         return false;
    00443 UnGuard
    00444 }
    00445
    00446 GLint ExManagerTexture::GetTextureId(std::string Name)
    00447 {
    00448 Guard(ExManagerTexture::GetTextureId(std::string Name))
    00449
    00450         if(Name.find(".")<Name.length())//if extension remove them
    00451         {
    00452                 Name.erase(Name.find("."),Name.length()-Name.find("."));
    00453         }
    00454         try
    00455         {
    00456                 m_CurrentObjectId=ManagerId->GetObjectId(Name,typeid(ExCTexture).name());
    00457                 return GetObject(m_CurrentObjectId)->m_Texture;
    00458                 //std::cout<<"ExManagerTexture ::"<<Name<<" Current object"<<std::endl;
    00459         }catch(...)
    00460         {
    00461                 if(!Load(Name))
    00462                 {
    00463                         std::cout<<"ExManagerTexture ::Exception can make :"<<Name<<" Current object"<<std::endl;
    00464                         return -1;
    00465                 }
    00466                 return -1;
    00467         }
    00468 UnGuard
    00469 }
    00470
    00471 GLint ExManagerTexture::GetTextureId(ExNihilo::ExId id)
    00472 {
    00473 Guard(ExManagerTexture::GetTextureId(ExNihilo::ExId id))
    00474         if(GetObject(id)!=NULL)
    00475         {
    00476                 m_CurrentObjectId=id;
    00477                 try
    00478                 {
    00479                         return GetObject(m_CurrentObjectId)->m_Texture;
    00480                         //std::cout<<"ExManagerTexture ::"<<Name<<" Current object"<<std::endl;
    00481                         }catch(...)
    00482                         {
    00483                                 std::cout<<"ExManagerTexture ::Exception can make :"<<m_CurrentObjectId<<" Current object"<<std::endl;
    00484                                 return -1;
    00485                         }
    00486         }
    00487         return -1;
    00488 UnGuard
    00489 }
    00490
    00491
    00492 ExCAction ExManagerTexture::InputAction(ExCAction Action)
    00493 {
    00494 Guard(ExCAction ExManagerTexture::InputAction(ExCAction Action))
    00495         switch(Action.m_Action)
    00496         {
    00497         case LIST_TEXTURE:
    00498                 List();
    00499                 break;
    00500         case LOAD_TEXTURE:
    00501                 break;
    00502         case UNLOAD_TEXTURE:
    00503                 break;
    00504         case MANAGER_TEXTURE_RELOAD: Reload();break;
    00505         case SAVE_TEXTURE:
    00506                 ExCTexture SaveTexture;
    00507                 SaveTexture.SaveFile(Action.m_Param,ExNihilo::GetResolutionX(),ExNihilo::GetResolutionY());
    00508                 break;
    00509         }
    00510         if(Action.m_NextAction){return *Action.m_NextAction;}
    00511         else{return NOTHING;}
    00512 UnGuard
    00513 }
    00514
    00515 void  ExManagerTexture::Reload(void)
    00516 {
    00517 Guard(void  ExManagerTexture::Reload(void))
    00518         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
    00519         {
    00520                 m_ItMap->second.LoadFile(m_ItMap->second.GetFileName());
    00521         }
    00522 UnGuard
    00523 }
    00524