gfx_opengl_texture2.hh

Go to the documentation of this file.
00001 /*
00009  * LEGAL:   COPYRIGHT (C) 2007 JIM E. BROOKS
00010  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00011  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00012  ******************************************************************************/
00013 
00014 #ifndef GFX_TEXTURE2_OPENGL_HH
00015 #define GFX_TEXTURE2_OPENGL_HH 1
00016 
00017 #include "gfx_targa.hh"
00018 
00019 namespace gfx {
00020 
00021 #if DEBUG
00022 #define CHECK_TEXTURE() ASSERT(mBits.mValid), CHECK_TYPESIG(this,TYPESIG_TEXTURE)
00023 #else
00024 #define CHECK_TEXTURE()
00025 #endif
00026 
00073 class Texture : public TextureBase
00074 {
00075 
00076 public:
00077     // Force giving an Alpha channel to the internal texture
00078     // despite the external texture lacking Alpha.
00079     enum eForceAlpha { eForceAlpha_FALSE, eForceAlpha_TRUE };
00080 
00081 public:
00082     Texture( void );  // create texture with empty texture data
00083     Texture( const uint width, const uint height,
00084              const eTexelType externalTexelType,
00085              const eTexelType internalTexelType );
00086     Texture( const Targa& targa,
00087              const eMipmap mipmap = eMipmap_OFF,
00088              const eForceAlpha forceAlpha = eForceAlpha_FALSE );
00089     ~Texture();
00090 
00091 public:
00098     void
00099     Bind( void ) const
00100     {
00101     CHECK_TEXTURE();
00102     ASSERT( glIsEnabled( GL_TEXTURE_2D ) );  // user required to enable texturing
00103     ASSERT( IfValid() );  // checks if texture image was loaded (mId)
00104 
00105         // Switch/bind to this texture.
00106         glBindTexture( GL_TEXTURE_2D, mId );
00107 
00108         // Unlike glTexParameter(), the effect of glTexEnv() doesn't stick to a texture object.
00109         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mTexFunc );
00110     }
00111 
00117     CLASS_METHOD void
00118     Map( const TexCoord2& tc )
00119     {
00120         glTexCoord2f( tc.x, tc.y );
00121     }
00122 
00123     CLASS_METHOD void
00124     Map( fp tx, fp ty )
00125     {
00126         glTexCoord2f( tx, ty );
00127     }
00128 
00129 public:
00133     bool
00134     IfValid( void ) const
00135     {
00136         return mBits.mValid && (mId != TEXTURE_ID_INVALID);
00137     }
00138 
00139     uint
00140     GetWidth( void ) const
00141     {
00142     CHECK_TEXTURE();
00143         return mBits.mWidth;
00144     }
00145 
00146     uint
00147     GetHeight( void ) const
00148     {
00149     CHECK_TEXTURE();
00150         return mBits.mWidth;  // w=h
00151     }
00152 
00153     uint
00154     DimCnt( void ) const
00155     {
00156     CHECK_TEXTURE();
00157         return 2;  // 2D
00158     }
00159 
00160     uint GetTexelCnt( void ) const;  // no data member for this
00161     uint GetExternalByteCnt( void ) const;  // texel cnt same but byte cnt may differ
00162 
00163     #if STATS
00164     // Track texture memory usage.
00165     CLASS_METHOD uint
00166     GetTotalMemoryUsed( void )
00167     {
00168         return msTotalMemoryUsed;
00169     }
00170     #endif
00171 
00172 public:
00176     void    SetTextureImage( const Array<Texel32>& texels, const eMipmap mipmap = eMipmap_OFF );
00177     void    SetTextureImage( const Texel32* texels, const uint texelCnt, const eMipmap mipmap = eMipmap_OFF );
00178     void    SetTextureImage( const uint8* texels, const uint externalByteCnt, const eMipmap mipmap = eMipmap_OFF );  // raw byte array
00179 
00180 public:
00184     void    SetMinFilter( eMinFilter filter );
00185     void    SetMagFilter( eMagFilter filter );
00186     void    SetFastestFilters( void );
00187     void    SetWrap( eWrap wrap, uint dim );
00188     void    SetWrap( eWrap wrap );
00189     void    SetFunc( eFunc func );
00190 
00191 private:
00195     friend bool
00196     operator<( Texture& a, Texture& b )
00197     {
00198     CHECK_TYPESIG(&a,TYPESIG_TEXTURE);
00199     CHECK_TYPESIG(&b,TYPESIG_TEXTURE);
00200         return a.mId < b.mId;
00201     }
00202 
00203 private:
00207     void    Init( const uint width, const uint height,
00208                   const eTexelType externalTexelType,
00209                   const eTexelType internalTexelType );
00210     uint    TexelCntToExternalByteCnt( const uint texelCnt ) const;
00211     uint    TexelCntToInternalByteCnt( const uint texelCnt ) const;
00212     uint    GetInternalTexelCnt( void ) const;
00213     uint    GetInternalByteCnt( void ) const;
00214     uint    GetByteCnt_( const bool hasAlpha ) const;  // subroutine
00215     uint    GetTexelCnt_( const bool hasAlpha ) const;  // subroutine
00216     uint    TexelCntToByteCnt_( const uint texelCnt, const uint texelLen ) const;  // subroutine
00217     #if STATS
00218     uint    EstimateInternalByteCnt( const eMipmap mipmap ) const;
00219     #endif
00220     
00221 public:
00222     enum    { MAX_TEXTURE_WIDTH_BITS = 12 };
00223     enum    { MAX_TEXTURE_WIDTH = 1 << (MAX_TEXTURE_WIDTH_BITS-1) };
00224     enum    { MAX_TEXTURE_BYTES = MAX_TEXTURE_WIDTH * MAX_TEXTURE_WIDTH * 4 };  // limit texture memory consumption
00225 
00226 private:
00227     // The parameters of glTexParameter() sticks to bound texture object.
00228     // But glTexEnv() don't, so need to store them in class members.
00229     // mTexFunc is passed to glTexEnv() every time this texture is active.
00230 
00232     struct Bits
00233     {
00234         uint    mValid              : 1;
00235         uint    mWidth              : 16;   
00236         uint    mExternalTexelLen   : 3;    
00237         uint    mInternalTexelLen   : 3;    
00238         uint    mLoaded             : 1;    
00239         uint    mMipmap             : 1;    
00240     };
00241     Bits            mBits;
00242     eTexelType      mExternalTexelType; 
00243     eTexelType      mInternalTexelType; 
00244     GLuint          mId;                
00245     eFunc           mTexFunc;           
00246 
00247 private:
00248     #if STATS
00249     CLASS_VAR int   msTotalMemoryUsed;  
00250     #endif
00251 
00252 public:
00253     
00254 };
00255 
00256 } // namespace gfx
00257 
00258 #endif // GFX_TEXTURE2_OPENGL_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:11 2007