00001
00009
00010
00011
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
00078
00079 enum eForceAlpha { eForceAlpha_FALSE, eForceAlpha_TRUE };
00080
00081 public:
00082 Texture( void );
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 ) );
00103 ASSERT( IfValid() );
00104
00105
00106 glBindTexture( GL_TEXTURE_2D, mId );
00107
00108
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;
00151 }
00152
00153 uint
00154 DimCnt( void ) const
00155 {
00156 CHECK_TEXTURE();
00157 return 2;
00158 }
00159
00160 uint GetTexelCnt( void ) const;
00161 uint GetExternalByteCnt( void ) const;
00162
00163 #if STATS
00164
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 );
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;
00215 uint GetTexelCnt_( const bool hasAlpha ) const;
00216 uint TexelCntToByteCnt_( const uint texelCnt, const uint texelLen ) const;
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 };
00225
00226 private:
00227
00228
00229
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 }
00257
00258 #endif // GFX_TEXTURE2_OPENGL_HH