0001: //-----------------------------------------------------------------------------
0002: // File: D3DUtil.h
0003: //
0004: // Desc: Helper functions and typing shortcuts for Direct3D programming.
0005: //-----------------------------------------------------------------------------
0006: #ifndef D3DUTIL_H
0007: #define D3DUTIL_H
0008: #include <D3D8.h>
0009: #include <D3DX8Math.h>
0010: 
0011: 
0012: 
0013: 
0014: //-----------------------------------------------------------------------------
0015: // Name: D3DUtil_InitMaterial()
0016: // Desc: Initializes a D3DMATERIAL8 structure, setting the diffuse and ambient
0017: //       colors. It does not set emissive or specular colors.
0018: //-----------------------------------------------------------------------------
0019: VOID D3DUtil_InitMaterial( D3DMATERIAL8& mtrl, FLOAT r=0.0f, FLOAT g=0.0f,
0020:                                                FLOAT b=0.0f, FLOAT a=1.0f );
0021: 
0022: 
0023: 
0024: 
0025: //-----------------------------------------------------------------------------
0026: // Name: D3DUtil_InitLight()
0027: // Desc: Initializes a D3DLIGHT structure, setting the light position. The
0028: //       diffuse color is set to white, specular and ambient left as black.
0029: //-----------------------------------------------------------------------------
0030: VOID D3DUtil_InitLight( D3DLIGHT8& light, D3DLIGHTTYPE ltType,
0031:                         FLOAT x=0.0f, FLOAT y=0.0f, FLOAT z=0.0f );
0032: 
0033: 
0034: 
0035: 
0036: //-----------------------------------------------------------------------------
0037: // Name: D3DUtil_CreateTexture()
0038: // Desc: Helper function to create a texture. It checks the root path first,
0039: //       then tries the DXSDK media path (as specified in the system registry).
0040: //-----------------------------------------------------------------------------
0041: HRESULT D3DUtil_CreateTexture( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strTexture,
0042:                                LPDIRECT3DTEXTURE8* ppTexture,
0043:                                D3DFORMAT d3dFormat = D3DFMT_UNKNOWN );
0044: 
0045: 
0046: 
0047: 
0048: //-----------------------------------------------------------------------------
0049: // Name: D3DUtil_SetColorKey()
0050: // Desc: Changes all texels matching the colorkey to transparent, black.
0051: //-----------------------------------------------------------------------------
0052: HRESULT D3DUtil_SetColorKey( LPDIRECT3DTEXTURE8 pTexture, DWORD dwColorKey );
0053: 
0054: 
0055: 
0056: 
0057: //-----------------------------------------------------------------------------
0058: // Name: D3DUtil_CreateVertexShader()
0059: // Desc: Assembles and creates a file-based vertex shader
0060: //-----------------------------------------------------------------------------
0061: HRESULT D3DUtil_CreateVertexShader( LPDIRECT3DDEVICE8 pd3dDevice, 
0062:                                     TCHAR* strFilename, DWORD* pdwVertexDecl,
0063:                                     DWORD* pdwVertexShader );
0064: 
0065:                                     
0066:                                     
0067:                                     
0068: //-----------------------------------------------------------------------------
0069: // Name: D3DUtil_GetCubeMapViewMatrix()
0070: // Desc: Returns a view matrix for rendering to a face of a cubemap.
0071: //-----------------------------------------------------------------------------
0072: D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace );
0073: 
0074: 
0075: 
0076: 
0077: //-----------------------------------------------------------------------------
0078: // Name: D3DUtil_GetRotationFromCursor()
0079: // Desc: Returns a quaternion for the rotation implied by the window's cursor
0080: //       position.
0081: //-----------------------------------------------------------------------------
0082: D3DXQUATERNION D3DUtil_GetRotationFromCursor( HWND hWnd,
0083:                                               FLOAT fTrackBallRadius=1.0f );
0084: 
0085: 
0086: 
0087: 
0088: //-----------------------------------------------------------------------------
0089: // Name: D3DUtil_SetDeviceCursor
0090: // Desc: Builds and sets a cursor for the D3D device based on hCursor.
0091: //-----------------------------------------------------------------------------
0092: HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE8 pd3dDevice, HCURSOR hCursor,
0093:                                  BOOL bAddWatermark );
0094: 
0095: 
0096: 
0097: 
0098: //-----------------------------------------------------------------------------
0099: // Name: class CD3DArcBall
0100: // Desc:
0101: //-----------------------------------------------------------------------------
0102: class CD3DArcBall
0103: {
0104:     INT            m_iWidth;   // ArcBall's window width
0105:     INT            m_iHeight;  // ArcBall's window height
0106:     FLOAT          m_fRadius;  // ArcBall's radius in screen coords
0107:     FLOAT          m_fRadiusTranslation; // ArcBall's radius for translating the target
0108: 
0109:     D3DXQUATERNION m_qDown;               // Quaternion before button down
0110:     D3DXQUATERNION m_qNow;                // Composite quaternion for current drag
0111:     D3DXMATRIX     m_matRotation;         // Matrix for arcball's orientation
0112:     D3DXMATRIX     m_matRotationDelta;    // Matrix for arcball's orientation
0113:     D3DXMATRIX     m_matTranslation;      // Matrix for arcball's position
0114:     D3DXMATRIX     m_matTranslationDelta; // Matrix for arcball's position
0115:     BOOL           m_bDrag;               // Whether user is dragging arcball
0116:     BOOL           m_bRightHanded;        // Whether to use RH coordinate system
0117: 
0118:     D3DXVECTOR3 ScreenToVector( int sx, int sy );
0119: 
0120: public:
0121:     LRESULT     HandleMouseMessages( HWND, UINT, WPARAM, LPARAM );
0122: 
0123:     D3DXMATRIX* GetRotationMatrix()         { return &m_matRotation; }
0124:     D3DXMATRIX* GetRotationDeltaMatrix()    { return &m_matRotationDelta; }
0125:     D3DXMATRIX* GetTranslationMatrix()      { return &m_matTranslation; }
0126:     D3DXMATRIX* GetTranslationDeltaMatrix() { return &m_matTranslationDelta; }
0127:     BOOL        IsBeingDragged()            { return m_bDrag; }
0128: 
0129:     VOID        SetRadius( FLOAT fRadius );
0130:     VOID        SetWindow( INT w, INT h, FLOAT r=0.9 );
0131:     VOID        SetRightHanded( BOOL bRightHanded ) { m_bRightHanded = bRightHanded; }
0132: 
0133:     CD3DArcBall();
0134: };
0135: 
0136: 
0137: 
0138: 
0139: //-----------------------------------------------------------------------------
0140: // Name: class CD3DCamera
0141: // Desc:
0142: //-----------------------------------------------------------------------------
0143: class CD3DCamera
0144: {
0145:     D3DXVECTOR3 m_vEyePt;       // Attributes for view matrix
0146:     D3DXVECTOR3 m_vLookatPt;
0147:     D3DXVECTOR3 m_vUpVec;
0148: 
0149:     D3DXVECTOR3 m_vView;
0150:     D3DXVECTOR3 m_vCross;
0151: 
0152:     D3DXMATRIX  m_matView;
0153:     D3DXMATRIX  m_matBillboard; // Special matrix for billboarding effects
0154: 
0155:     FLOAT       m_fFOV;         // Attributes for projection matrix
0156:     FLOAT       m_fAspect;
0157:     FLOAT       m_fNearPlane;
0158:     FLOAT       m_fFarPlane;
0159:     D3DXMATRIX  m_matProj;
0160: 
0161: public:
0162:     // Access functions
0163:     D3DXVECTOR3 GetEyePt()           { return m_vEyePt; }
0164:     D3DXVECTOR3 GetLookatPt()        { return m_vLookatPt; }
0165:     D3DXVECTOR3 GetUpVec()           { return m_vUpVec; }
0166:     D3DXVECTOR3 GetViewDir()         { return m_vView; }
0167:     D3DXVECTOR3 GetCross()           { return m_vCross; }
0168: 
0169:     D3DXMATRIX  GetViewMatrix()      { return m_matView; }
0170:     D3DXMATRIX  GetBillboardMatrix() { return m_matBillboard; }
0171:     D3DXMATRIX  GetProjMatrix()      { return m_matProj; }
0172: 
0173:     VOID SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
0174:                         D3DXVECTOR3& vUpVec );
0175:     VOID SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
0176:                         FLOAT fFarPlane );
0177: 
0178:     CD3DCamera();
0179: };
0180: 
0181: //-----------------------------------------------------------------------------
0182: // Helper macros for pixel shader instructions
0183: //-----------------------------------------------------------------------------
0184: 
0185: // Parameter writemasks
0186: #define D3DPSP_WRITEMASK_B   D3DSP_WRITEMASK_0
0187: #define D3DPSP_WRITEMASK_G   D3DSP_WRITEMASK_1
0188: #define D3DPSP_WRITEMASK_R   D3DSP_WRITEMASK_2
0189: #define D3DPSP_WRITEMASK_A   D3DSP_WRITEMASK_3
0190: #define D3DPSP_WRITEMASK_C   (D3DPSP_WRITEMASK_B|D3DPSP_WRITEMASK_G|D3DPSP_WRITEMASK_R)
0191: #define D3DPSP_WRITEMASK_ALL (D3DSP_WRITEMASK_0|D3DSP_WRITEMASK_1|D3DSP_WRITEMASK_2|D3DSP_WRITEMASK_3)
0192: #define D3DPSP_WRITEMASK_10  (D3DSP_WRITEMASK_0|D3DSP_WRITEMASK_1)
0193: #define D3DPSP_WRITEMASK_32  (D3DSP_WRITEMASK_2|D3DSP_WRITEMASK_3)
0194: 
0195: // Source and destination parameter token
0196: #define D3DPS_REGNUM_MASK(_Num)   ( (1L<<31) | ((_Num)&D3DSP_REGNUM_MASK) )
0197: #define D3DPS_DST(_Num)           ( D3DPS_REGNUM_MASK(_Num) | D3DSPR_TEMP | D3DPSP_WRITEMASK_ALL )
0198: #define D3DPS_SRC_TEMP(_Num)      ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEMP )
0199: #define D3DPS_SRC_INPUT(_Num)     ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_INPUT )
0200: #define D3DPS_SRC_CONST(_Num)     ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_CONST )
0201: #define D3DPS_SRC_TEXTURE(_Num)   ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEXTURE )
0202: #define D3DVS_SRC_ADDR(_Num)      ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_ADDR )
0203: #define D3DVS_SRC_RASTOUT(_Num)   ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_RASTOUT )
0204: #define D3DVS_SRC_ATTROUT(_Num)   ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_ATTROUT )
0205: #define D3DVS_SRC_TEXCRDOUT(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEXCRDOUT )
0206: 
0207: // Temp destination registers
0208: #define D3DS_DR0   D3DPS_DST(0)
0209: #define D3DS_DR1   D3DPS_DST(1)
0210: #define D3DS_DR2   D3DPS_DST(2)
0211: #define D3DS_DR3   D3DPS_DST(3)
0212: #define D3DS_DR4   D3DPS_DST(4)
0213: #define D3DS_DR5   D3DPS_DST(5)
0214: #define D3DS_DR6   D3DPS_DST(6)
0215: #define D3DS_DR7   D3DPS_DST(7)
0216: 
0217: // Temp source registers
0218: #define D3DS_SR0   D3DPS_SRC_TEMP(0)
0219: #define D3DS_SR1   D3DPS_SRC_TEMP(1)
0220: #define D3DS_SR2   D3DPS_SRC_TEMP(2)
0221: #define D3DS_SR3   D3DPS_SRC_TEMP(3)
0222: #define D3DS_SR4   D3DPS_SRC_TEMP(4)
0223: #define D3DS_SR5   D3DPS_SRC_TEMP(5)
0224: #define D3DS_SR6   D3DPS_SRC_TEMP(6)
0225: #define D3DS_SR7   D3DPS_SRC_TEMP(7)
0226: 
0227: // Texture parameters
0228: #define D3DS_T0   D3DPS_SRC_TEXTURE(0)
0229: #define D3DS_T1   D3DPS_SRC_TEXTURE(1)
0230: #define D3DS_T2   D3DPS_SRC_TEXTURE(2)
0231: #define D3DS_T3   D3DPS_SRC_TEXTURE(3)
0232: #define D3DS_T4   D3DPS_SRC_TEXTURE(4)
0233: #define D3DS_T5   D3DPS_SRC_TEXTURE(5)
0234: #define D3DS_T6   D3DPS_SRC_TEXTURE(6)
0235: #define D3DS_T7   D3DPS_SRC_TEXTURE(7)
0236: 
0237: // Constant (factor) source parameters
0238: #define D3DS_C0     D3DPS_SRC_CONST(0)
0239: #define D3DS_C1     D3DPS_SRC_CONST(1)
0240: #define D3DS_C2     D3DPS_SRC_CONST(2)
0241: #define D3DS_C3     D3DPS_SRC_CONST(3)
0242: #define D3DS_C4     D3DPS_SRC_CONST(4)
0243: #define D3DS_C5     D3DPS_SRC_CONST(5)
0244: #define D3DS_C6     D3DPS_SRC_CONST(6)
0245: #define D3DS_C7     D3DPS_SRC_CONST(7)
0246: 
0247: // Iterated source parameters (0==Diffuse, 1==specular)
0248: #define D3DS_V0     D3DPS_SRC_INPUT(0)
0249: #define D3DS_V1     D3DPS_SRC_INPUT(1)
0250: 
0251: 
0252: 
0253: 
0254: #endif // D3DUTIL_H
0255: