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 <D3D9.h>
0009: #include <D3DX9Math.h>
0010: 
0011: 
0012: 
0013: 
0014: //-----------------------------------------------------------------------------
0015: // Name: D3DUtil_InitMaterial()
0016: // Desc: Initializes a D3DMATERIAL9 structure, setting the diffuse and ambient
0017: //       colors. It does not set emissive or specular colors.
0018: //-----------------------------------------------------------------------------
0019: VOID D3DUtil_InitMaterial( D3DMATERIAL9& 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( D3DLIGHT9& 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( LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strTexture,
0042:                                LPDIRECT3DTEXTURE9* ppTexture,
0043:                                D3DFORMAT d3dFormat = D3DFMT_UNKNOWN );
0044: 
0045: 
0046: 
0047: 
0048: //-----------------------------------------------------------------------------
0049: // Name: D3DUtil_GetCubeMapViewMatrix()
0050: // Desc: Returns a view matrix for rendering to a face of a cubemap.
0051: //-----------------------------------------------------------------------------
0052: D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace );
0053: 
0054: 
0055: 
0056: 
0057: //-----------------------------------------------------------------------------
0058: // Name: D3DUtil_GetRotationFromCursor()
0059: // Desc: Returns a quaternion for the rotation implied by the window's cursor
0060: //       position.
0061: //-----------------------------------------------------------------------------
0062: D3DXQUATERNION D3DUtil_GetRotationFromCursor( HWND hWnd,
0063:                                               FLOAT fTrackBallRadius=1.0f );
0064: 
0065: 
0066: 
0067: 
0068: //-----------------------------------------------------------------------------
0069: // Name: D3DUtil_SetDeviceCursor
0070: // Desc: Builds and sets a cursor for the D3D device based on hCursor.
0071: //-----------------------------------------------------------------------------
0072: HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE9 pd3dDevice, HCURSOR hCursor,
0073:                                  BOOL bAddWatermark );
0074: 
0075: 
0076: //-----------------------------------------------------------------------------
0077: // Name: D3DUtil_D3DFormatToString
0078: // Desc: Returns the string for the given D3DFORMAT.
0079: //       bWithPrefix determines whether the string should include the "D3DFMT_"
0080: //-----------------------------------------------------------------------------
0081: TCHAR* D3DUtil_D3DFormatToString( D3DFORMAT format, bool bWithPrefix = true );
0082: 
0083: 
0084: //-----------------------------------------------------------------------------
0085: // Name: class CD3DArcBall
0086: // Desc:
0087: //-----------------------------------------------------------------------------
0088: class CD3DArcBall
0089: {
0090:     INT            m_iWidth;   // ArcBall's window width
0091:     INT            m_iHeight;  // ArcBall's window height
0092:     FLOAT          m_fRadius;  // ArcBall's radius in screen coords
0093:     FLOAT          m_fRadiusTranslation; // ArcBall's radius for translating the target
0094: 
0095:     D3DXQUATERNION m_qDown;               // Quaternion before button down
0096:     D3DXQUATERNION m_qNow;                // Composite quaternion for current drag
0097:     D3DXMATRIX  m_matRotation;         // Matrix for arcball's orientation
0098:     D3DXMATRIX  m_matRotationDelta;    // Matrix for arcball's orientation
0099:     D3DXMATRIX  m_matTranslation;      // Matrix for arcball's position
0100:     D3DXMATRIX  m_matTranslationDelta; // Matrix for arcball's position
0101:     BOOL           m_bDrag;               // Whether user is dragging arcball
0102:     BOOL           m_bRightHanded;        // Whether to use RH coordinate system
0103: 
0104:     D3DXVECTOR3 ScreenToVector( int sx, int sy );
0105: 
0106: public:
0107:     LRESULT     HandleMouseMessages( HWND, UINT, WPARAM, LPARAM );
0108: 
0109:     D3DXMATRIX* GetRotationMatrix()         { return &m_matRotation; }
0110:     D3DXMATRIX* GetRotationDeltaMatrix()    { return &m_matRotationDelta; }
0111:     D3DXMATRIX* GetTranslationMatrix()      { return &m_matTranslation; }
0112:     D3DXMATRIX* GetTranslationDeltaMatrix() { return &m_matTranslationDelta; }
0113:     BOOL        IsBeingDragged()            { return m_bDrag; }
0114: 
0115:     VOID        SetRadius( FLOAT fRadius );
0116:     VOID        SetWindow( INT w, INT h, FLOAT r=0.9 );
0117:     VOID        SetRightHanded( BOOL bRightHanded ) { m_bRightHanded = bRightHanded; }
0118: 
0119:                 CD3DArcBall();
0120:     VOID        Init();
0121: };
0122: 
0123: 
0124: 
0125: 
0126: //-----------------------------------------------------------------------------
0127: // Name: class CD3DCamera
0128: // Desc:
0129: //-----------------------------------------------------------------------------
0130: class CD3DCamera
0131: {
0132:     D3DXVECTOR3 m_vEyePt;       // Attributes for view matrix
0133:     D3DXVECTOR3 m_vLookatPt;
0134:     D3DXVECTOR3 m_vUpVec;
0135: 
0136:     D3DXVECTOR3 m_vView;
0137:     D3DXVECTOR3 m_vCross;
0138: 
0139:     D3DXMATRIX  m_matView;
0140:     D3DXMATRIX  m_matBillboard; // Special matrix for billboarding effects
0141: 
0142:     FLOAT       m_fFOV;         // Attributes for projection matrix
0143:     FLOAT       m_fAspect;
0144:     FLOAT       m_fNearPlane;
0145:     FLOAT       m_fFarPlane;
0146:     D3DXMATRIX  m_matProj;
0147: 
0148: public:
0149:     // Access functions
0150:     D3DXVECTOR3 GetEyePt()           { return m_vEyePt; }
0151:     D3DXVECTOR3 GetLookatPt()        { return m_vLookatPt; }
0152:     D3DXVECTOR3 GetUpVec()           { return m_vUpVec; }
0153:     D3DXVECTOR3 GetViewDir()         { return m_vView; }
0154:     D3DXVECTOR3 GetCross()           { return m_vCross; }
0155: 
0156:     FLOAT       GetFOV()             { return m_fFOV; }
0157:     FLOAT       GetAspect()          { return m_fAspect; }
0158:     FLOAT       GetNearPlane()       { return m_fNearPlane; }
0159:     FLOAT       GetFarPlane()        { return m_fFarPlane; }
0160: 
0161:     D3DXMATRIX  GetViewMatrix()      { return m_matView; }
0162:     D3DXMATRIX  GetBillboardMatrix() { return m_matBillboard; }
0163:     D3DXMATRIX  GetProjMatrix()      { return m_matProj; }
0164: 
0165:     VOID SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
0166:                         D3DXVECTOR3& vUpVec );
0167:     VOID SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
0168:                         FLOAT fFarPlane );
0169: 
0170:     CD3DCamera();
0171: };
0172: 
0173: 
0174: 
0175: 
0176: #endif // D3DUTIL_H
0177: