d3dutil.h
上传用户:fengshi120
上传日期:2014-07-17
资源大小:6155k
文件大小:7k
源码类别:

3D图形编程

开发平台:

C/C++

  1. //-----------------------------------------------------------------------------
  2. // File: D3DUtil.h
  3. //
  4. // Desc: Helper functions and typing shortcuts for Direct3D programming.
  5. //-----------------------------------------------------------------------------
  6. #ifndef D3DUTIL_H
  7. #define D3DUTIL_H
  8. #include <D3D9.h>
  9. #include <D3DX9Math.h>
  10. //-----------------------------------------------------------------------------
  11. // Name: D3DUtil_InitMaterial()
  12. // Desc: Initializes a D3DMATERIAL9 structure, setting the diffuse and ambient
  13. //       colors. It does not set emissive or specular colors.
  14. //-----------------------------------------------------------------------------
  15. VOID D3DUtil_InitMaterial( D3DMATERIAL9& mtrl, FLOAT r=0.0f, FLOAT g=0.0f,
  16.                                                FLOAT b=0.0f, FLOAT a=1.0f );
  17. //-----------------------------------------------------------------------------
  18. // Name: D3DUtil_InitLight()
  19. // Desc: Initializes a D3DLIGHT structure, setting the light position. The
  20. //       diffuse color is set to white, specular and ambient left as black.
  21. //-----------------------------------------------------------------------------
  22. VOID D3DUtil_InitLight( D3DLIGHT9& light, D3DLIGHTTYPE ltType,
  23.                         FLOAT x=0.0f, FLOAT y=0.0f, FLOAT z=0.0f );
  24. //-----------------------------------------------------------------------------
  25. // Name: D3DUtil_CreateTexture()
  26. // Desc: Helper function to create a texture. It checks the root path first,
  27. //       then tries the DXSDK media path (as specified in the system registry).
  28. //-----------------------------------------------------------------------------
  29. HRESULT D3DUtil_CreateTexture( LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strTexture,
  30.                                LPDIRECT3DTEXTURE9* ppTexture,
  31.                                D3DFORMAT d3dFormat = D3DFMT_UNKNOWN );
  32. //-----------------------------------------------------------------------------
  33. // Name: D3DUtil_GetCubeMapViewMatrix()
  34. // Desc: Returns a view matrix for rendering to a face of a cubemap.
  35. //-----------------------------------------------------------------------------
  36. D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace );
  37. //-----------------------------------------------------------------------------
  38. // Name: D3DUtil_GetRotationFromCursor()
  39. // Desc: Returns a quaternion for the rotation implied by the window's cursor
  40. //       position.
  41. //-----------------------------------------------------------------------------
  42. D3DXQUATERNION D3DUtil_GetRotationFromCursor( HWND hWnd,
  43.                                               FLOAT fTrackBallRadius=1.0f );
  44. //-----------------------------------------------------------------------------
  45. // Name: D3DUtil_SetDeviceCursor
  46. // Desc: Builds and sets a cursor for the D3D device based on hCursor.
  47. //-----------------------------------------------------------------------------
  48. HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE9 pd3dDevice, HCURSOR hCursor,
  49.                                  BOOL bAddWatermark );
  50. //-----------------------------------------------------------------------------
  51. // Name: D3DUtil_D3DFormatToString
  52. // Desc: Returns the string for the given D3DFORMAT.
  53. //       bWithPrefix determines whether the string should include the "D3DFMT_"
  54. //-----------------------------------------------------------------------------
  55. TCHAR* D3DUtil_D3DFormatToString( D3DFORMAT format, bool bWithPrefix = true );
  56. //-----------------------------------------------------------------------------
  57. // Name: class CD3DArcBall
  58. // Desc:
  59. //-----------------------------------------------------------------------------
  60. class CD3DArcBall
  61. {
  62.     INT            m_iWidth;   // ArcBall's window width
  63.     INT            m_iHeight;  // ArcBall's window height
  64.     FLOAT          m_fRadius;  // ArcBall's radius in screen coords
  65.     FLOAT          m_fRadiusTranslation; // ArcBall's radius for translating the target
  66.     BOOL           m_bDrag;               // Whether user is dragging arcball
  67.     BOOL           m_bRightHanded;        // Whether to use RH coordinate system
  68.  
  69. D3DXMATRIX    m_matRotationDelta;    // Matrix for arcball's orientation
  70.     D3DXMATRIX     m_matTranslationDelta; // Matrix for arcball's position
  71.     D3DXVECTOR3 ScreenToVector( int sx, int sy );
  72. public:
  73.     D3DXQUATERNION m_qDown;               // Quaternion before button down
  74.     D3DXQUATERNION m_qNow;                // Composite quaternion for current drag
  75.     D3DXMATRIX    m_matRotation;         // Matrix for arcball's orientation
  76.     D3DXMATRIX     m_matTranslation;      // Matrix for arcball's position
  77.     LRESULT     HandleMouseMessages( HWND, UINT, WPARAM, LPARAM );
  78.     D3DXMATRIX* GetRotationMatrix()         { return &m_matRotation; }
  79.     D3DXMATRIX* GetRotationDeltaMatrix()    { return &m_matRotationDelta; }
  80.     D3DXMATRIX* GetTranslationMatrix()      { return &m_matTranslation; }
  81.     D3DXMATRIX* GetTranslationDeltaMatrix() { return &m_matTranslationDelta; }
  82.     BOOL        IsBeingDragged()            { return m_bDrag; }
  83.     VOID        SetRadius( FLOAT fRadius );
  84.     VOID        SetWindow( INT w, INT h, FLOAT r=0.9 );
  85.     VOID        SetRightHanded( BOOL bRightHanded ) { m_bRightHanded = bRightHanded; }
  86.                 CD3DArcBall();
  87.     VOID        Init();
  88. };
  89. //-----------------------------------------------------------------------------
  90. // Name: class CD3DCamera
  91. // Desc:
  92. //-----------------------------------------------------------------------------
  93. class CD3DCamera
  94. {
  95.     D3DXVECTOR3 m_vEyePt;       // Attributes for view matrix
  96.     D3DXVECTOR3 m_vLookatPt;
  97.     D3DXVECTOR3 m_vUpVec;
  98.     D3DXVECTOR3 m_vView;
  99.     D3DXVECTOR3 m_vCross;
  100.     D3DXMATRIX  m_matView;
  101.     D3DXMATRIX  m_matBillboard; // Special matrix for billboarding effects
  102.     FLOAT       m_fFOV;         // Attributes for projection matrix
  103.     FLOAT       m_fAspect;
  104.     FLOAT       m_fNearPlane;
  105.     FLOAT       m_fFarPlane;
  106.     D3DXMATRIX  m_matProj;
  107. public:
  108.     // Access functions
  109.     D3DXVECTOR3 GetEyePt()           { return m_vEyePt; }
  110.     D3DXVECTOR3 GetLookatPt()        { return m_vLookatPt; }
  111.     D3DXVECTOR3 GetUpVec()           { return m_vUpVec; }
  112.     D3DXVECTOR3 GetViewDir()         { return m_vView; }
  113.     D3DXVECTOR3 GetCross()           { return m_vCross; }
  114.     FLOAT       GetFOV()             { return m_fFOV; }
  115.     FLOAT       GetAspect()          { return m_fAspect; }
  116.     FLOAT       GetNearPlane()       { return m_fNearPlane; }
  117.     FLOAT       GetFarPlane()        { return m_fFarPlane; }
  118.     D3DXMATRIX  GetViewMatrix()      { return m_matView; }
  119.     D3DXMATRIX  GetBillboardMatrix() { return m_matBillboard; }
  120.     D3DXMATRIX  GetProjMatrix()      { return m_matProj; }
  121.     VOID SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
  122.                         D3DXVECTOR3& vUpVec );
  123.     VOID SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
  124.                         FLOAT fFarPlane );
  125.     CD3DCamera();
  126. };
  127. #endif // D3DUTIL_H