Main Page   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

ExManagerTexture.cpp

Go to the documentation of this file.
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.28 2002/08/01 18:45:58 data Exp $
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         //openf file and load object
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())//if extension remove them
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         //cheking extension type
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         //watching for file exentions type
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         // no aviable extension was found 
00122         if(!Loaded)
00123         {
00124                 //watch if there is a extension
00125                 if(FileName.find(".")<FileName.length())
00126                 {
00127                         //ther is a unknow extension
00128                         *Consol<<"File format not supported :"<<FileName.data()<<std::endl;
00129                 }
00130         }
00131         //there is no extension try to find correct extension 
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                 // Assume that the texture is a 24 bit RGB texture (We convert 16-bit ones to 24-bit)
00144                 int textureType = GL_RGB;
00145                 // If the image is 32-bit (4 channels), then we need to specify GL_RGBA for an alpha
00146                 if(image->channels == 4)textureType = GL_RGBA;
00147         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00148                 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
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                 // Build Mipmaps (builds different versions of the picture for distances - looks better)
00153                 //gluBuild2DMipmaps(GL_TEXTURE_2D,image->channels,image->sizeX,image->sizeY, textureType, GL_UNSIGNED_BYTE, image->data);
00154                 //Assign the mip map levels and texture info
00155                 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
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())//if extension remove them
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 

Generated on Tue Aug 6 20:27:14 2002 for ExNihilo by doxygen1.2.17