Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreD3D9Texture.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __D3D8TEXTURE_H__
00026 #define __D3D8TEXTURE_H__
00027 
00028 #include "OgreD3D9Prerequisites.h"
00029 #include "OgreTexture.h"
00030 #include "OgreRenderTexture.h"
00031 #include "OgreImage.h"
00032 #include "OgreException.h"
00033 
00034 #include "OgreNoMemoryMacros.h"
00035 #include <d3d9.h>
00036 #include <d3dx9.h>
00037 #include <dxerr9.h>
00038 #include "OgreMemoryMacros.h"
00039 
00040 namespace Ogre {
00041     class D3D9Texture : public Texture
00042     {
00043     private:
00045         IDirect3DDevice9        *mpDev;     
00047         IDirect3D9              *mpD3D;
00049         IDirect3DTexture9       *mpNormTex; 
00051         IDirect3DCubeTexture9   *mpCubeTex; 
00053         IDirect3DVolumeTexture9 *mpVolumeTex;
00055         IDirect3DSurface9       *mpZBuff;   
00057         IDirect3DBaseTexture9   *mpTex;     
00058 
00060         String                          mCubeFaceNames[6];
00062         D3DDEVICE_CREATION_PARAMETERS   mDevCreParams;
00064         D3DFORMAT                       mBBPixelFormat;
00066         D3DCAPS9                        mDevCaps;
00067         // Auto-generated mipmaps?
00068         bool                            mAutoGenMipMaps;
00069     
00071         void _loadCubeTex();
00073         void _loadNormTex();
00075         void _loadVolumeTex();
00076 
00078         void _createTex();
00080         void _createNormTex();
00082         void _createCubeTex();
00083 
00085         D3DFORMAT _chooseD3DFormat();
00087         void _getColorMasks(D3DFORMAT format, DWORD *pdwRed, DWORD *pdwGreen, DWORD *pdwBlue, DWORD *pdwAlpha, DWORD *pdwRGBBitCount);
00089         void _copyMemoryToSurface( const unsigned char *pBuffer, IDirect3DSurface9 *pSurface );
00091         void _blitImageToNormTex(const Image &srcImage);
00093         void _blitImagesToCubeTex(const Image srcImages[]);
00094 
00096         static PixelFormat _getPF(D3DFORMAT d3dPF);
00098         static D3DFORMAT _getPF(PixelFormat ogrePF);
00099 
00101         void _freeResources();
00103         void _initMembers();
00105         void _setDevice(IDirect3DDevice9 *pDev);
00107         void _constructCubeFaceNames(const String& name);
00109         void _setSrcAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
00111         void _setFinalAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
00113         D3DTEXTUREFILTERTYPE _getBestFilterMethod();
00115         bool _canAutoGenMipMaps(DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat);
00117         void _createDepthStencil();
00118 
00120         String _getCubeFaceName(unsigned char face) const
00121         { assert(face < 6); return mCubeFaceNames[face]; }
00123         static unsigned short _getPFBpp(PixelFormat ogrePF)
00124         { return Image::getNumElemBits(ogrePF); }
00125 
00126     public:
00128         D3D9Texture( const String& name, TextureType texType, IDirect3DDevice9 *pD3DDevice, TextureUsage usage );
00130         D3D9Texture( const String& name, TextureType texType, IDirect3DDevice9 *pD3DDevice, uint width, uint height, uint numMips, PixelFormat format, TextureUsage usage );
00132         ~D3D9Texture();
00133 
00135         void blitImage(const Image& src, const Image::Rect imgRect, const Image::Rect texRect );
00137         void blitToTexture( const Image &src, unsigned uStartX, unsigned uStartY );
00139         void copyToTexture( Texture * target );
00141         void loadImage( const Image &img );
00142 
00144         void load();
00146         void unload();
00147 
00149         IDirect3DBaseTexture9 *getTexture() 
00150         { assert(mpTex); return mpTex; }
00152         IDirect3DTexture9 *getNormTexture()
00153         { assert(mpNormTex); return mpNormTex; }
00155         IDirect3DCubeTexture9 *getCubeTexture()
00156         { assert(mpCubeTex); return mpCubeTex; }
00158         IDirect3DSurface9 *getDepthStencil() 
00159         { assert(mpZBuff); return mpZBuff; }
00160     };
00161 
00162     class D3D9RenderTexture : public RenderTexture
00163     {
00164     public:
00165         D3D9RenderTexture( const String & name, uint width, uint height, TextureType texType = TEX_TYPE_2D ) : RenderTexture( name, width, height, texType )
00166         {
00167             mPrivateTex = TextureManager::getSingleton().createManual( mName + "_PRIVATE##", texType, mWidth, mHeight, 0, PF_R8G8B8, TU_RENDERTARGET );
00168         }
00169         
00170         ~D3D9RenderTexture()
00171         {
00172             SAFE_DELETE(mPrivateTex);
00173         }
00174 
00175         virtual void getCustomAttribute( const String& name, void *pData )
00176         {
00177             if( name == "DDBACKBUFFER" )
00178             {
00179                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00180                 if (((D3D9Texture*)mPrivateTex)->getTextureType() == TEX_TYPE_2D)
00181                     ((D3D9Texture*)mPrivateTex)->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
00182                 else if (((D3D9Texture*)mPrivateTex)->getTextureType() == TEX_TYPE_CUBE_MAP)
00183                     ((D3D9Texture*)mPrivateTex)->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
00184                 else
00185                 {
00186                     Except( Exception::UNIMPLEMENTED_FEATURE, 
00187                             "getCustomAttribute is implemented only for 2D and cube textures !!!", 
00188                             "D3D9RenderTexture::getCustomAttribute" );
00189                 }
00190                 (*pSurf)->Release();
00191                 return;
00192             }
00193             else if( name == "D3DZBUFFER" )
00194             {
00195                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00196                 *pSurf = ((D3D9Texture*)mPrivateTex)->getDepthStencil();
00197                 return;
00198             }
00199             else if( name == "DDFRONTBUFFER" )
00200             {
00201                 IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
00202                 if (((D3D9Texture*)mPrivateTex)->getTextureType() == TEX_TYPE_2D)
00203                     ((D3D9Texture*)mPrivateTex)->getNormTexture()->GetSurfaceLevel( 0, &(*pSurf) );
00204                 else if (((D3D9Texture*)mPrivateTex)->getTextureType() == TEX_TYPE_CUBE_MAP)
00205                     ((D3D9Texture*)mPrivateTex)->getCubeTexture()->GetCubeMapSurface( (D3DCUBEMAP_FACES)0, 0, &(*pSurf) );
00206                 else
00207                 {
00208                     Except( Exception::UNIMPLEMENTED_FEATURE, 
00209                             "getCustomAttribute is implemented only for 2D and cube textures !!!", 
00210                             "D3D9RenderTexture::getCustomAttribute" );
00211                 }
00212                 (*pSurf)->Release();
00213                 return;
00214             }
00215             else if( name == "HWND" )
00216             {
00217                 HWND *pHwnd = (HWND*)pData;
00218                 *pHwnd = NULL;
00219                 return;
00220             }
00221             else if( name == "isTexture" )
00222             {
00223                 bool *b = reinterpret_cast< bool * >( pData );
00224                 *b = true;
00225                 return;
00226             }
00227         }
00228 
00229         bool requiresTextureFlipping() const { return false; }
00230         virtual void writeContentsToFile( const String & filename ) {}
00231 
00232     protected:
00234         Texture * mPrivateTex;
00235         
00236         virtual void _copyToTexture()
00237         {
00238             // Copy the newly-rendered data to the public texture surface.
00239             mPrivateTex->copyToTexture( mTexture );
00240         }
00241     };
00242 }
00243 
00244 #endif

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:22 2004