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

# ExCTexture.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: ExCTexture.cpp,v 1.17 2002/12/13 11:45:03 data Exp $
    00021  *
    00022  */
    00023
    00024 #include "ExCTexture.h"
    00025
    00026
    00027 ExCTexture::ExCTexture(void)
    00028 {
    00029 Guard(ExCTexture::ExCTexture(void))
    00030         SetName("ExCTexture");
    00031         SetType(typeid(this).name());
    00032         m_Anisotropy_level=-1;
    00033         m_Anisotropy=true;
    00034         m_SGIS_Mimap=true;
    00035         m_ARB_Texture_Compression=true;
    00036 UnGuard
    00037 }
    00038
    00039 ExCTexture::ExCTexture(std::string FileName)
    00040 {
    00041 Guard(ExCTexture::ExCTexture(std::string FileName))
    00042         SetName("ExCTexture");
    00043         SetType(typeid(this).name());
    00044         LoadFile(FileName);
    00045 UnGuard
    00046 }
    00047
    00048 ExCTexture::~ExCTexture(void)
    00049 {
    00050 Guard(ExCTexture::~ExCTexture(void))
    00051 UnGuard
    00052 }
    00053
    00054 void ExCTexture::SetName(std::string     Name)
    00055 {
    00056 Guard(void ExCTexture::SetName(std::string       Name))
    00057         if(Name.find(".")<Name.length())//if extension remove them
    00058         {
    00059                 Name.erase(Name.find("."),Name.length()-Name.find("."));
    00060                 m_ObjectName=Name;
    00061         }else
    00062         {
    00063                 m_ObjectName=Name;
    00064         }
    00065 UnGuard
    00066 }
    00067
    00068 void ExCTexture::SetActive(void)
    00069 {
    00070 Guard(ExCTexture::SetActive(void))
    00071
    00072         glBindTexture(GL_TEXTURE_2D,m_Texture);
    00073 UnGuard
    00074 }
    00075
    00076
    00077 bool ExCTexture::SaveFile(std::string FileName,int width,int height)
    00078 {
    00079         Guard(ExCTexture::SaveFile(std::string FileName,int width,int height))
    00080         m_Image= new ExCImageLoader;
    00081         return m_Image->SaveImage(FileName,width,height);
    00082         delete m_Image;
    00083         UnGuard
    00084 }
    00085
    00086 bool ExCTexture::LoadFile(std::string FileName)
    00087 {
    00088 Guard(ExCTexture::LoadFile(std::string FileName))
    00089         //std::cout<<"Fichier:"<<FileName<<std::endl;
    00090         m_ObjectFileName=FileName;
    00091         m_Image= new ExCImageLoader;
    00092         int Type = GL_RGB;
    00093         int offset = 0;
    00094         int size=0;
    00095         int blockSize =0;
    00096         std::string             StrPath;
    00097         SetName(FileName);
    00098         #ifdef UNIX_SRC
    00099                 StrPath=PREFIX "/ExNihilo/Data/Textures/";
    00100         #else
    00101                 StrPath="../Data/Textures/";
    00102         #endif
    00103
    00104
    00105                 glCompressedTexImage3DARB               = (PFNGLCOMPRESSEDTEXIMAGE3DARBPROC)wglGetProcAddress("glCompressedTexImage3DARB");
    00106                 glCompressedTexImage2DARB               = (PFNGLCOMPRESSEDTEXIMAGE2DARBPROC)wglGetProcAddress("glCompressedTexImage2DARB");
    00107                 glCompressedTexImage1DARB           = (PFNGLCOMPRESSEDTEXIMAGE1DARBPROC)wglGetProcAddress("glCompressedTexImage1DARB");
    00108                 glCompressedTexSubImage3DARB    = (PFNGLCOMPRESSEDTEXSUBIMAGE3DARBPROC)wglGetProcAddress("glCompressedTexSubImage3DARB");
    00109                 glCompressedTexSubImage2DARB    = (PFNGLCOMPRESSEDTEXSUBIMAGE2DARBPROC)wglGetProcAddress("glCompressedTexSubImage2DARB");
    00110                 glCompressedTexSubImage1DARB    = (PFNGLCOMPRESSEDTEXSUBIMAGE1DARBPROC)wglGetProcAddress("glCompressedTexSubImage1DARB");
    00111                 glGetCompressedTexImageARB              = (PFNGLGETCOMPRESSEDTEXIMAGEARBPROC)wglGetProcAddress("glGetCompressedTexImageARB");
    00112
    00113         if(m_Image->LoadImage(StrPath+FileName))
    00114         {
    00115                 SetName(FileName);
    00116                 glGenTextures(1,&m_Texture);
    00117                 glBindTexture(GL_TEXTURE_2D,m_Texture);
    00118
    00119
    00120                 switch (m_Image->m_TextureType)
    00121                 {
    00122                 case BMP:
    00123                         //std::cout<<"Extension:BMP"<<std::endl;
    00124                         gluBuild2DMipmaps(GL_TEXTURE_2D, GL_COMPRESSED_RGB_ARB, m_Image->m_sizeX, m_Image->m_sizeY,
    00125                                 GL_RGB, GL_UNSIGNED_BYTE, m_Image->m_data);
    00126                         m_ARB_Texture_Compression=true;
    00127                         break;
    00128                 case PCX:
    00129                         //std::cout<<"Extension:PCX"<<std::endl;
    00130                         gluBuild2DMipmaps(GL_TEXTURE_2D, GL_COMPRESSED_RGBA_ARB, m_Image->m_sizeX,m_Image->m_sizeY,
    00131                                 GL_RGBA, GL_UNSIGNED_BYTE, m_Image->m_data);
    00132                         break;
    00133                 case TGA:
    00134                         //std::cout<<"Extension:TGA"<<std::endl;
    00135                          gluBuild2DMipmaps(GL_TEXTURE_2D,
    00136                (m_Image->m_channels == 4) ? GL_COMPRESSED_RGBA_ARB : GL_COMPRESSED_RGB_ARB,
    00137                m_Image->m_sizeX, m_Image->m_sizeY,
    00138                Type, GL_UNSIGNED_BYTE, m_Image->m_data);
    00139                          m_ARB_Texture_Compression=true;
    00140                          break;
    00141                 case PPM:
    00142                         //std::cout<<"Extension:PPM"<<std::endl;
    00143                         gluBuild2DMipmaps(GL_TEXTURE_2D,GL_COMPRESSED_RGB_ARB, m_Image->m_sizeX, m_Image->m_sizeY,
    00144                                 GL_RGB, GL_UNSIGNED_BYTE, m_Image->m_data);
    00145             break;
    00146                 case DDS:
    00147                         //std::cout<<"Extension:DDS"<<std::endl;
    00148                         /* load the mipmaps */
    00149                         blockSize = (m_Image->m_ddsFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;//if not dxt1 blocksize = 16
    00150                         for (int i = 0; i < m_Image->m_ddsNumMipmaps&& (m_Image->m_sizeX || m_Image->m_sizeY); ++i)
    00151                         {
    00152
    00153                                 if (m_Image->m_sizeX == 0)m_Image->m_sizeX = 1;
    00154                                 if (m_Image->m_sizeY == 0)m_Image->m_sizeY = 1;
    00155                                 size = ((m_Image->m_sizeX+3)/4)*((m_Image->m_sizeY+3)/4)*blockSize;
    00156
    00157                                 glCompressedTexImage2DARB(GL_TEXTURE_2D, i, m_Image->m_ddsFormat, m_Image->m_sizeX, m_Image->m_sizeY,
    00158                                         0, size, m_Image->m_data );
    00159
    00160                                 offset += size;
    00161                                 m_Image->m_sizeX>>= 1;
    00162                                 m_Image->m_sizeY >>= 1;
    00163                                 m_ARB_Texture_Compression=false;
    00164                                 //std::cout<<"Compression :"<<i<<" ";ExNihilo::ShowGlError();
    00165                         }
    00166                         break;
    00167                 default:
    00168                         break;
    00169                 }
    00170                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    00171                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    00172                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    00173                 //--------------------------------
    00174                 //MIMAP SGIS EXTENSION
    00175                 //--------------------------------
    00176                 if(m_SGIS_Mimap)
    00177                 {
    00178                         glHint(GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
    00179                         //std::cout<<"glHint m_SGIS_Mimap :"<<" ";ExNihilo::ShowGlError();
    00180                         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true);
    00181                         //std::cout<<"GL_GENERATE_MIPMAP_SGIS :"<<" ";ExNihilo::ShowGlError();
    00182                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    00183                         //std::cout<<"GL_TEXTURE_MIN_FILTER :"<<" ";ExNihilo::ShowGlError();
    00184                 }else
    00185                 {
    00186                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    00187                 }
    00188
    00189                 //std::cout<<"Tex ID :"<<m_Texture<<std::endl;
    00190
    00191
    00192                 //--------------------------------
    00193                 //ANISOTROPIC EXTENSION
    00194                 //--------------------------------
    00195                 if(m_Anisotropy)
    00196                 {
    00197                         glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT,&m_Anisotropy_level);
    00198                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, m_Anisotropy_level);
    00199                         //std::cout<<"m_Anisotropy :"<<" ";ExNihilo::ShowGlError();
    00200                 }
    00201
    00202                 //--------------------------------
    00203                 //TEXTURE COMPRESSION EXTENSION
    00204                 //--------------------------------
    00205
    00206                 if(m_ARB_Texture_Compression)
    00207                 {
    00208
    00209                         int compressed_size;
    00210                         int num_compressed_format;
    00211                         int * compressed_format = NULL;
    00212                         int compressed;
    00213                         int internalformat;
    00214                         unsigned char * img = NULL;
    00215                         glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB, &num_compressed_format);
    00216                         compressed_format = (int*)malloc(num_compressed_format * sizeof(int));
    00217                         glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS_ARB, compressed_format);
    00218
    00219                         // Check if the image has been compressed by GL
    00220                         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB, &compressed);
    00221
    00222                         // Query for the compressed internal format
    00223                         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
    00224
    00225                         // Query for the size of the compressed data texture buffer
    00226                         //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_IMAGE_SIZE_ARB, &compressed_size);
    00227                         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &compressed_size);
    00228
    00229                         //std::cout<<"FileNAme:"<<FileName<<" Compressed: "<<compressed<<" Internal format: "<<internalformat<<" Compressed size: "<<compressed_size<<std::endl;
    00230                         // Allocate a buffer to host a copy of the compressed image data
    00231                         img = (unsigned char *)malloc(compressed_size * sizeof(unsigned char));
    00232
    00233                         // get the compressed data buffer
    00234                         glGetCompressedTexImageARB(GL_TEXTURE_2D, 0, img);
    00235                         //glBindTexture(GL_TEXTURE_2D,m_Texture);
    00236                         glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, internalformat, m_Image->m_sizeX, m_Image->m_sizeY, 0, compressed_size, img);
    00237
    00238                         delete img;
    00239                         delete compressed_format;
    00240                         //std::cout<<"m_ARB_Texture_Compression :"<<" ";ExNihilo::ShowGlError();
    00241                 }
    00242                 delete m_Image->m_data;
    00243                 //ExNihilo::ShowGlError();
    00244                 //std::cout<<"---------------------------------------------------------------"<<std::endl;
    00245                 return true;
    00246         }
    00247
    00248         *Consol<<"ExCTexture Exception::unable to load file "<<FileName<<std::endl;
    00249         delete m_Image;
    00250 return false;
    00251 UnGuard
    00252 }
    00253