Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

d3dutil.h

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 // File: D3DUtil.h
00003 //
00004 // Desc: Helper functions and typing shortcuts for Direct3D programming.
00005 //
00006 // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved
00007 //-----------------------------------------------------------------------------
00008 #ifndef D3DUTIL_H
00009 #define D3DUTIL_H
00010 #include <D3D8.h>
00011 #include <D3DX8Math.h>
00012 
00013 
00014 
00015 
00016 //-----------------------------------------------------------------------------
00017 // Name: D3DUtil_InitMaterial()
00018 // Desc: Initializes a D3DMATERIAL8 structure, setting the diffuse and ambient
00019 //       colors. It does not set emissive or specular colors.
00020 //-----------------------------------------------------------------------------
00021 VOID D3DUtil_InitMaterial( D3DMATERIAL8& mtrl, FLOAT r=0.0f, FLOAT g=0.0f,
00022                                                FLOAT b=0.0f, FLOAT a=1.0f );
00023 
00024 
00025 
00026 
00027 //-----------------------------------------------------------------------------
00028 // Name: D3DUtil_InitLight()
00029 // Desc: Initializes a D3DLIGHT structure, setting the light position. The
00030 //       diffuse color is set to white, specular and ambient left as black.
00031 //-----------------------------------------------------------------------------
00032 VOID D3DUtil_InitLight( D3DLIGHT8& light, D3DLIGHTTYPE ltType,
00033                         FLOAT x=0.0f, FLOAT y=0.0f, FLOAT z=0.0f );
00034 
00035 
00036 
00037 
00038 //-----------------------------------------------------------------------------
00039 // Name: D3DUtil_CreateTexture()
00040 // Desc: Helper function to create a texture. It checks the root path first,
00041 //       then tries the DXSDK media path (as specified in the system registry).
00042 //-----------------------------------------------------------------------------
00043 HRESULT D3DUtil_CreateTexture( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strTexture,
00044                                LPDIRECT3DTEXTURE8* ppTexture,
00045                                D3DFORMAT d3dFormat = D3DFMT_UNKNOWN );
00046 
00047 
00048 
00049 
00050 //-----------------------------------------------------------------------------
00051 // Name: D3DUtil_SetColorKey()
00052 // Desc: Changes all texels matching the colorkey to transparent, black.
00053 //-----------------------------------------------------------------------------
00054 HRESULT D3DUtil_SetColorKey( LPDIRECT3DTEXTURE8 pTexture, DWORD dwColorKey );
00055 
00056 
00057 
00058 
00059 //-----------------------------------------------------------------------------
00060 // Name: D3DUtil_CreateVertexShader()
00061 // Desc: Assembles and creates a file-based vertex shader
00062 //-----------------------------------------------------------------------------
00063 HRESULT D3DUtil_CreateVertexShader( LPDIRECT3DDEVICE8 pd3dDevice, 
00064                                     TCHAR* strFilename, DWORD* pdwVertexDecl,
00065                                     DWORD* pdwVertexShader );
00066 
00067                                     
00068                                     
00069                                     
00070 //-----------------------------------------------------------------------------
00071 // Name: D3DUtil_GetCubeMapViewMatrix()
00072 // Desc: Returns a view matrix for rendering to a face of a cubemap.
00073 //-----------------------------------------------------------------------------
00074 D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace );
00075 
00076 
00077 
00078 
00079 //-----------------------------------------------------------------------------
00080 // Name: D3DUtil_GetRotationFromCursor()
00081 // Desc: Returns a quaternion for the rotation implied by the window's cursor
00082 //       position.
00083 //-----------------------------------------------------------------------------
00084 D3DXQUATERNION D3DUtil_GetRotationFromCursor( HWND hWnd,
00085                                               FLOAT fTrackBallRadius=1.0f );
00086 
00087 
00088 
00089 
00090 //-----------------------------------------------------------------------------
00091 // Name: D3DUtil_SetDeviceCursor
00092 // Desc: Builds and sets a cursor for the D3D device based on hCursor.
00093 //-----------------------------------------------------------------------------
00094 HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE8 pd3dDevice, HCURSOR hCursor,
00095                                  BOOL bAddWatermark );
00096 
00097 
00098 
00099 
00100 //-----------------------------------------------------------------------------
00101 // Name: class CD3DArcBall
00102 // Desc:
00103 //-----------------------------------------------------------------------------
00104 class CD3DArcBall
00105 {
00106     INT            m_iWidth;   // ArcBall's window width
00107     INT            m_iHeight;  // ArcBall's window height
00108     FLOAT          m_fRadius;  // ArcBall's radius in screen coords
00109     FLOAT          m_fRadiusTranslation; // ArcBall's radius for translating the target
00110 
00111     D3DXQUATERNION m_qDown;               // Quaternion before button down
00112     D3DXQUATERNION m_qNow;                // Composite quaternion for current drag
00113     D3DXMATRIX     m_matRotation;         // Matrix for arcball's orientation
00114     D3DXMATRIX     m_matRotationDelta;    // Matrix for arcball's orientation
00115     D3DXMATRIX     m_matTranslation;      // Matrix for arcball's position
00116     D3DXMATRIX     m_matTranslationDelta; // Matrix for arcball's position
00117     BOOL           m_bDrag;               // Whether user is dragging arcball
00118     BOOL           m_bRightHanded;        // Whether to use RH coordinate system
00119 
00120     D3DXVECTOR3 ScreenToVector( int sx, int sy );
00121 
00122 public:
00123     LRESULT     HandleMouseMessages( HWND, UINT, WPARAM, LPARAM );
00124 
00125     D3DXMATRIX* GetRotationMatrix()         { return &m_matRotation; }
00126     D3DXMATRIX* GetRotationDeltaMatrix()    { return &m_matRotationDelta; }
00127     D3DXMATRIX* GetTranslationMatrix()      { return &m_matTranslation; }
00128     D3DXMATRIX* GetTranslationDeltaMatrix() { return &m_matTranslationDelta; }
00129     BOOL        IsBeingDragged()            { return m_bDrag; }
00130 
00131     VOID        SetRadius( FLOAT fRadius );
00132     VOID        SetWindow( INT w, INT h, FLOAT r=0.9 );
00133     VOID        SetRightHanded( BOOL bRightHanded ) { m_bRightHanded = bRightHanded; }
00134 
00135     CD3DArcBall();
00136 };
00137 
00138 
00139 
00140 
00141 //-----------------------------------------------------------------------------
00142 // Name: class CD3DCamera
00143 // Desc:
00144 //-----------------------------------------------------------------------------
00145 class CD3DCamera
00146 {
00147     D3DXVECTOR3 m_vEyePt;       // Attributes for view matrix
00148     D3DXVECTOR3 m_vLookatPt;
00149     D3DXVECTOR3 m_vUpVec;
00150 
00151     D3DXVECTOR3 m_vView;
00152     D3DXVECTOR3 m_vCross;
00153 
00154     D3DXMATRIX  m_matView;
00155     D3DXMATRIX  m_matBillboard; // Special matrix for billboarding effects
00156 
00157     FLOAT       m_fFOV;         // Attributes for projection matrix
00158     FLOAT       m_fAspect;
00159     FLOAT       m_fNearPlane;
00160     FLOAT       m_fFarPlane;
00161     D3DXMATRIX  m_matProj;
00162 
00163 public:
00164     // Access functions
00165     D3DXVECTOR3 GetEyePt()           { return m_vEyePt; }
00166     D3DXVECTOR3 GetLookatPt()        { return m_vLookatPt; }
00167     D3DXVECTOR3 GetUpVec()           { return m_vUpVec; }
00168     D3DXVECTOR3 GetViewDir()         { return m_vView; }
00169     D3DXVECTOR3 GetCross()           { return m_vCross; }
00170 
00171     D3DXMATRIX  GetViewMatrix()      { return m_matView; }
00172     D3DXMATRIX  GetBillboardMatrix() { return m_matBillboard; }
00173     D3DXMATRIX  GetProjMatrix()      { return m_matProj; }
00174 
00175     VOID SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
00176                         D3DXVECTOR3& vUpVec );
00177     VOID SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
00178                         FLOAT fFarPlane );
00179 
00180     CD3DCamera();
00181 };
00182 
00183 //-----------------------------------------------------------------------------
00184 // Helper macros for pixel shader instructions
00185 //-----------------------------------------------------------------------------
00186 
00187 // Parameter writemasks
00188 #define D3DPSP_WRITEMASK_B   D3DSP_WRITEMASK_0
00189 #define D3DPSP_WRITEMASK_G   D3DSP_WRITEMASK_1
00190 #define D3DPSP_WRITEMASK_R   D3DSP_WRITEMASK_2
00191 #define D3DPSP_WRITEMASK_A   D3DSP_WRITEMASK_3
00192 #define D3DPSP_WRITEMASK_C   (D3DPSP_WRITEMASK_B|D3DPSP_WRITEMASK_G|D3DPSP_WRITEMASK_R)
00193 #define D3DPSP_WRITEMASK_ALL (D3DSP_WRITEMASK_0|D3DSP_WRITEMASK_1|D3DSP_WRITEMASK_2|D3DSP_WRITEMASK_3)
00194 #define D3DPSP_WRITEMASK_10  (D3DSP_WRITEMASK_0|D3DSP_WRITEMASK_1)
00195 #define D3DPSP_WRITEMASK_32  (D3DSP_WRITEMASK_2|D3DSP_WRITEMASK_3)
00196 
00197 // Source and destination parameter token
00198 #define D3DPS_REGNUM_MASK(_Num)   ( (1L<<31) | ((_Num)&D3DSP_REGNUM_MASK) )
00199 #define D3DPS_DST(_Num)           ( D3DPS_REGNUM_MASK(_Num) | D3DSPR_TEMP | D3DPSP_WRITEMASK_ALL )
00200 #define D3DPS_SRC_TEMP(_Num)      ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEMP )
00201 #define D3DPS_SRC_INPUT(_Num)     ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_INPUT )
00202 #define D3DPS_SRC_CONST(_Num)     ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_CONST )
00203 #define D3DPS_SRC_TEXTURE(_Num)   ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEXTURE )
00204 #define D3DVS_SRC_ADDR(_Num)      ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_ADDR )
00205 #define D3DVS_SRC_RASTOUT(_Num)   ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_RASTOUT )
00206 #define D3DVS_SRC_ATTROUT(_Num)   ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_ATTROUT )
00207 #define D3DVS_SRC_TEXCRDOUT(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEXCRDOUT )
00208 
00209 // Temp destination registers
00210 #define D3DS_DR0   D3DPS_DST(0)
00211 #define D3DS_DR1   D3DPS_DST(1)
00212 #define D3DS_DR2   D3DPS_DST(2)
00213 #define D3DS_DR3   D3DPS_DST(3)
00214 #define D3DS_DR4   D3DPS_DST(4)
00215 #define D3DS_DR5   D3DPS_DST(5)
00216 #define D3DS_DR6   D3DPS_DST(6)
00217 #define D3DS_DR7   D3DPS_DST(7)
00218 
00219 // Temp source registers
00220 #define D3DS_SR0   D3DPS_SRC_TEMP(0)
00221 #define D3DS_SR1   D3DPS_SRC_TEMP(1)
00222 #define D3DS_SR2   D3DPS_SRC_TEMP(2)
00223 #define D3DS_SR3   D3DPS_SRC_TEMP(3)
00224 #define D3DS_SR4   D3DPS_SRC_TEMP(4)
00225 #define D3DS_SR5   D3DPS_SRC_TEMP(5)
00226 #define D3DS_SR6   D3DPS_SRC_TEMP(6)
00227 #define D3DS_SR7   D3DPS_SRC_TEMP(7)
00228 
00229 // Texture parameters
00230 #define D3DS_T0   D3DPS_SRC_TEXTURE(0)
00231 #define D3DS_T1   D3DPS_SRC_TEXTURE(1)
00232 #define D3DS_T2   D3DPS_SRC_TEXTURE(2)
00233 #define D3DS_T3   D3DPS_SRC_TEXTURE(3)
00234 #define D3DS_T4   D3DPS_SRC_TEXTURE(4)
00235 #define D3DS_T5   D3DPS_SRC_TEXTURE(5)
00236 #define D3DS_T6   D3DPS_SRC_TEXTURE(6)
00237 #define D3DS_T7   D3DPS_SRC_TEXTURE(7)
00238 
00239 // Constant (factor) source parameters
00240 #define D3DS_C0     D3DPS_SRC_CONST(0)
00241 #define D3DS_C1     D3DPS_SRC_CONST(1)
00242 #define D3DS_C2     D3DPS_SRC_CONST(2)
00243 #define D3DS_C3     D3DPS_SRC_CONST(3)
00244 #define D3DS_C4     D3DPS_SRC_CONST(4)
00245 #define D3DS_C5     D3DPS_SRC_CONST(5)
00246 #define D3DS_C6     D3DPS_SRC_CONST(6)
00247 #define D3DS_C7     D3DPS_SRC_CONST(7)
00248 
00249 // Iterated source parameters (0==Diffuse, 1==specular)
00250 #define D3DS_V0     D3DPS_SRC_INPUT(0)
00251 #define D3DS_V1     D3DPS_SRC_INPUT(1)
00252 
00253 
00254 
00255 
00256 #endif // D3DUTIL_H

Generated on Thu May 23 17:51:00 2002 by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001